using select() to listen to multiple clients (TCP)
I have developed (TCP) server to listen to a client and interact with it. Now I'm trying to adapt that server code to listen to mulitple clients. I am wanting to use select, but I'm getting confused with some of the examples and explainations I've found.
I have been reading: http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/select.htm and http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/select.htm both of which have been recommended to me.. :S
The first site's example seems less complex.. (though still don't know how to adapt it to my code as I am very very very new to network etc.) but I worry that I'm missing out on key aspects due to the complexity of the second site's example.
Below is kind of a snapshot of my server code when only listening for one client (some psuedocode included to minimize not so important stuff):
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[3];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){error("ERROR opening socket");}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{error("ERROR on binding");}
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0){error("ERROR on accept");}
while (UNTIL END OF FILE)
{
<DO SOME FILE READING STUFF>
n = write(newsockfd, "test/n", 5);
if (n < 0){error("ERROR writing to socket");}
bzero(buffer,3);
n = read(newsockfd,buffer,3);
if (n < 0){error("ERROR reading from socket");}
buffer[n] = 0;
<DO SOME STRING STUFF>
while(done != 1)
{
bzero(buffer,3);
n = read(newsockfd,buffer,3);
if (n < 0){error("ERROR reading from socket");}
buffer[n] = 0;
if(strcmp(buffer, "CO"))
{
done = 1;
}
}
done = 0;
}
<DO STUFF>
n = write(ne开发者_开发百科wsockfd, "DN\n", 2);
if (n < 0){error("ERROR writing to socket");}
close(sockfd);
close(newsockfd);
return 0;
}
Which site's example for select() would work best for my what I'm trying to do (change the server code to listen to multiple clients)? Could someone explain select() in some more simplier terms for me? (since I'm so new to this and all...)
Thanks!
You need to wrap a loop around the accept()
and following code, so that you can accept more than one connection over the life of the program. You then need to decide how your server is going to handle multiple connections. You have a few choices:
- Use multiple threads, one thread per client.
- Use a forking server, one process per client.
- Use a single server, which will use
select()
to decide which file descriptor(s) are ready.
In the first two cases, the child thread or process is single-mindedly dealing with one client; it will wait while the client is working out how to respond to what it sends, which means that other threads or processes get a turn at the CPU. There are variations on the theme that prelaunch some number of worker threads or processes and arrange for those to pick up the workload, but that's more complex.
In the last case, you'll set up an array of the file descriptors you want select()
to work on, and then pass a copy of that list to select()
(since it tramples all over it). When you've got work to do (because select()
returns), you perform an accept()
on the file descriptor that you're listening on if that's ready, or you read from the already opened socket descriptors if they're ready.
精彩评论