[ DatagramSocket never receives ]
Despite me knowing that my UDP packets are arriving as expected (via Wireshark), and having the Windows firewall turned off - my very simple Java code never receives any packets.
byte[] buffer = new byte[2048];
DatagramSocket socket = new DatagramSocket( 50000 );
DatagramPacket packet = new DatagramPacket( buffer, buffer.length );
socket.receive( packet );
I have 3 network adapters on my machine, so I tried being very specific about IPs. I tried setting the socket to look at the local address the packets were arriving on - didn't receive anything. And I also tried connecting the socket to the remote IP the packets were being sent on - didn't receive anything. All the while Wireshark is seeing all the 160 packets per second arriving...
My only other thought was that I'm not calculating the UDP checksum on the sending end (the UDP packets are assembled 'by hand' on an embedded system), it is just being set to 0x0000 - does DatagramSocket
discard UDP packets without a checksum!?
I wrote a simple test server to aid diagnosing the problem per @Andreas' suggestion.
InetAddress outAddr = InetAddress.getLocalHost();
InetAddress inAddr = InetAddress.getByName( "192.178.178.0" );
byte[] buffer = ( "Hello" ).getBytes();
DatagramSocket socket = new DatagramSocket( 50000, outAddr );
while ( true ) {
DatagramPacket packet = new DatagramPacket( buffer, buffer.length,
inAddr, 50000 );
socket.send( packet );
try {
Thread.sleep( 500 );
} catch ( InterruptedException e ) {
// Ignore.
}
}
Wireshark dutifully notes that it is receiving them all - but my client still doesn't, even when the receiving IP is explicitly set:
InetAddress inAddr = InetAddress.getByName( "192.178.178.0" );
DatagramSocket socket = new DatagramSocket( 50000, inAddr );
...
socket.receive( packet );
Something odd I have just noticed though, when I check which NetworkInterface
is being used for sending and receiving, Java reports back the right one (the one 'owning' 192.178.178.0) - but Wireshark only sees my test server packets on a different network device. Any ideas on what is going on?
Answer 1
Ok, well this is embarassing. I turned off the Windows firewall for Private and Work networks - but it turns out Windows considered the one I actually needed to be a Public network...
It works fine now. I considered deleting this question, but hopefully it will help some other muppet.