July 17, 2018, 07:26:42 PM

News : LinuxSolved.com Linux Help Community Forum..

Author Topic: Socket Programming Problem  (Read 1779 times)

Offline docfan

  • New Member
  • Posts: 1
Socket Programming Problem
« on: February 26, 2008, 06:38:44 PM »

I am programming a simple chat server in C.
I use asynchronous TCP socket to receive data from clients which are on Win32 system.

The problem is that when the application on Win32 is close or disconnect from server (by closesocket()),
the server will display a message "pollable event" and quit.

It seems that it receive SIGPIPE signal as I am searching on the Internet to figure out the reason.

Can anyone tell me how I can fix this problem so that the server will not close automatically?
Thank you very much
Here is my part of code.

void* TCPThread(void *arg)

   int tcpid[MAXUSER];
   int len;
   int index;
   int fileflags;
   struct sockaddr_in tcp_addr[MAXUSER];
   void sig_io(int sig);

   fprintf(stderr, "TCP thread Start\n");

   for( ; ; )
      bzero((char *) tcp_addr+index,sizeof(tcp_addr[index]));
      tcpid[index]=accept(sock_tcp, (struct sockaddr*) tcp_addr+index, &len);
      index= index & MAXUSER;

      fileflags  = fcntl(tcpid[index], F_GETFL );
      if (fileflags  == -1)
         perror("fcntl F_GETFL");

      if (fcntl(tcpid[index], F_SETFL, fileflags | FNDELAY | FASYNC) < 0)
         fprintf(stderr, "%d TCP Socket Asyn Error", index);
      if (fcntl(tcpid[index], F_SETOWN, getpid()) <0)
         fprintf(stderr, "Can't own SIGIO");

      signal(SIGIO, sig_io);


void sig_io(int sig)
   char msg[MAXMESG];
   recv(tcpid[0], msg, sizeof(msg), 0);
   fprintf(stderr, msg);