peer 2 peer UDP sockets?

I try to make a peer-to-peer network using UDP sockets, but I can't manage to get the two nodes to communicate with each other. It seems the node acting as the client can't connect to the node acting as the server for some reason.

public class PeerNode {

    DatagramSocket clientSocket;

    public void sendRequest(String sentence, String host, int port) throws UnknownHostException, IOException{

        clientSocket = new DatagramSocket (port);
        InetAddress IPAddress = InetAddress.getByName(host);   
        byte[] sendData = new byte[1024];       
        System.out.println(sentence);
        sendData = sentence.getBytes();        
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 
        clientSocket.send(sendPacket);
    }

    void mainLoop() throws IOException{
        try {
            // Establish the listen socket.
            DatagramSocket serverSocket = new DatagramSocket(9876);   
            byte[] receiveData = new byte[1024]; 
            System.out.println("listening on port " + serverSocket.getLocalPort());
            //int count =0;
            while (true) {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);
                String sentence = new String( receivePacket.getData());
                System.out.println("Server:  "+sentence);
                RequestHandler request =new RequestHandler     

                // Create a new thread to process the request.
                Thread thread = new Thread(request);

                // Start the thread.
                thread.start(); 
                //count++;
            }     
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


public class RequestHandler implements Runnable{

    DatagramPacket packet;
    DatagramSocket socket;
    byte[] sendData = new byte[1024];

    public RequestHandler(DatagramPacket packet,DatagramSocket socket) throws Exception 
    {
        this.packet = packet;
        this.socket = socket;
        String sentence = new String(packet.getData()); 
        System.out.println("handler : "+sentence);
    }

    @Override
    public void run() 
    {
        try {
            System.out.println("Test");
            processRequest();
        } catch (Exception e) {
            System.out.println(e);
        }
    }   

    /*
     * Gets a request from a node. 
     */
    private void processRequest() throws Exception 
    {
        String sentence = new String( packet.getData());                   
        System.out.println("RECEIVED: " + sentence);                   
        InetAddress IPAddress =packet.getAddress();                   
        int port = packet.getPort();                   
        String capitalizedSentence = sentence.toUpperCase();                   
        sendData = capitalizedSentence.getBytes();                   
        DatagramPacket sendPacket =new DatagramPacket(sendData, sendData.length, IPAddress, port);
        socket.send(sendPacket);
        socket.close();
    }

}


public class ClientServer {

    public static void main( String [] args ) throws IOException{

        PeerNode node1=new PeerNode();
        PeerNode node2=new PeerNode();

        node1.sendRequest("Hellllll", "localhost",9876);
        node2.mainLoop();

    }
}
链接地址: http://www.djcxy.com/p/35096.html

上一篇: 不受支持的major.minor版本52.0

下一篇: 对等2对等UDP套接字?