several TCP sockets

m4l490n

Solid State Member
Messages
8
hi everybody

can I have several sockets using the same port in a computer (acting this as server)? Or Do I have to use one different port for every socket that I need?
 
generally you can't have two processes listening on the same port.

but you can have one process listening on a port that spawns multiple sockets. e.g. you generally start a process with only a few sockets, and spawn more sockets as the connection count goes up. after clients have finished talking to the server process you kill the sockets to keep resource use down.

I assume that you're writing some software? -if you're bothered about the socket connections
 
generally you can't have two processes listening on the same port.

but you can have one process listening on a port that spawns multiple sockets. e.g. you generally start a process with only a few sockets, and spawn more sockets as the connection count goes up. after clients have finished talking to the server process you kill the sockets to keep resource use down.

I assume that you're writing some software? -if you're bothered about the socket connections

Yes, well not directly, but I don't know what would be more efficient, to have multiple sockets opened at the same port or to have one individual port for every client
 
multiple sockets.

think of it this way.

The queue length on the TCP stack is constant, having more ports isn't going to mean you can accept more waiting connections. (e.g. there isn't a separate queue for each port, nor each socket). so you gain no benefit on this.

when you say efficiency, efficiency of what?
a single process could listen on hundreds of ports,
this is not efficient use of ports,

and since one one program can listen on a given port at any given time, it might lead you into trouble.
if for example you listen on ports 50 - 150, then you can't run a web server on the machine alongside your existing application as you're already using port 80. either the web server won't be able to bind to the port because your program started first, or your program won't be able to bind to the port because the web server is already using it.

it won't really make a difference to the CPU or memory load though.

if you assign a single socket to each port, you've got 100 open sockets,
if you use 1 port and assign 100 sockets to the port, you've got 100 open sockets.
 
Back
Top Bottom