Reputation: 11
I am writing a C program where for every new request to be processed by server, I need to open a new TCP connection? That is every request from client needs to be handled by a separate TCP connection to a server listening on a particular port.
Can someone help me in code pointers?
Any code snippet will be highly useful?
Upvotes: 0
Views: 1772
Reputation: 409196
If you need to save state with the socket, put it in a struct
together with the data you need, and link the structures together as a list. For checking which sockets are ready see Maluchi's answer.
Upvotes: 0
Reputation: 1625
You can use the select()
function (assuming you are working with <sys/socket.h>
), "gives you the power to monitor several sockets at the same time. It'll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions" from http://beej.us/guide/bgnet/ (here you can download a pretty good book on network programming basics).
For a server example using select check http://beej.us/guide/bgnet/examples/selectserver.c
Hope it helps
Upvotes: 1