简体   繁体   中英

Data wont send through DatagramSocket

For some reason, the data in my datagramsocket wont send. Im trying to make a simple chatclient. Here is how my chat client is going to work:

  1. The person enters the username
  2. They enter the IP
  3. It connects
  4. They send messages

Here is my code:

Client 1 (Sends and recieves):

// 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);
    }
}

For the second client (recieves only):

@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);
    }
}

Client 1:

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

This client is sending to port 9876.

Client 2:

DatagramSocket serverSocket = new DatagramSocket();

There's no way this client is ever going to receive anything, as it is receiving on an unknown port which is different every time you run it. It should be:

DatagramSocket serverSocket = new DatagramSocket(9876);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM