我正在创建一个小型客户端 - 服务器应用程序,其中服务器在接受后分叉子进程,在客户端连接到它时发送多个连接,发送消息并接收响应。以下是我的代码片段: - client.c
char buffer [256];
portno=5001;
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
/* Now connect to the server */
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR connecting");
exit(1);
}
/* Now ask for a message from the user, this message
* will be read by server
*/
while(1)
{
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
/* Now read server response */
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if(n==0)
{
perror("nothing to read");
}
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("%s\n",buffer);
}
- server.c
int main( int argc, char *argv[] ) {
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *)NULL,NULL);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
/* Create child process */
pid = fork();
if (pid < 0) {
perror("ERROR on fork");
exit(1);
}
if (pid == 0)
{
while(1)
{
/* This is the client process */
close(sockfd);
doprocessing(newsockfd);
/** exit(0); **/
}
}
else {
close(newsockfd);
}
} /* end of while */
}
void doprocessing (int sock) {
int n;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
if(n==0)
{
perror("nothing to read");
}
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
if (n <= 0) {
perror("ERROR writing to socket");
exit(1);
}
}
当我运行两者时,O / P跟随:
Please enter the message: arjun
I got your message
Please enter the message: gaur
ERROR reading from socket: Connection reset by peer
请帮助我们解决此问题
答案 0 :(得分:1)
会发生什么:
答案 1 :(得分:1)
分叉子进程正在退出而不关闭newsockfd
。在一些导致连接重置而不是有序关闭的平台上。
您还需要检查recv()
的结果为零并正确处理,即不是错误,并且您还需要正确使用正返回值,例如而不是
printf("%s\n",buffer);
应该是
printf("%.*s\n",n,buffer);
答案 2 :(得分:-1)
您的客户这样做:
但您的服务器执行此操作(可能同时使用多个连接):
因此服务器在客户端即将发送更多数据时关闭连接,并且客户端因服务器关闭连接而收到错误。
您需要让服务器继续在循环中接收和发送数据(因此它可以在同一连接上多次发生),或者让客户端在每次想要发送内容时建立新连接。