我開發了(TCP)服務器來偵聽客戶端並與之交互。現在我試圖調整這個服務器代碼來收聽多個客戶端。我想要使用select,但是我對我發現的一些示例和解釋感到困惑。使用select()來偵聽多個客戶端(TCP)
我一直在讀:http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/select.htm和http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/select.htm這兩個國家都被推薦到我..:S
第一個站點的例子似乎不太複雜。(儘管仍然不知道如何將它適應我因爲我對網絡非常非常陌生等),但我擔心由於第二個站點示例的複雜性,我錯過了關鍵方面。
下面是那種我的服務器代碼的快照時只監聽一個客戶端(包括一些僞代碼,以儘量減少不那麼重要的東西):
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;
}
哪個網站對選)爲例(將工作最好爲我我想要做的(更改服務器代碼來聽多個客戶端)?有人可以用一些更簡單的術語解釋select()嗎? (因爲我對這個和所有這麼新......)
謝謝!