views:

141

answers:

4

I am trying to create a Socket "Hello World" program.

I would ideally like to create two programs who can exchange simple text, peer-to-peer.

I can create a webservice that the peers can post their IP Address to.

One peer can then select another to pair up with.

If one of the peers is my desktop computer, and the other is my laptop, how can I differentiate the two?

A: 

Run ipconfig on each one of them and find out what is their ip. You might also be able to ping them by name depending on how they are connected.

To identify either machine outside your network you need to pay money to your internet provider so they allocate a range of public IPs for you and then you expose each one as having those addresses. What you have at your home is probably a 192.168.1.x address range which is private.

Otávio Décio
Yes, I know I can get their local intranet IP by ipconfig, and pinging them over my intranet is no big deal. My question is how do I differentiate the two computers from OUTSIDE my own intranet. If I were writing a chat program, how would I send the chats to the correct computer, since they are both behind the same router, with the same IP?
Joshua
@Joshua-Modified my answer.
Otávio Décio
Then how do other services allow me to communicate peer-to-peer within my own intranet?
Joshua
A: 

You don't usually initiate the requests, the clients do. You get a request and respond to it. Presumably your request contains something like a "conversation Identifier" and you match up all the talkers with the same one.

No Refunds No Returns
So only the "server" (one of the peers arbitrarily) needs to have a fully resolvable IP address?
Joshua
Although I can involve a server at the beginning of the application, I need this to be truly peer-to-peer.
Joshua
still the same thing. You're going to need someone to arbitrate the communication. It's doubtful many corps are going to let individuals advertise themselves to the public internet populace.
No Refunds No Returns
A: 

I would set up port forwarding in the router so whatever port you connect to in the external IP will forward to the right computer in the internal network.

Ian Jacobs
I cannot expect my users to execute port-forwarding, as a requirement for using my socket program.
Joshua
+1  A: 

First of all. If all this is on your local network, without any NAT gateways or similar - it's no big deal. Your server will see your clients with different IP addresses, and the clients can connect directly to each other.

On the other hand, if your server runs on an internet reachable IP address, and your clients connect from wherever they can reach that server you will have to consider the rest of this post.

Client A connects to your server. There's now a connection between: (ClientAIP:ClientAPort,ServerIP:ServerPort)

Client B connects to to your server. There's now a connection between: (ClientBIP:ClientBPort,ServerIP:ServerPort)

If both your clients are behind the same NAT'ed gatway, the server will see ClientAIP == ClientBIP. But ClientAPort will be different from ClientBPort. And that's enough to to keeep up two connections to your server.

Now the trouble comes if you want to pair two of them. Have the clients send you its internal IP address(And perhaps its internal port too so that could be used for extra verification.)

You now know the clients (external) IP and their internal IP. These will be the scenarios:

  1. ClientAInternalIP == ClientAIP, ClientBinternalIP == ClientBIP. Great neither are behind a NAT gateway - select one of them to be a server and the other one to connect directly to it.

  2. ClientAInternalIP != ClientAIP, ClientBinternalIP == ClientBIP. ClientA is behind NAT, ClientB is not. Have ClientB be a server and ClientA connect diectly to ClientB

  3. ClientAInternalIP == ClientAIP, ClientBinternalIP != ClientBIP. Just the reverse case 2).

  4. ClientAInternalIP != ClientAIP, ClientBinternalIP != ClientBIP, but ClientAIP == ClientBIP . Special case - they're both behind a NAT gateway but they're on the same internal network. This is the same as case 1). Except the clients can communicate on their internal network instead of the internet.

  5. ClientAInternalIP != ClientAIP, ClientBinternalIP != ClientBIP. Both are behind NAT gatways. -You can't have the connect to eachother - atleast not without asking someone to set up port forwarding on one of the gateways at the client. You'll have to route the communication through your server. (Though there are routers/gateways providing APIs you might be able to use to programatically open port forwarding from a client site. It's not widely available and probably locked down anyways).

And even with the above scenarios there might be firewalls set up so clients can't peer directly even if their real IP address is routable on the internet - you might need to fall back to routing through your server anyway.

nos
Thank you! This got me started! I also learned about something called NAT Hole Punching, which was very useful for case 5.
Joshua