Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, every once a while one of my programs which connects to a server using TCP shows some very odd behaviour, which shows up like this: 1. after a application ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    3

    network programming problem


    Hello,

    every once a while one of my programs which connects to a server using TCP shows some very odd behaviour, which shows up like this:

    1. after a application timeout (i.e. i don't get an application protocol response for a message send) i close the socket using
    shutdown(soc, SHUT_RDWR);
    close(soc);
    2. i re-connect to the server and send() a message, which works fine
    3. immediately after this, i'm falling into recv() to wait for an answer, recv returns 0 which indicates me the destination has closed the connection
    4. i continue with step 2 (after a short delay), unfortunately this leds to a loop

    Up to now i always thought the remote side has some serious problems causing this behaviour, but here is what puzzles me:

    I did a tcpdump to check whats really going on, and it shows me that MY host is sending a tcp packet with the data and the FIN, PSH, ACK flags set to the remote host?!?!
    Does this sound familiar to anyone? It is not what my program is doing or given back by the socket functions. What could cause such a tcp behaviour? When i did a netstat on the remote IP, i saw a lot of connections in the LAST_ACK state.

    Any help or hints (is there a better place to ask this?) would be greatly appreciated.

    thanks,
    christian!

  2. #2
    Just Joined!
    Join Date
    Nov 2007
    Location
    Camp Pendleton
    Posts
    55
    Hi,

    Could you send the code? Or if it's too big, at least the client's setting up the connection and send()?

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    3
    Hello,

    thanks for your reply! I'm afraid it's a little bit spread over multiple source files, the connect functions looks like this:
    (#define SOCKET int)
    SOCKET CommHandling::Connect(char* IP, int port)
    {
    int iReturn;
    if(sock != -1)
    lsy_socketClose(sock);

    sock = lsy_createTcpIpSocket();
    if(sock == INVALID_SOCKET)
    {
    return -1;
    }

    int bReuse = 1;
    setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,(const char FAR *)&bReuse, sizeof(bReuse));

    iReturn = lsy_connectToServer(sock, IP, port);
    if(iReturn != 0)
    {
    return -1;
    }
    return sock;
    }

    where the lsy_ functions do under linux:
    lsy_socketClose(SOCKET soc) {
    shutdown(soc, SHUT_RDWR);
    return close(soc);
    }
    SOCKET lsy_createTcpIpSocket()
    {
    SOCKET soc;
    soc = socket(AF_INET, SOCK_STREAM, 0);
    return soc;
    }
    lsy_connectToServer(SOCKET soc, LPSTR szHostName, unsigned int uiPortAddr)
    {
    struct sockaddr_in RemAddr;
    int err;
    u_long ulHostAddress;
    u_short usPortNo;

    memset ( &RemAddr, 0x00, sizeof(RemAddr));
    err = SOCKET_ERROR;

    //these lsy_ functions basically call the named API functions
    if((ulHostAddress = lsy_getHostAddressOverName(szHostName)) == 0)
    {
    if((ulHostAddress = lsy_getHostAddress(szHostName)) == 0)
    {
    return err;
    }
    }

    RemAddr.sin_family = AF_INET;
    RemAddr.sin_addr.s_addr = ulHostAddress ;
    RemAddr.sin_port = htons(uiPortAddr);

    err = connect(soc, (struct sockaddr *)&RemAddr, sizeof(RemAddr));
    return err;
    }


    the send method looks like this:
    int CommHandling::ServerSend(char* sendbuf, int len)
    {
    if(send(sock, sendbuf, len, MSG_NOSIGNAL) == -1)
    {
    return -1;
    }
    return 0;
    }

    the receive is done like this:
    iRecv = recv(sock, acMsgBuffer, iBuffLen-1, 0);
    and like i said, it returns 0.

    any ideas?
    thanks.

    christian!

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2007
    Location
    Camp Pendleton
    Posts
    55
    I would take out that call to shutdown(). I think you're disallowing any further communication on the socket and the server is left waiting in the LAST_ACK status.

    Then, after this happens enough, you try to reconnect and are probably getting refused because the server has too many open connections. (Though the majority are just waiting to be torn down.)

    Let me know if that helps...

  6. #5
    Just Joined!
    Join Date
    Nov 2007
    Posts
    3
    I was always assuming that shutdown() prevents further data transmission, but not ALL tcp communication (i.e. the necessary FIN ACK packets)?!

    the intention was to make the socketClose call non-blocking, as I really do not need any DATA that arrives after it's call (bec. i'll re-transmit the original submission it after successful re-connection).

    Anyway, i'll try removing the shutdown and see if it fixes the problem. Thanks for your ideas!

    christian!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •