-1
有人可以向我解釋這段代碼的粗體部分在做什麼?C聊天客戶端? (C網絡編程)
while (1)
{
**FD_ZERO(&readfds);
FD_SET(0, &readfds); /* add stdin */
FD_SET(sock, &readfds);**
/* BLOCK on select() */
**select(FD_SETSIZE, &readfds, NULL, NULL, NULL);**
**if (FD_ISSET(0, &readfds))**
{
char msg[1024];
scanf("%[^\n]", msg); /* read everything up to the '\n' */
getchar(); /* read (skip) the '\n' character */
/* write the message to the socket connection */
int n = write(sock, msg, strlen(msg));
if (n < strlen(msg))
{
perror("write() failed");
return EXIT_FAILURE;
}
}
**if (FD_ISSET(sock, &readfds))**
{
char buffer[1024];
int n = read(sock, buffer, 1024);
if (n < 1)
{
perror("read() failed");
}
else
{
buffer[n] = '\0';
printf("Rcvd msg from server: %s", buffer);
}
}
}
大膽的沒有顯示,但你可以看到他們在雙星號。有四個部分。 – Aerovistae 2012-04-11 06:31:50
你讀過「man select」嗎? – Mat 2012-04-11 06:33:11
好吧,打那個。雖然我不完全理解它,我有一個想法。 – Aerovistae 2012-04-11 06:33:36