1
votes

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.

2
Test it using localhost (127.0.0.1), which is your own computer. You can use Wireshark to check network activity. UDP is part of TCP/IP protocol stack. - m0skit0
What error you are getting on server. I don't think you need to use connect() for UDP sockets.. why can't you use just send(packet).. - nullptr
if your scene is with NAT, the remote server is behind router/nat (with private IP -10.*/172.xx.*/192.168.*) and you don't have port-forwarded the UDP-port.... can't work. - ggrandes
Calling isConnected() and ignoring the result is pointless. It's pointless anyway. If you weren't connected, connect() would have thrown an exception. - user207421

2 Answers

1
votes

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.

0
votes

You need to pass the following variable to your JRE

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