Results 1 to 4 of 4
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 ...
- 02-27-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 2
socket programming help
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);
}
- 02-27-2008 #2
If the server is receiving the SIGPIPE signal, that is because it is attempting to write to a client which has already closed the socket (perhaps because the client has exited).
The only way to avoid this, other than not doing the write to a closed socket, is to ignore (or handle) the SIGPIPE signal in your server code. This should be done before you fire up any threads.
But if you ignore the SIGPIPE signal, be sure to check for the EPIPE error on any write to a socket.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 02-27-2008 #3Just Joined!
- Join Date
- Feb 2008
- Posts
- 2
If so, I have a question.
The socket responsible for connection to a client is not performing any function at all (in the application), when the other side close the socket. The TCP socket is receiving or sending the "FIN" / "ACK" message (in network layer) only.
Does this event seem as write to a client?
Besides, can you tell me how I can ignore this signal in C programme as I am quite new to linux/C programming. Recommending a website or a book for this issue will be much appreciated too.
Thank you very much for your help.
- 02-27-2008 #4
I can see that your immediate question will be the first in a long series. :) This isn't a bad thing. But it does mean that you need to read a lot before continuing in this forum, and you've already suggested that.
The main reason is not that if you read books first it will save us time, although it will do that. The main reason is that if you read books, you will discover questions that you should be asking, but aren't yet. (We've all been in that position.)
So I recommend to you two Addison Wesley books.
On signals (and many, many other things): Advanced Programming in the UNIX Environment.
On network programming: UNIX Network Programming, volume 1.
Both of these by W. Richard Stevens of happy memory. These books are classics and almost infinitely useful. He's dead, but these books are very much alive.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote