December 15, 2017, 05:58:38 PM

News : LinuxSolved.com Linux Help Community Forum..


Author Topic: Socket Programming Problem  (Read 1574 times)

Offline docfan

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

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");
   index=0;
   len=sizeof(tcp_addr[index]);

   for( ; ; )
   {
      bzero((char *) tcp_addr+index,sizeof(tcp_addr[index]));
      tcpid[index]=accept(sock_tcp, (struct sockaddr*) tcp_addr+index, &len);
      printaddr(&tcp_addr[index]);
      index++;
      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);
   }

   pthread_exit(NULL);
}

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