数据不会通过DatagramSocket发送

时间:2014-08-24 17:35:17

标签: java chat datagram

由于某种原因,我的datagramsocket中的数据不会发送。我试图做一个简单的聊天客户端。 以下是我的聊天客户端的工作方式:

  1. 此人输入用户名
  2. 他们输入IP
  3. 连接
  4. 他们发送消息
  5. 这是我的代码:

    客户1(发送和接收):

    // Server Info
    public static int serverPort = 8743;
    public static String serverName = "ServerName";
    
    // Functions for the server
    
    @SuppressWarnings("resource")
    public static void main(String args[]) throws Exception {
        DatagramSocket serverSocket = new DatagramSocket();
        byte[] receiveData = new byte[1024];
        System.out.println("You are now the host.");
        boolean enteredDetails = false;
        String username = null;
        String ipAddress = null;
        System.out.print("Enter your username: ");
        Scanner userInput = new Scanner(System.in);
        if (userInput.hasNext()) {
            username = userInput.next();
        }
        System.out.print("Enter the IP: ");
        if (userInput.hasNext()) {
            ipAddress = userInput.next();
            enteredDetails = true;
        }
    
        while (true) {
            Scanner message = new Scanner(System.in);
            if (message.hasNextLine()) {
                String messageFormat = username + ": " + message.nextLine();
                byte[] sendData = messageFormat.getBytes();
                InetAddress IPAddress = InetAddress.getByName(ipAddress);
                DatagramPacket sendPacket = new DatagramPacket(sendData,
                        sendData.length, InetAddress.getByName(ipAddress), 9876);
                serverSocket.send(sendPacket);
                System.out.println(messageFormat);
            }
            // Recieve text
            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String(receivePacket.getData());
            System.out.println(sentence);
        }
    }
    

    对于第二个客户(仅收到):

    @SuppressWarnings("resource")
    public static void main(String args[]) throws Exception {
        DatagramSocket serverSocket = new DatagramSocket();
        byte[] receiveData = new byte[1024];
        boolean enteredDetails = false;
        String username = null;
        String ipAddress = null;
        System.out.print("Enter your username: ");
        Scanner userInput = new Scanner(System.in);
        if (userInput.hasNext()) {
            username = userInput.next();
        }
        System.out.print("Enter the IP: ");
        if (userInput.hasNext()) {
            ipAddress = userInput.next();
            enteredDetails = true;
        }
    
        while (true) {
            // Recieve text
            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String(receivePacket.getData());
            System.out.println(sentence);
        }
    }
    

1 个答案:

答案 0 :(得分:0)

客户1:

DatagramPacket sendPacket = new DatagramPacket(sendData,
                sendData.length, InetAddress.getByName(ipAddress), 9876);

此客户端正在发送到端口9876。

客户2:

DatagramSocket serverSocket = new DatagramSocket();

这个客户端无法接收任何东西,因为它在每次运行时都在不同的端口上接收。它应该是:

DatagramSocket serverSocket = new DatagramSocket(9876);