Problems with TCP

I wrote my own server wich is working great...

there is just one thing which is not working probably. I am using the select statement for incoming data. In case if the timeout is happening the select statement inform me abt it. but if before the timeout is happening the client which is connecting to me stop without inform me then the select statement dont inform me and my server hangs...

my code:

do
  {
    fd_set readset;
    struct timeval tv;
    int ret = -1;
    // Initialize the set
    FD_ZERO(&readset);
    FD_SET(fd, &readset);
    tv.tv_sec = SOCKET_TIMEOUT_SEC;
    tv.tv_usec = 0;

    ret = select (FD_SETSIZE, &readset, NULL, NULL, &tv); // Wait for one second

    if (ret == 0) // Timeout
    {
      printf("Connection aborted because of socket timeout (%d seconds)...\n", SOCKET_TIMEOUT_SEC);
      return NULL;
    }
    else if (ret < 0) // Some error
    {
      printf("Connection aborted because of socket error...\n");
      return NULL;
    }
    else
    {
      if ((numbytes=recv(fd, buf, MAXDATASIZE, 0)) == -1) {
        printf("recv error - connection will be closed...\n");
      }

      buf[numbytes] = '\0';
      message = (char *)realloc(message, sizeof(char) * ( strlen(message) + 1 + strlen(buf) ) );
      strcat(message, buf);
    }
  }while (buf[numbytes-2]!=CR || buf[numbytes-1]!=LF);

has anyone a idea how to get informed if the client is gone?
woiglAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
Well, one problem I see is that you never accept the connection ... so that's probably your problem.

In the else after the select, you have to include something like this :

    if (FD_ISSET(fd, &readset))
      // accept the connection !!

Then that connection will be serviced on a different socket, which you then check for available data to read.

Again, I refer to the link I posted earlier ... it's a fine example of the use of select() to create a server.
0
 
Infinity08Commented:
>> but if before the timeout is happening the client which is connecting to me stop without inform me
Can you explain a bit more what you mean by this ? If the client doesn't reply during connection, then the timeout should occur.

In any case, here's a good example code of what you should have :

http://www.lowtek.com/sockets/select.html

It's very complete, and you'll probably find the difference with your code ...
0
 
woiglAuthor Commented:
>> >> but if before the timeout is happening the client which is connecting to me stop without inform me
>> Can you explain a bit more what you mean by this ? If the client doesn't reply during connection, then the timeout should occur.

Following scenario: The client is connected with the server and the server is at the select statement. At this time the client crash (reason not important). From this time on the timout is not happening and the select still wait. If the client is alive and dont send data then the timeout works fine.
0
 
woiglAuthor Commented:
I found the problem... the problem is not that i dont accept the connection. the problem is that i didnt handle if the recv returns 0... this is only happening if the other side die.
0
 
Infinity08Commented:
If you keep your design that way, you'll only be able to have one connection at a time to the server ! You can do a lot more with select() if you wish ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.