我写了这个:
客户:
printf("\nType a string: (STOP to ending connection)\n\n");
do{
while ((c = getchar ()) != '\n' && i < MAXLENGHT){
buf[i++] = c;
}
buf[i] = '\0';
len = strlen(buf);
}
while( strcmp(buf,"STOP")!=0);
close(sockfd);
exit(0);
}
服务器:
int main (int argc, char **argv) {
int sock, fd;
socklen_t client_len;
struct sockaddr_in server, client;
if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
perror ("Socket failed");
exit (1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons (SERVER_PORT);
if (bind ( sock, (struct sockaddr *) &server, sizeof server) == -1) {
perror ("bind failed");
exit (2);
}
listen (sock,1);
while (1) {
client_len = sizeof (client);
if ((fd = accept ( sock, (struct sockaddr *) &client, &client_len)) < 0) {
perror ("accepting connection");
exit(3);
}
fprintf (stderr, "\nOpen.\n");
send (fd, "\nWelcome!\n", 50, 0);
check_stats (fd, fd);}
close(fd);
fprintf (stderr, "\nClose\n");
exit (0);
}
.
void check_stats(int in, int out)
是一个函数,服务器接收字符串,做一些事情,然后将它发送回客户端。我的问题是:在服务器的代码中,我必须先放什么
close(fd)
当用户键入“STOP”时,服务器结束,但在结束之前,它向客户端发送另一个 BYE 字符串?我遇到了问题
recv(int sockfd, void *buf, size_t len, int flags);
功能。我必须把主要内容作为
int sockfd
?