Zbarcea Christian
Zbarcea Christian

Reputation: 9548

I don't get response from DatagramPacket with Java

I want to check if one of a remote host server accepts UDP Packets on a specific port. With SocketServer I can set up a TCP/IP connection and it worked, but I must use UDP.

DatagramSocket ds;
DatagramPacket dp;
byte[] bytes = new byte[32];
InetAddress IAddress = new InetAddress.getByName("86.55.X.X");    

ds = new DatagramSocket();
ds.setSoTimeout(100);
ds.connect(IAddress, 1122);
ds.send(dp);
ds.isConnected();
dp = new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
ds.close();

The code above it should work, I don't know how to test it because I don't find any host/soft which/where I could run/test the code above. I'm trying to test it on a remote host. The remote host has Windows OS and Firewall disabled. It has a router and the firewall is also disabled. I can access the remote server and make changes in the router. My question is: can a remote host accept UDP packets if the only opened ports are for TCP/IP? (Port forward). In the router control panel I can make port forward for only TCP/IP.

Upvotes: 1

Views: 1102

Answers (2)

Will
Will

Reputation: 6286

You need to pass the following variable to your JRE

java -Djava.net.preferIPv4Stack=true  ...

Upvotes: 0

Viç
Viç

Reputation: 356

You should definitely try on localhost (on localhost you can easily debug your software on what is going on when something is received).

Moreover you should want to bind your DatagramSocket on a port in order to receive a packet. And you do not need a DatagramSocket to be connected when sending.

Upvotes: 1

Related Questions