Advertisement

how many can connect at 1 port?

Started by February 27, 2004 01:46 PM
9 comments, last by _nomad_ 20 years, 11 months ago
let''s say computer number 1 is a server that accepts application at port 5555. it is multithreaded and creates a separate thread for each connection. how many clients can connect to it? can i have 2 or more clients simultaneously connecting at 5555? how will the server know which data going through port 5555 is to which thread? thanks.
Assuming you''re talking about TCP ports, then when a client connects to the server and the server does an accept(), the server allocates a new local port for communication with that client. The client will then send all future packets to the new port, not the port that the server is listening for new connections on. The total number of open connections that a server can run is operating system/protocol stack implementation dependent. In theory it''s limited to a maximum of 65535, which would be the number of valid ports. In practice it will be much smaller than that.
Advertisement
i see...so if i know the new local port being used, can i connect to it directly after the socket disconnects (about a minute or two after it disconnects)? as it seems that the port is free...is it correct to assume this?

thanks!
The new port assigned for the connection on the server will not have a listen() called on it, so trying to connect to it should give a connection refused error. You should only try to connect to the port that the server is actually listening on.
i see. ok thanks!

btw, since the theoretical limit is about 65,000 ports, does it mean i can''t have more than that number of simultaneous incoming connections?

thanks.
The maximum number of simulataneous incoming, unaccepted conncections is dependent on the protocol stack implementation. Don''t expect it to be a very large number.
Advertisement
NOw i gotta ask, that doesnt apply to udp right? it doesnt ask any qyestions, it kinda just takes the data, so theres no real limit... right? and also, what would happen if two packets arrived at the same time?
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
UDP has no concept of connections, so, of course, almost of all this thread has no bearing on UDP.

There is, in addition to whatever physical bandwidth limitations exist, a protocol implementation defined limit to the amount of unreceived data buffered by the protocol stack. If two packets arrive simultaneously, hopefully both will get placed in the buffer to be read by the application. If a large number of packets arrive before the application reads the packets, some of those packets will be simply dropped.
> that doesnt apply to udp right?

You simply receive the packet and you extract both the data and the sender info via ''recvfrom()''.

> what would happen if two packets arrived at the same time?

That''s a hardware issue; there is nothing you can do in software. With UDP you''ll get lost packets and you would have to build some reliability layer atop your comm code.

> since the theoretical limit is about 65,000 ports

The TCP stack is more than just 65000 integers. Each connection has input and output buffers, state variables, etc. So each socket eats a fair amount of memory; to lower memory consumption you need multiplexing. And threads are not the ideal solution for that because each thread needs stack space and other overhead, not to mention task/thread switching overhead. That''s why modern servers utilize advanced overlapped I/O strategies such as IOCP on Windows and AIO on unix.

-cb
ok thanks guys!

This topic is closed to new replies.

Advertisement