user3059427
user3059427

Reputation: 209

how does server find out my ipaddress behind a router?

I have a question regarding how server can send udp packet to my laptop successfully when my laptop is behind a router and the server is using my external ip address. I was trying out udp client and server code. The code that i have used is in the following link.

http://systembash.com/content/a-simple-java-udp-server-and-udp-client/

Here is what i did.

First, i uploaded the server to a remote host.

import java.io.*;
import java.net.*;

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
      }
}

Then, i ran this client code from my laptop and it worked as suggested.

import java.io.*;
import java.net.*;

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}

But, my confusion is this.

In the following section in the server code

              DatagramPacket sendPacket =
              new DatagramPacket(sendData, sendData.length, IPAddress, port);
              serverSocket.send(sendPacket);

The server uses my external ipaddress. This request becomes successful since the client receives the sent packet. How is it possible since my laptop is behind a router?

To test if i can have the client send request to a server sitting in my laptop to check if it is really possible to send udp packets on machines behind a router, I put the server code in my laptop and then ran the client code in a remote machine. In the client code i updated the ipaddress to my external ipaddress which i got from printing the ipaddress of the received packet in the server code.

              InetAddress IPAddress = receivePacket.getAddress();
              System.out.println(IPAddress)
              int port = receivePacket.getPort();
              String capitalizedSentence = sentence.toUpperCase();
              sendData = capitalizedSentence.getBytes();
              DatagramPacket sendPacket =
              new DatagramPacket(sendData, sendData.length, IPAddress, port);
              serverSocket.send(sendPacket);

But, this approach doesn't work i.e the client at the remote machine cannot find the ipaddress of laptop which is expected.

But, how did the server send the request udp packet to my laptop successfully is my question? Thanks!

Upvotes: 0

Views: 369

Answers (1)

Usually, the server doesn't know your internal IP address, and it couldn't send you a packet there even if it did. What happens is that the router remembers that you sent a packet with a certain source port and forwards any replies back to you.

Upvotes: 4

Related Questions