Reputation: 181
I am trying to connect between 2 different machines using Java Socket. The TCP Ports on the Server and Client Sockets are different as below,
ServerSocket sourceSocket = new ServerSocket(8080, 10, InetAddress.getByName("192.168.1.2"));
On the same machine I also run the client Socket
destSocket = new Socket(InetAddress.getByName("192.168.1.3"), 49984);
Is it possible to send/ receive packets/ data between port 8080 and 49984 ion the above case between 2 different machines. Does this require NAT?
Upvotes: 0
Views: 977
Reputation: 1095
Sending packets between two ports on the same machine does not require any sort of NAT, or really any network - you don't have to be connected to the Internet at all. By opening two sockets on the same machine, on different ports, you can send data between those two sockets with no further configuration.
In order to send to a different machine (call it machine #2) from machine #1, you must have a socket opened on machine #2 (by running code on the other machine) that is ready to accept incoming data from the socket on machine #1. Additionally, the client socket must be sending data to the correct IP and port - so, when you open the port using new Socket
, you define the address of the socket, but not where it will send data.
So, while those commands are more or less correct, one must be run on machine #1, and one run on machine #2, in order to send data between the two. You must also make sure that the client is sending data to the server's address, and not itself - opening a port does not define where data will be sent to, only from.
Upvotes: 2