Thread: Client/server problem; server either stops receiving data or client stops sending

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    12

    Client/server problem; server either stops receiving data or client stops sending

    I have a client program running on Unix which creates a socket and connects to a server program running on a Windows machine, then starts performing operations which produce constantly changing double values. I use a timer, and every 5 milliseconds I call a function which takes whatever my value is at that time, converts it to a string, and sends it to the second computer. The server program receives the value from the client and prints to the screen.

    The client and server successfully connect, and initially the sending and receiving works. But after a short time, the server's print messages to the screen slow down, and then the messages showing the new values stop altogether.

    On the client side program, here's the function I call to connect to the server:
    Code:
     if ((ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
      {
      perror("Client-socket() error");
      }
      else
       printf("Client: socket() is OK.\n");
     
    
    	clientService.sin_family = AF_INET;
    	clientService.sin_port = htons(55555);
    	clientService.sin_addr.s_addr = inet_addr("198.9.200.2");
    
      if (connect(ConnectSocket, (struct sockaddr *)&clientService, sizeof(clientService)) < 0)
      {
        printf("Client: Failed to connect.\n");
        close(ConnectSocket);
        exit(-1);
      }
      else
      printf("Client: connect() is OK.\n");
      
      
      printf("Client: Connected to server...\n");
      
      //send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
    }
    and for this the function called every 5 milliseconds by the client to send data:
    Code:
    int bytesRecv;  
         int bytesSent;
        char recvbuf[200] = "";
        char sendbuf1[200]; 
        // convert data to string then send
    	sprintf(sendbuf1,"%20.4f",val);
    	//sending 
    	send(ConnectSocket, sendbuf1, strlen(sendbuf1), 0);
    	
    	
      bytesRecv = recv(ConnectSocket, recvbuf, 32, 0);
    
       if(bytesRecv < 0)
    {
    perror("Client-read() error");
    close(ConnectSocket);
    exit(-1);
    }
    
    else if (bytesRecv == 0)
    
    {
    
    printf("Server program has issued a close()\n");
    
    close(ConnectSocket);
    
    exit(-1);
    
    }
    And for the server, this is the part of the code I use for receiving the data:
    Code:
    for(;;) {
    /* wait for client’s message */
    bytesRecv = recv(socket, recvbuf, 200, 0);
    
    printf("value received: %s\n",recvbuf);
    if (bytesRecv == SOCKET_ERROR)
    {
        printf("Server: recv() error %ld.\n", WSAGetLastError());
    	exit(-1);
    }
    Does anyone have an idea what could be wrong, or what I could do to check for what is wrong?
    Last edited by robot-ic; 02-12-2009 at 11:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use wireshark to see what's going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    Thanks, wireshark looks like it could be of some help.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    From using wireshark, I see that I have a zerowindow problem. Any chance somebody knows where I can find a solution to that? So far I'm coming up empty.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It sounds like the window size is something you could manipulate in the header if you did some ARP redirection. But that is certainly not the answer

    Doesn't this just mean you have a stream input somewhere you are not reading from properly?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    Quote Originally Posted by MK27 View Post
    Doesn't this just mean you have a stream input somewhere you are not reading from properly?
    Hmm, that might be it, I would hope not though. I'm not the one who was responsible for programming the module that deals with generating the data, just the part that dealt with transferring it to another networked computer. There are about 15 source files involved with those parts of the code I didn't work on, so finding a flaw there would be an epic, painful chore.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can use fcntl() to set a socket "nonblock" and then run some kind of of very brief pause in your server's for( ;; ) loop. Most servers have to deal with multiple connections, which I'm assuming is done one of three ways:
    - via select()
    - with multi-threading
    - using the loop I just described.
    Although you don't have to, it may shake something out of a tree. The timed delay may be needed to prevent recv() from dominating the processor in an infinite loop. The timed delay can also make a difference in the successful reception of information, for one strange reason or another.

    I honestly don't know if this will make a difference with tcp sockets, but if you're desperate it's a simple trick to try.

    You could do some printf() debugging in the loop you already have as well.
    Last edited by MK27; 02-15-2009 at 10:23 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    Thanks MK27. I don't really understand the advantage of setting the socket to nonblocking though, could you expand on that?

    Also, do you have any suggestions for what I'd check for with printf statements?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robot-ic View Post
    Thanks MK27. I don't really understand the advantage of setting the socket to nonblocking though, could you expand on that?

    Also, do you have any suggestions for what I'd check for with printf statements?

    "non-blocking" basically means that it doesn't stop and wait for data to arrive, but returns immediately if there is no data. This means that you can "poll" to see if there is data available.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    Quote Originally Posted by matsp View Post
    "non-blocking" basically means that it doesn't stop and wait for data to arrive, but returns immediately if there is no data. This means that you can "poll" to see if there is data available.

    --
    Mats

    But my understanding was that when you you go into the non-blocking mode and poll for data when there is none, you get an error.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > you get an error.
    Yes, something like EAGAIN
    As in nothing here at the moment, try again later.

    This would be a soft error which wouldn't cause the program any particular difficulty if it's all set up to use non-blocking streams.
    Other errors of course you could deal with in the appropriate manner.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-24-2008, 06:05 AM
  2. Sending data to a server
    By Hakins90 in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-16-2008, 07:54 AM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM