Caleb Caleb - 2 months ago 18
Java Question

Should i use the same port numbers when sending data through UDP?

I just implemented a basic socket-based application, nothing special. For the sake of saving time, my server-client thing acts like a server and a client simultaneously.

When we send data (in this case) to a client/server, does this really matter to use the same port number?
My guess is no, because it doesn't matter which port you are using when sending data to. The only thing has to be kept, the port has to be any availabe one on the receiver machine, and if that receiver decides to send something back, he or she will have enough information about sender: his IP address, port number ect. As far as i know, a received packed provides with all of that info.

Below is just an illustration of what i've said above.

public class Server {

public static void main(String[] args) {
GameServer server = new GameSever(9822);
server.start();

InetAddress address = null;
int port = 7877;
try {
address = InetAddress.getByName("192.168.0.2");
} catch (UnknownHostException e) {
e.printStackTrace();
}
server.send(new byte[] { 1, 2, 3 }, address, port);
}
}

Answer

When we send data (in this case) to a client/server, does this really matter to use the same port number? My guess is no, because it doesn't matter which port you are using when sending data to.

Firstly, use the terms client and server distinguishly(as generally client initiates by sending the message, to which the server responds).

Next, the port which you're using is logically of no significance, the reason being server uses request.getPort() to determine the port while seding the response; (request is a DatagramPacket sent by the client).

Though you can hardcode the port at server(if known beforehand), but, it is a bad idea. What in those applications where you've no idea about who sent the datagram packet?

Java documentation by Oracle also uses an example of client-server where client's port number is left for the constructor to pick. It mentions :

Mostly, the client uses a constructor that does not require a port number. This constructor just binds the DatagramSocket to any available local port. It doesn't matter what port the client is bound to because the DatagramPackets contain the addressing information. The server gets the port number from the DatagramPackets and send its response to that port.


MORE INFO (taken from Java Network Programming) :

public DatagramSocket() throws SocketException

This constructor creates a socket that is bound to an anonymous port. For example:

DatagramSocket client = new DatagramSocket();

Pick this constructor for a client that initiates a conversation with a server. In this scenario, you don’t care what port the socket is bound to because the server will send its response to the port from which the datagram originated. Letting the system assign a port means that you don’t have to worry about finding an unused port. If, for some reason, you need to know the local port, you can find out with the getLocalPort() method.

NOTE : The same socket can receive the datagrams that a server sends back to it(underlying implementation).

Whereas, the below constructor creates a socket that listens for incoming datagrams on a particular port, specified by the port argument :

public DatagramSocket(int port) throws SocketException

Use this constructor to write a server that listens on a well-known port.

Comments