2

Is it possible to reuse a listening TCP port in linux in different processes ?

for eg : process A and process B are listening on server=INADDR_ANY and port=10000

if its impossible then how the apache http server is able to listen to port #80 in different process.

Thanks in advance

sehe
  • 374,641
  • 47
  • 450
  • 633
suhail
  • 21
  • 1
  • 2
  • 1
    Why would you need that? – weekens May 11 '11 at 07:54
  • bcoz i want the separte server process listening on the same port at same time ? is there any way we can achieve this – suhail May 11 '11 at 12:45
  • well, that's an interesting question. Don't know about such possibility. But what's the exact intent? Load balancing? Reserving? (If not a secret, of course). – weekens May 12 '11 at 06:54
  • its both load balancing and reservation , no much support from linux in doing so – suhail May 12 '11 at 09:45

3 Answers3

1

It is possible to have multiple processes listening on the same port, but as you've seen you cannot do this using distinct sockets, with or without SO_REUSEADDR.

The only way to arrange this is to create the socket, bind() it and call listen(), then duplicate it. You can duplicate the socket by:

  • calling fork() - both child processes will inherit the listening socket. This is what Apache does; or
  • sending a handle to the listening socket over a UNIX-domain socket connection using a SCM_RIGHTS message.
caf
  • 233,326
  • 40
  • 323
  • 462
0

Apache spawns subprocesses, which inherit file descriptors. But these subprocesses are not actually listening the server port (80), but performing interaction with the clients on different ports, opened after a connection is established.

2 processes cannot listen 1 TCP port. How would they share the incoming data?

weekens
  • 8,064
  • 6
  • 45
  • 62
  • They don't share, each receives different clients with accept(). And if they shared an opened socket they could use a shared semaphore or any other synchronization mechanism. – Arkaitz Jimenez May 11 '11 at 07:39
  • weekens, we can process the message if linux allows to listen on same address and port in different machines . as REUSEPORT options allows in MAC and Solaris – suhail May 11 '11 at 07:40
0

2 processes can listen on the same port. They would both be calling accept and only one would get accept returned with the first client.

You can open the port, spawn 100 children and have them accept(socket) all at the same time, and they will be receiving equally the new connections, one each, never the same obviously.

If you want to do it from non-related processes, the SO_REUSEPORT flag allows multiple processes to bind to the same address provided all of them use the SO_REUSEPORT option.

link

Community
  • 1
  • 1
Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105
  • Arkaitz , i tried SO_REUSEADDR but it gives "bind : already in use " error. may i know if i am doing something basic mistake – suhail May 11 '11 at 07:47
  • Have you tried SO_REUSEPORT? Apparently if all your binds are with sockets with SO_REUSEPORT you might be able to do it. – Arkaitz Jimenez May 11 '11 at 07:55