views:

136

answers:

2

I am having some trouble with this code. The problem is when i Request the Server to send me some data and the client just Disconnects when the server tries to send me data, the application Exists.

Here's the lines I think cause the problem

int SendBinary(int *byte, int length)
{
    int bytes_sent;

    bytes_sent = send(connecting_socket, byte, length, 0);

    return bytes_sent;


    return 0;
}

void SendFile(FILE *fp, int file_size)
{
    int current_char = 0;

    do{
     current_char = fgetc(fp);

     if ( current_char == EOF )
      break;

     SendBinary(&current_char, sizeof(char));
    }
    while(current_char != EOF);

}

Any ideas what i should do to prevent this? Revise the whole source for complements to this snippet.

+5  A: 

Perhaps your application is receiving SIGPIPE during the write/send and not ignoring it? Try ignoring this signal or installing a do-nothing handler for it.

Greg Rogers
Like this? signal(SIGPIPE, SIG_IGN);
Filip Ekberg
Works like a charm. How would one Really handle SIGPIPE?
Filip Ekberg
Almost every program ignores SIGPIPE. You will know it happened when the write() or send() fails by properly checking the returned error codes.
robottobor
A good way to detect errors and crashes related to stuff like this and more is to run strace on your program. I can't count how many times strace has been a life saver for me.
Steve Lazaridis
A: 

Are you sure client didn't fail on receiving the data from server?

  1. telnet to your server to see what it sends in response

  2. Replace your server with netcat to make sure client is behaving correctly

  3. Sniff the traffic between server and client using tcpdump or similar tool

qrdl
The client is Chrome and IE, since it's a webserver. In the Chrome case it should behave good, but you never know with IE ;)
Filip Ekberg