-1

I am attempting to add a multiplayer form to a simple pong game, but when I try to start the DatagramPacket and try to read the IP and port it says the ip is null and the port is -1. Does anyone know why it would be doing this? I thought maybe it was because the socket hadn't recieved the packet yet, but when I look I saw that all code after socket.recieve(packet) isn't running.

Code where I start the server:

public GameServer(PongEngine engine) {
    this.engine = engine;
    try {
        this.socket = new DatagramSocket(4269);
    } catch (SocketException e) {
        e.printStackTrace();
    }
}

public void run() {
    while(true) {
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        System.out.println(packet.getAddress() + ":" + packet.getPort());
        try {
            socket.receive(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String message = new String(packet.getData());
        if(message.trim().equalsIgnoreCase("ping")) {
            System.out.println("CLIENT[" + packet.getAddress() + ":" + packet.getPort() + "] > " + message);
            sendData("pong".getBytes(), packet.getAddress(), packet.getPort());
        }
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Your first thought was correct. The IP and port values are not populated until after the receive function is called. If the code after that point is not executing then either an exception is being thrown, or there simply isn't a client connecting. The receive will block until it gets a packet. – ccarton Sep 25 '16 at 22:12
  • I am running the client and server correctly as far as I know. Here is my client: http://pastebin.com/2zMdgNVP. Do you see any problems? – TheGamerPlayz Sep 25 '16 at 22:42

2 Answers2

2

DatagramPacket's getAddress returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.

In the first System.out.println you have just created the object, but have not done any network I/O with it.

Then you ignore the exception and just try to work with the datagram. If there was an I/O error, it's likely that the datagram was not initialized and hence still has IP address null and port -1.

If nothing happens after socket.receive() I'd assume the call is blocked, waiting for a packet to come in. Do you actually run the client that connects to your server code?

Robert
  • 7,394
  • 40
  • 45
  • 64
1

To add to Roberts answer, your code is simply out of order. Once you have that fixed then you can address why you might not be recieving a packet form the other PC like ccarton suggested.

Try this, and note the two comments

public void run() {
    while(true) {
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);

        try {
            //Wait for packet (The code will not move on until a packet is received or there is an error)
            System.out.println("Waiting for packet");
            socket.receive(packet);

            //Move your socket/port info after receiving a packet so you don't get null or -1
            System.out.println("Packet received: "+ packet.getAddress() + ":" + packet.getPort());

            //Move your code inside try, rather than after
            String message = new String(packet.getData());
            if(message.trim().equalsIgnoreCase("ping")) {
                System.out.println("CLIENT[" + packet.getAddress() + ":" + packet.getPort() + "] > " + message);
                sendData("pong".getBytes(), packet.getAddress(), packet.getPort());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now do you still get the same issues?

sorifiend
  • 5,927
  • 1
  • 28
  • 45