gpt4 book ai didi

c - 服务器在处理完一个请求后立即终止

转载 作者:行者123 更新时间:2023-11-30 15:17:28 25 4
gpt4 key购买 nike

我编写这个服务器程序是为了一次接受一个客户端的请求,服务该请求,然后等待进一步的请求,外部 while 循环应该继续运行,但程序在恰好一个请求后终止

#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/signal.h>

#define SUCCESS 0
#define ERROR 1
#define END_LINE 0x0
#define SERVER_PORT 1306
#define MAX_MSG 100

static int words = 0;
static int letters = 0;
static int delim = 0;


int
main (int argc, char *argv[])
{

int i, serv_sd, cli_sd, cliLen;
struct sockaddr_in cliAddr, servAddr;
char line[MAX_MSG], buffer[MAX_MSG];


serv_sd = socket (AF_INET, SOCK_STREAM, 0);
if (serv_sd < 0)
{
perror ("cannot open socket ");
return ERROR;
}

servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr ("127.0.0.1");
/* servAddr.sin_addr.s_addr = htonl(INADDR_ANY); */
servAddr.sin_port = htons (SERVER_PORT);
printf (" Socket Id is %d, Server port is %d\n", serv_sd, SERVER_PORT);

if (bind (serv_sd, (struct sockaddr *) &servAddr, sizeof (servAddr)) < 0)
{
perror ("cannot bind port ");
return ERROR;
}
listen (serv_sd, 5);

while (1)
{

printf ("%s :: waiting for data on port TCP %u\n", argv[0],
SERVER_PORT);

cliLen = sizeof (cliAddr);
cli_sd = accept (serv_sd, (struct sockaddr *) &cliAddr, &cliLen);
if (cli_sd < 0)
{
perror ("cannot accept connection ");
return ERROR;
}

memset (line, 0x0, MAX_MSG);

while (recv(cli_sd, line, MAX_MSG, 0) != -1)
{

printf ("\n %s:: Received from %s: TCP port %d << %s\n", argv[0],
inet_ntoa (cliAddr.sin_addr),
ntohs (cliAddr.sin_port), line);
words++;
int line_size = strlen (line);
for (i = 0; i < line_size; i++)
{
if (isalnum (line[i]) != 0)
letters++;
else
delim++;
}
printf("\nwords: %d, letters: %d\n,",words,letters);
send(cli_sd, &words, 4, 0);
send(cli_sd, &letters, 4, 0);
send(cli_sd, &delim, 4, 0);
memset (line, 0x0, MAX_MSG);

}//receiver


}//while
}

客户端

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>

#define MAX 100


int
main (int argc, int *argv[])
{
int sock_desc, rc, i, serv_port, letters, words, delim;
struct sockaddr_in localAddr, servAddr;

if (argc < 4)
{
printf
("usage: %s <server IP> <Server Port> <data1> <data2> ... <dataN>\n",
argv[0]);
exit (1);
}

servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr (argv[1]);
serv_port = atoi (argv[2]);
servAddr.sin_port = htons (serv_port);

sock_desc = socket (AF_INET, SOCK_STREAM, 0);
if (sock_desc < 0)
{
perror ("cannot open socket ");
exit (1);
}

rc = connect (sock_desc, (struct sockaddr *) &servAddr, sizeof (servAddr));
if (rc < 0)
{
perror ("cannot connect ");
exit (1);
}

for (i = 3; i < argc; i++)
{
rc = send (sock_desc, argv[i], strlen (argv[i]) + 1, 0);
if (rc < 0)
{
perror ("Connection lost! ");
exit (1);
}

// printf ("\n %s::Data %d Sent--> %s", argv[0], i - 2, argv[i]);
recv(sock_desc, &words, 4, 0);
recv(sock_desc, &letters, 4, 0);
recv(sock_desc, &delim, 4, 0);
}

printf ("\n");

// close(sock_desc);

printf("\n\nThe message you sent had %d letters, %d words and %d delimiter.\n\n",letters,words,delim+words);

return 0;

}

最佳答案

您的服务器未正确检查客户端是否关闭。如果对方关闭连接,recv将返回0。因此,当您尝试调用 send 时,服务器进程会收到 SIGPIPE 信号。由于您没有该信号的信号处理程序,因此该进程将退出。

您需要保存recv的返回值并检查它是-1(错误)还是0(关闭)。 send 的返回值也应该被检查。另外,当内部循环退出时,请务必关闭接受的套接字。

 #include <signal.h>
....
int rval;
...
signal(SIGPIPE,SIG_IGN); // ignore SIGPIPE
...
while ((rval=recv(cli_sd, line, MAX_MSG, 0)) != 0)
{
if (rval == -1) {
perror("recv failed");
break;
}

printf ("\n %s:: Received from %s: TCP port %d << %s\n", argv[0],
inet_ntoa (cliAddr.sin_addr),
ntohs (cliAddr.sin_port), line);
words++;
int line_size = strlen (line);
for (i = 0; i < line_size; i++)
{
if (isalnum (line[i]) != 0)
letters++;
else
delim++;
}
printf("\nwords: %d, letters: %d\n,",words,letters);
if (send(cli_sd, &words, 4, 0) == -1) {
perror("send 1 failed");
break;
}
if (send(cli_sd, &letters, 4, 0) == -1) {
perror("send 1 failed");
break;
}
if (send(cli_sd, &delim, 4, 0) == -1) {
perror("send 1 failed");
break;
}
memset (line, 0x0, MAX_MSG);

}//receiver
close(cli_sd);

关于c - 服务器在处理完一个请求后立即终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32450937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com