tags:

views:

44

answers:

2

The Tcp server need to serve many clients, If one client one server port and one server thread to listen the port, I want to know weather it is faster ?

If one port is good, could someone explain the different between one port and multiple ports in this case, thanks!

+1  A: 

Generally speaking* the server will allocate it's own outgoing sockets for each connected client (and you don't need to be aware of those numbers). Each client connection handle will hold it's port references.

When defining the servers connection port, you will allocate a socket for incoming clients to connect to. There is no performance benefit using multiple sockets, in fact allocating additional sockets will show a performance hit (although for each port it will be tiny.)

update...

By the way, assigning multiple incoming ports (particularly a large range) for clients to connect to is also insane. From the perspective of making the service usable and maintainable.

slomojo
thanks! I think there is a buffer for one port to save the client request(if there are too many requests).
why
+1  A: 

The problem with using multiple ports to achieve this is that each of your clients will each have a specific port number. Depending on the number of clients there could be a tremendous amount of bookkeeping involved.

Typically, for a tcp server that is to serve multiple clients, you have a "main" thread which listens to a port and accepts connections on that port. That thread then passes the connected socket off to another thread for processing and goes back to listening.

For a wealth of Unix network programming knowledge check out "The Stevens Book"

nathan
thank you, good book!
why
+1 for The Stevens Book
slomojo