c - write on closed socket doesn't generate sigpipe immediatly -
i've problem server/client on c. if close server socket after sigint, , seek write on closed socket client, i've write 2 times before client generates sigpipe. shouldn't generate immediatly? normal behaviour or need fix? code. i'm testing things on ubuntu, same pc, connecting via 127.0.0.1.
server.c
sigset_t set; struct sigaction sign; int sock_acc; int sock; void closesig(){ close(sock_acc); close(sock); exit(1); } int main(){ sigemptyset(&set); sigaddset(&set, sigint); sig.sa_sigaction = &closesig; sig.sa_flags = sa_siginfo; sig.sa_mask = set; sigaction(sigint, &sig, null); //other code take connection client sigprocmask(sig_unblock, &set, null); //write/read calls }
client.c
void closesigpipe(){ close(ds_sock); printf("stop..."); exit(1); } int main(){ sigpipe.sa_sigaction = &closesigpipe; sigpipe.sa_flags = sa_siginfo; sigaction(sigpipe, &sigpipe, null); //other code connect server, , write/read calls }
the problem when close server terminal ctrl+c, first write on socket client works without problem... perror("erorr:"); prints "success"...
the tcp protocol doesn't provide way receiver tell sender it's closing connection. when closes connection, sends fin
segment, means it's done sending, not can no longer receive.
the way sender detects connection has been closed tries send data, , receiver sends rst
segment in response. writing socket doesn't wait response, queues info , returns immediately. signal occurs when rst
received, short time later.
the reason have 2 writes may because of nagle's algorithm. avoid excessive network overhead, tcp tries combine short messages single segment. wikipedia page includes workarounds this.
c sockets signals sigpipe
No comments:
Post a Comment