C # - Multiple TCP connections on one port?

What interests me, I am allowed to do this

client_db.clients[numberOfClients].sock = listener.Accept(); 

For my network class, I am writing a chat client and the server to which it connects. I have an array of client objects that contains various information about each of the people connecting to the server. Before contacting one client, I will have a socket equal to the listener. Accept, and then I will do all my things with this socket. I incorrectly assumed that I could use an array of sockets to have multiple TCP connections with multiple clients.

Is there any way to do this? I understand that there are probably more efficient ways to do this, but I'm still getting network programming hanging up, and more importantly, my server is currently based on the idea of ​​me using an array of sockets. If there is no way to do this, this is definitely a lesson that I will remember.

EDIT: I had the impression that it didn’t work because I got an exception saying that “cannot have multiple connections”, although I cannot get this exception again. Now I get an object error. I'm confused, I need to look into this a little more.

+6
c # sockets tcp
source share
1 answer

With TCP, you can only have one process listening on a port, but when it accepts a connection (like yours), you get a full socket descriptor to continue the session so you can return and listen to the original descriptor descriptor for another connection.

Uniqueness in TCP is at the session level. In other words, the 5-tuple (source-ip, source-port, dest-ip, dest-port, protocol) must be unique so that packets do not get confused about where they are going.

You can have thousands of clients talking to this dest-ip / dest-port pair (for example, a large number of people who click stackoverflow.com:80 right now).

So yes, you are allowed to do what you do.

What can you find if you try to bind this port while there are still sessions in the TIME_WAIT state, you will not be able to bind. This means that any live packets from a previous session on the network will not log in and will not ruin your session.

Further information on TIME_WAIT and why this is necessary can be found in this excellent answer :-)

+32
source share

All Articles