Eko
Eko

Reputation: 1557

Multiple processes listening on a same socket

Is it possible for multiple processes to listen on a same socket?

For example I have 5 clients, and one process acting as a server writes on a socket and all the 5 clients receive the same mesage.

Upvotes: 0

Views: 3113

Answers (1)

Ankit Tripathi
Ankit Tripathi

Reputation: 384

Yup it is very much possible for multiple processes listening on same port by using fork ().

In fact most of the web servers use this to enhance the performance when there is a requirement to receive message from huge number of clients.

Just create the listener & fork the processes. Now all the processes will be listening on the same port. Now how they divide the incoming requests among themselves totally depends upon the OS like in Solaris the requests are divided among the processes on round-robin basis.

But for your scenario you can use socket multiplexing (select ()) to attain the required result.

Upvotes: 2

Related Questions