antorrr
antorrr

Reputation: 21

C socket TCP server fails after client disconnects

I am modifying the server code from here http://www.linuxhowtos.org/C_C++/socket.htm. I want to reset the server when the client disconnects, so it's ready the next time for the client. But instead when I close the client side, the server shuts down and nothing is written in the server log. Since I only want to handle one client at a time, I don't need to fork right? Otherwise the server works fine and responds to messages I've sent from a client I wrote in C#.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

FILE *fp;

double square(double x)
{
     return x*x;
}

void error(const char *msg)
{
     fprintf(fp, "%s", msg);
     fprintf(fp, "%s", "\n");
}

int main(int argc, char *argv[])
{
     fp = fopen("serverlog.txt", "a");

     int sockfd;
     int newsockfd;
     int portno = 43635;
     int clientIsConnected;

     socklen_t clilen;
     char buffer[256];

     struct sockaddr_in serv_addr, cli_addr;
     int n;

while (1)
{
     sockfd = socket(AF_INET, SOCK_STREAM, 0);

     if (sockfd < 0)
     { 
          error("ERROR opening socket");
          close(sockfd);
          return 0;
     }

     bzero((char *) &serv_addr, sizeof(serv_addr));

     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
     {
          error("ERROR on binding");
          close(sockfd);
          return 0;
     }

     if (listen(sockfd,5) < 0)
     {
          error("ERROR on listen");
          close(sockfd);
          return 0;
     }

     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

     if (newsockfd < 0) 
     {
          error("ERROR on accept");
          close(newsockfd);
          close(sockfd);
          return 0;
     }

     clientIsConnected = 1;

     while (clientIsConnected)
     {
          bzero(buffer, 256);
          n = read(newsockfd, buffer, 255);

          if (n < 0)
          {
               error("ERROR reading from socket");
               clientIsConnected = 0;
               break;
          }

          double x = atof(buffer);
          double squared = square(x); 
          char response[256];
          sprintf(response, "%f", squared);

          n = write(newsockfd, response, strlen(response));

          if (n < 0)
          { 
               error("ERROR writing to socket");
               clientIsConnected = 0;
               break;
          }
     }
}

     close(sockfd);
     return 0; 
}

Upvotes: 0

Views: 340

Answers (1)

Florin Petriuc
Florin Petriuc

Reputation: 1176

It may be that your server stops because it received SIGPIPE. You can use

signal(SIGPIPE, SIG_IGN);

to keep your program going and you can handle the closed communication in your application code.

Upvotes: 2

Related Questions