Thread: Slight problem with socket reading!!!

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Question Slight problem with socket reading!!!

    Annoying problem!!!! ...I have a client server application; requests are sent between the two until the user disconnects!!! (simple)

    So, the problem is when the client connects, you send a request to the server, the server replies and the reply is shown to the user on the client interface! However, when the server is responding any time after the first time; the data at the client end never gets printed (or received)!!!
    ...So i made sure the server was sending data to the client over the connection, which it was, then i made sure the client was reading by checking the number of chars read (which it says is 255, but that is wrong! it should be around 30-50 chars!!!!!!) WHATS GOING ON?????????

    My recieve data from socket function is.....

    Code:
    int readMsgFromServer(char *theMessage){
      
      /* ATTEMPT TO READ DATA FROM SOCKET */
      n = recv(sockfd, theMessage, (BUFFSIZE - 1), 0);
      if (n < 0) { 
        return ERROR;
      }
      else {
        return 0; 
      }
      
    }
    The call to read from the socket in the client program is...

    Code:
              bzero(buffer, BUFFSIZE); 
              if (readMsgFromServer(buffer) != 0) {
                printf("ERROR Reading the servers response! Try again!\n");
              }
              if (n < 0) printf("error\n");
              else {
    	    printf("RESPONSE FROM SERVER...\n%s\n",buffer);
              }
    So, if i check n on the second read from socket its 255!!! (but there isn't a response string to print to the user!!!!! ...where the hell does it go?????? the server definatley is sending data! The error is somewhere in the client read function.

    NOTE: For the time being the socket variables and structs are declared as global so all functions have access to the socket!!!

    Any ideas??? Cheers!!!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    What type is n?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You need to either be sure how much data you are expecting and call recv untill you have that amount or use a deliminator to indicate the end of a message and keep receiving data untill you reach the deliminator.

    As it stands your single call to recv may return anywhere from 1 byte to the amount of data the server sent.

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    what type is n?

    n is an integer!

  5. #5
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Quote Originally Posted by Quantum1024
    You need to either be sure how much data you are expecting and call recv untill you have that amount or use a deliminator to indicate the end of a message and keep receiving data untill you reach the deliminator.

    As it stands your single call to recv may return anywhere from 1 byte to the amount of data the server sent.
    I am expecting data of size MAXBUFF to arrive; MAXBUFF is currently set to 256 because that is more than enough space to recieve the messages!

    It is for university coursework, for a coffeepot client / server program!!!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    well recv dosen't wait untill it has received the buffer size, it returns however much data is available.

    To recv MAXBUF-1 bytes you would do something like this. I will assume a buffer that is zeroed.

    Code:
    n=0;
    int recved=0;
    while (recved<MAXBUFFSIZE-1)
    {
       n = recv(sockfd, &theMessage[recved], ((BUFFSIZE - 1)-recved), 0);
       if (n==SOCKET_ERROR || n==0)
          return FALSE;
    recved+=n;
    }
    return TRUE;
    If you use something like that then the server should always send data who's size is MAXBUFFSIZE-1 bytes.
    Last edited by Quantum1024; 02-15-2006 at 09:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Problem with the socket
    By shiju in forum C Programming
    Replies: 3
    Last Post: 10-22-2004, 04:29 AM
  4. reading file problem
    By samsam1 in forum Windows Programming
    Replies: 4
    Last Post: 01-15-2003, 06:03 PM
  5. simple socket problem.
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 01-14-2003, 06:20 PM