[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with TCP

Posted on 2006-05-15
5
Medium Priority
?
227 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:woigl
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 16682429
>> 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
 

Author Comment

by:woigl
ID: 16682537
>> >> 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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 750 total points
ID: 16682689
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
 

Author Comment

by:woigl
ID: 16682743
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 16682767
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question