Just want to clarify something...

Started by
4 comments, last by Anon Mike 18 years, 7 months ago
I am just starting to learn networking. Like all my other coding so far, I'm learning on my own through my own projects. I promise I won't bother you with questions about the M-word. Anyways, I just wanted to check something. If there were several people connecting to, say, a chat server, then could you identify them each individually by their IP address, no matter what? Let's ignore hackers for a moment. What I wonder is this: Say you had several computers sharing a single internet connection, connected through a router. Now, when you install the router, you assign it an IP address. So my question is, does it broadcast all the computers using it as having the same IP address? I assume not, since that would make it difficult to play MMO games from two computers on the same router, but I want to check. If so, then how do individual computers get identified uniquely? How much info is there to keep track of for each individual person signed onto a server, in terms of unique connection identification? Thanks for your time.
my siteGenius is 1% inspiration and 99% perspiration
Advertisement
All computers behind a router have the same IP address. If your using sockets then you can use them to identify the computers.
OK. Sorry, obviously I am very new to this. So sockets are kind of like "holes" in your firewall/router that allow you to more or less directly connect to a server of some sort, right?

For example, I am currently logged in on this site. So there is a socket open in my router and firewall that allows my computer to connect directly to gamedev.net? And is there a single socket on the gamedev server that everybody connects to, or is one opened per client?

I'm sure I could figure out this stuff by reading more, but I just wanted to get these couple of points cleared up before I started.

Thanks again.
my siteGenius is 1% inspiration and 99% perspiration
A socket is like a connection. When you try to connect to gamedev a socket is created. That socket then connects to the servers socket which creates a new socket (One socket to listen for connections and one socker per connection). Then the sockets comunicate and then close.

Any tutorial on sockets will give this info and more.
Actually, HTTP connections only usually exist until the data has been transferred, then they're closed. But never mind.
When you connect to gamedev.net, your PC opens a socket, and connects it to gamedev.net. At the GDNet server, it has one socket listening for connections, and when someone connects to it, it'll create a new socket. So at this point, if you're the only one connected, the server has a socket listening for connections, and one for transferring data to you. Your PC has one socket open, which is connected to the one on the GDNet server.

If you have two PCs behind a router, or any other NAT (Network Address Translation) device, then the server will see the two clients as having the same IP address, but they'll have different ports, and will have different sockets. When data is sent from the server to the client, it'll get sent to your IP address, and will be picked up by your router. The router has a NAT table of where to send data destined for a certain port, so it knows to send it to the correct machine on the network.
For example, if two PCs connect to a server, and PC 1 uses port 10000 on the router, and PC 2 uses port 10001 on the router, then when data comes from the server for PC 1, it'll be sent to your IP on port 10000. Your router will see that port 10000 goes to PC 1, so it'll forward the data to there.

This is why you can't host a server behind a router without telling the router to forward the port first. If someone tries to connect to your IP and you have a router, the router won't know where to send the data. It can't really send it to everyone on the network, because that would cause chaos. So it just drops the packet.
Quote:Original post by mike25025
All computers behind a router have the same IP address.


It's not quite that simple and it depends on exactly what you mean when you say "router".

First off, each computer actually has it's own unique (amongst themselves) IP address - if they didn't then there would be no way for them to talk to each other, nevermind going through the router.

When you say "router" you probably actually mean a NAT (network address translation). A NAT is used when your ISP only gives you one IP address yet you have to have a bunch of computers share it. What the NAT does is translate your machine's IP address into the one ISP has given you and back again creating a sort of virtual connection. This is what Evil Steve is talking about. Computers outside NAT will see a single IP address (with different ports). Inside the NAT they all use thier own addresses.

Other common usages of "router" are to mean a firewall - basically a layer of extra security and policy between two networks that may or may not also have NAT services. A switch - a piece of hardware with multiple network interfaces and enough smarts to efficiently choose which interface to use. And occasionally a hub - which is basically a really dumb switch that just rebroadcasts everything to all interfaces.
-Mike

This topic is closed to new replies.

Advertisement