user3222214
user3222214

Reputation: 11

How to handle multiple socket client through server in c linux

I am trying to develop server to multi client application over Internet. in which clients are connected always (suppose 10). connected client summary will display on server side. from that client list i will select particular client and send some command to perform particular task. after that client will send me related output.

If any client disconnected due to some reason, client list should update.

I am trying it with using threading to connect multiple client. It works good only if i develop echo server. I tried to handle clients from server side, but due to infinite loop to accept multiple clients, i stuck in handling particular client and thread.

How can i solve this problem..... any good solution or idea that i can use? or am i using wrong logic?

its like IPC mechanism between different pc connected over internet.

Upvotes: 1

Views: 3097

Answers (3)

lfxgroove
lfxgroove

Reputation: 3908

It would seem that what you're doing now is that you're using blocking IO. With that you'll generally be able to handle just one client since every call to recv/accept/send will block until some data is received/sent. What you can do to work around this without the need for threads is to make these calls nonblocking. If you're into hacking some c you can change your current code to become nonblocking via a call to fcntl on the socket that is the server. With that done you can have a loop where you call accept() and send()/recv() after one another and they will return immediately. The difference is that they'll have a special return value if they didn't have any data for you. Check the man pages for accept etc to find these values. For more info you could check http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html which covers the most of sockets programming in linux. If you would like to that this a step further you can also take a look at select() which nouney mentions. It will give you the ability to have a set of sockets that you are connected to and it will tell you which ones you can recv/send from/to and if there are any new pending connections.

If you want to use the threading model you can still keep the sockets blocking, do note that this will pretty quickly start using resources if you create a new thread for each incoming connection which perhaps isn't what you would like to do. You'll also have to do some synchronization between the threads since i guess you'll want them to communicate with each other in some way aswell.

On the other hand if you don't want to go lowlevel you could use some of the alternatives that SirDarius suggested. Boost::ASIO seems to do the work well for c++ imo and doesn't require you to create any new threads iirc.

If you haven't done any socket programming before though i would recommend you to take a look at Beejs guide and create some basic functionality with it just for the learning experience.

Upvotes: 1

SirDarius
SirDarius

Reputation: 42889

Since you are using Linux, there are a few asynchronous frameworks (event loops) that you can use:

The general philosophy for these libraries is that they create an event loop, just like GUI frameworks, and it is up to the user to register callbacks to handle events related to network connections (accept, disconnect, data read, data write...) or I/O in general.

These libraries are efficient, usually based on the epoll api, and hide many implementation details so they are relatively easy to use.

Upvotes: 2

nouney
nouney

Reputation: 4411

Take a look at select(). It's portable and efficient with a low number of sockets. You can find an example here.

Upvotes: 1

Related Questions