hi..
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(newsockfd, "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!