服务器在处理完一个请求后完全终止

时间:2015-09-08 06:44:19

标签: c sockets server

我写了这个服务器程序,一次接受来自一个客户端的请求,提供请求,然后等待进一步的请求,外部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;

}

1 个答案:

答案 0 :(得分:1)

您的服务器未正确检查客户端是否关闭。如果对方关闭连接,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);