Client accidently close when the server crash

The steps to test the problem

  1. Open TCP Server
  2. Open TCP Client
  3. TCP Client sends data to Server.
  4. Close TCP Server and the client also crash without any notification

Second wonderful test:

  1. Comment the following statement in Client.c (at line 168) and compile it
Writen( sockfd, data.send_buf, strlen( data.send_buf ), __FILE__, __LINE__ );
  1. Run TCP Server
  2. Run TCP Client (since no writen(), so response from server, it is OK)
  3. Close TCP Server
  4. A lot of rubbish from TCP Server.........

I also posted the source code here.
Compile TCP Server

gcc -Wall -lmcheck -g -Werror -lpthread -o server server.c

Compile TCP Client

gcc -Wall -lmcheck -g -Werror -lpthread -o client Client.c lib/*.c

I'm glad that I won't end-up maintaining your code...

I played with it a bit, took me some time to get the client working without crash:

  • A Pthread_attr_init is missing for rx (Client.c:218)

  • Your client must be aware that a server may close the TCP connection brutally (RST segment). In this case, you'll get 0 on read() or SIGPIPE on the next write(). The way you usually get around this is to ignore SIGPIPE and watch for errno EPIPE when write() fails.

  • There is likely more problems with your code.

HTH, Lo�c

1 Like