简体   繁体   English

DatagramPacket中没有地址

[英]No address in DatagramPacket

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 . 我试图将多人游戏形式添加到一个简单的pong游戏中,但是当我尝试启动DatagramPacket并尝试读取IP和端口时,它说ip为null ,端口为-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. 我以为可能是因为套接字尚未接收到数据包,但是当我看时,我发现socket.recieve(packet)之后的所有代码都没有运行。

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

DatagramPacket 's getAddress returns the IP address of the machine to which this datagram is being sent or from which the datagram was received. DatagramPacketgetAddress返回此数据报发送到的机器或从其接收数据报的机器的IP地址。

In the first System.out.println you have just created the object, but have not done any network I/O with it. 在第一个System.out.println您刚刚创建了对象,但尚未对其执行任何网络I / O。

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. 如果发生I / O错误,则可能是数据报未初始化,因此IP地址仍然为null ,端口-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? 如果在socket.receive()之后什么也没发生,我认为调用已被阻塞,等待一个数据包进入。您是否真的在运行连接到服务器代码的客户端?

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. 修复该问题后,您就可以解决为什么您可能无法像ccarton所建议的那样从其他PC接收数据包的问题。

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? 现在您仍然遇到同样的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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