Reputation: 35
I'm currently trying to create a client-server program in C Language, similar to a webmail. The thing is, I have managed to connect my server to the client and have it work properly, but now I'd like to introduce another server and create a triplet: client connects to one server, requests something and, if that request can't be fulfilled, server 1 sends it to server 2 through a socket, to see if that one can comply. I began by creating two sockets in server 1 - one for the client and one for server 2, with the same basic code, so it could listen on one port to clients and on another port to server 2, but as it turns out the 'accept' function is blocking, so once it connects to a client it doesn't move unless server 2 speaks out.
Not sure if I was clear enough, but any suggestion would be appreciated.
Thank you
Upvotes: 1
Views: 916
Reputation: 5557
You have several options to do what you want:
Basic multithreading: start a second thread, connect to server in thread2 and use blocking queues or something similar for communication between threads.
As Idan suggests: use I/O-Multiplexing. select() is the easiest to use imo and it will scale well enough up to ~1000 connections as far as I remember.
Split the logic in your architecture to provide one server, which is contacted to obtain data and run other servers (invisible to the clients) in separate processes (or machines even) to store data. In that case the first server queries the other servers for the data in parallel or sequentially. I would advise you to devise a strategy to distribute the data among the servers in this case to speed up queries.
Upvotes: 1
Reputation: 165
If you wish to avoid accept from blocking i would suggest that you will use select. it basically waits (with timeout) on the socket untill it has connection and only then runs the accept.
You can find some sample code in my sockets programming guide
Upvotes: 2