How Can I ping or create a scoket connection with my friend?

234 views Asked by At

When we ping to any Public IP of a router it give a reply, we dont get reply, if we ping a local computer with its private IP of a particular network, So is there a way so I can make direct connection or socket with a particular PC in a network. I have heard that, I have to make connection with public IP of a particular network and the port number will decide, on which PC in that network , my request will go. But this method is not working..

2

There are 2 answers

1
Warren Dew On

If the computer is behind a router that uses Network Address Translation (NAT) - that is, the computer has a private IP that is not visible to the internet - then you cannot ping or establish a connection to the computer from the outside. This is because the router does not know which computer you want since all the computers behind the router share the same public IP address.

The way to establish a connection in this case is to have the computer which is behind the router establish the connection to the outside computer. For example, when you go to a web site, you can do it from a computer behind a router since your computer is initiating the connection. However, the web server you are going to must have a public IP.

When a computer behind the router initiates a connection, the router does select a port number to associate with that connection so packets from the outside for that connection will go to the right computer. However, this only works for traffic on that particular connection; the port number is not a general purpose mapping to that computer.

Two computers behind the same router can establish connections with each other using the private IPs as they don't have to go through the router to do it.

0
AudioBubble On

You can decide with your friend who is going to be the client (the one initiation the TCP connection) and who is the server (the one receiving the TCP connection) and which port should be used (let's say X). Let's say that your friend is the server. Then he/she has to configure his/her router to redirect all the traffic to the router to port X to his/her private IP to port X (this is what Warren mentioned as NAT).

If you use a port above 1024, the server (running on your friend's PC) doesn't need root/administrator privileges.

Your program (the client) would then connect to the public IP address of your friend. He/she can check his/her public IP with: What is my IP