简体   繁体   English

Java DatagramSocket发送但不接收

[英]Java DatagramSocket sending but not receiving

I'm working on a game in Java, and I'm trying to communicate between a client and server on the same computer over the internet, and eventually between multiple computers. 我正在使用Java开发游戏,并且试图通过Internet在同一台计算机上的客户端和服务器之间进行通信,最终在多台计算机之间进行通信。 I used to be able to, but now the server doesn't seem to be receiving any packets from the client. 我曾经能够,但是现在服务器似乎没有从客户端接收任何数据包。 I'll try to post all the relevant code. 我将尝试发布所有相关代码。

This is on the server: 这是在服务器上:

private DatagramSocket m_Socket = new DatagramSocket( 8000 );
private DatagramPacket m_DatagramPacket = new DatagramPacket( new byte[ 1024 ], 1024 );

... ...

while( true )
{
    m_Socket.receive( m_DatagramPacket );
    byte[] data = new byte[ 1024 ];
    data = m_DatagramPacket.getData( );
    System.out.println( "SERVER RECEIVING: " + data );
}

This is on the client: 这是在客户端上:

private DatagramSocket m_Socket = new DatagramSocket( null );
private DatagramPacket m_DatagramPacket = new DatagramPacket( new byte[ 1024 ], 1024 );

... ...

m_DatagramPacket.setAddress( InetAddress.getByName( "72.49.50.49" ) );
m_DatagramPacket.setPort( 8000 );
m_DatagramPacket.setLength( length );
m_DatagramPacket.setData( data );

// "data" is the byte array, "length" is the length of the array

m_Socket.send( m_DatagramPacket );

I'm seeing the packet in Wireshark so I know it's sending. 我在Wireshark中看到了数据包,所以我知道它正在发送。

EDIT: I may not understand how Wireshark works, is the screenshot below sending or receiving the packet? 编辑:我可能不明白Wireshark的工作原理,下面的屏幕快照是发送还是接收数据包?

Wireshark: Wireshark: Wireshark屏幕截图

Firewall exception: 防火墙例外: 开启端口8000

Port forwarding: 转发端口: 转发端口8000

EDIT: I also ran this port tester program to test port 8000 and it says the port is open 编辑:我也运行了此端口测试程序以测试端口8000,它说该端口是打开的 端口测试仪

UPDATE: For some reason I guess our routers were just refusing connections from the source local IP to the external IP destination. 更新:出于某种原因,我想我们的路由器只是拒绝从源本地IP到外部IP目标的连接。 I can connect to the server using the local IP from the same network and using the external IP from a different network 我可以使用来自同一网络的本地IP和来自不同网络的外部IP连接到服务器

If your client side you are creating an unbounded DatagramSocket (By passing null to the DatagramSocket(SocketAddress bindaddr) ) which means there will not be any originating port for that socket. 如果您的客户端正在创建一个无边界的DatagramSocket (通过将null传递给DatagramSocket(SocketAddress bindaddr) ),这意味着该套接字将没有任何原始端口。 Eventhough this might work, this is clearly not the correct approach. 尽管这可能有效,但这显然不是正确的方法。 In your client side you must do the following change. 在客户端,您必须进行以下更改。

private DatagramSocket m_Socket = new DatagramSocket();
private DatagramPacket m_DatagramPacket = new DatagramPacket( new byte[ 1024 ], 1024 );

Using the Default DatagramSocket constructor will bind the socket to next available port and will guarantee that there is a source port involved in the communication. 使用Default DatagramSocket构造函数会将套接字绑定到下一个可用端口,并保证通信中涉及源端口。

And your firewall exception doesn't have a Source IP and Source Port pattern or restriction. 而且您的防火墙例外没有源IP和源端口模式或限制。 Please check based on your Router whether this is required. 请根据您的路由器检查是否需要这样做。

Firstly, assuming that your IP and other codes you write but not in above is correct,I have done a lot of test, even tried to change the settings of the firewall.But it still works. 首先,假设您编写的IP和其他代码正确无误,我已经做了很多测试,甚至尝试更改防火墙的设置。但是它仍然可以正常工作。 So, I think that maybe because of the unreliability of UDP protocol, your server sometimes can not receive the data from the client.You can just try to use TCP protocol. 因此,我认为也许是由于UDP协议的不可靠性,您的服务器有时无法从客户端接收数据。您可以尝试使用TCP协议。

Secondly, in your screenshot of PcWinTech.com v3.0.0 , it seems that your computer is in the subnetwork. 其次,在PcWinTech.com v3.0.0的屏幕快照中,您的计算机似乎在子网中。 So I gust that maybe the IP("72.49.50.49") is your router's IP, and I suggest you to check the settings of the router whether the router can transmit the data to your computer ,say, port forwarding.And I have just find a article that may help you. 因此,我建议IP(“ 72.49.50.49”)是您路由器的IP,建议您检查路由器的设置,看看路由器是否可以将数据传输到计算机,例如端口转发。查找可能对您有帮助的文章

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

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