Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Understanding NAT and UDP

HI,
 If i send a UDP packet from my computer A which is behind a NAT to a computer B that is outside my NAT. Will B be able to send a response back to A which is behind NAT. How will things work exactly in this case.
Also any articles or steps i can take to see this in action like making a program to send a udp packet from my comp to another and then receiving a reply (probably in java)?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

HI,
I tried making some actual code to send a UDP and TCP packets and see here are the codes :
Using UDP :
import java.io.IOException;
import java.net.*;

public class UDP {
    public static void main(String[] args) throws IOException {

        DatagramSocket socket = new DatagramSocket(0);
        socket.setSoTimeout(100000);
        InetAddress host = InetAddress.getByName("time.nist.gov");
        DatagramPacket request = new DatagramPacket("A".getBytes(), 1, host, 13);
        byte[] data = new byte[1024];
        DatagramPacket response = new DatagramPacket(data, data.length);
        socket.send(request);
        System.out.println("packet sent");
        socket.receive(response);
        String daytime = new String(response.getData(), 0, response.getLength(), "US-ASCII");
        System.out.println(daytime);
    }
}

Open in new window


Using TCP :
import java.net.*; import java.io.*;
public class TCP {
    public static void main(String[] args) {
        String hostname = args.length > 0 ? args[0] : "time.nist.gov"; Socket socket = null;
        try {
            socket = new Socket(hostname, 13); socket.setSoTimeout(15000);
            InputStream in = socket.getInputStream();
            StringBuilder time = new StringBuilder();
            InputStreamReader reader = new InputStreamReader(in, "ASCII"); for (int c = reader.read(); c != -1; c = reader.read()) {
                time.append((char) c); }
            System.out.println(time); } catch (IOException ex) {
            System.err.println(ex); } finally {
            if (socket != null) { try {
                socket.close();
            } catch (IOException ex) {
            } }
        } }
}

Open in new window


Interestingly the TCP one does get the response whereas the UDP code i dont see any response on my home network (i am capturing packets in wireshark parallely)
while on my office network it also didnt returned any response but i saw an error packet in wireshark stating
Destination Unreachable (Port Unreachable)

Any comment on this behavior
I don't think you should in any way rely on what happens when you make a connection to a time server and do what seems to me to be entirely arbitrary comms with it to answer your wider question. All you've done there is create additional problems
i just wrote this small prog to find out what will happen during plaing udp packet sending...as my computer is behind nat it seems the server is unable to send me data.
want to see nat and udp in action..
Did you forward the port from WAN to your machine? You have to as otherwise it will be discarded
Do you mean that i have to make a port forwarding rule on my router ? But why that should be required.
As TCP packet is getting the response. Also i am connecting to port 13 and the reply will be received on some random port number as i have used        DatagramSocket socket = new DatagramSocket(0);
Also does the above code for UDP works on your system ? I have taken this example from java network programming book by orielly. And the author seems to be having no problem in getting reponse with UDP packets same as in TCP packets