Socket Programming Problem!!!!

This is a discussion on Socket Programming Problem!!!! within the Networking/Device Communication forums, part of the General Programming Boards category; I posted here at the beginning of the afternoon, but I now have a much clearer way to show what ...

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

    Angry Socket Programming Problem!!!!

    I posted here at the beginning of the afternoon, but I now have a much clearer way to show what is going wrong with my program!!!!

    I have a coffeepot control server (accepts multiple clients) and you can give various requests! Right, so the problem; when a client connects, a request message is sent to the server, the server processes the request and creates a response! However, when doing a propfind (one of the requests) I am getting a very strange error!!!!

    It works perfectly the first time around, but then after that nothing is being sent from the server to the client, despite the fact that the server is printing the contents of the char array to the screen the line before it sends it and it is correct at the server end!!!!!

    To show this; the clients useage...
    Code:
    NOTE: Message [user input] response [servers response]
    Message: PROPFIND POT-1 HTCPCP/1.0
    response: HTCPCP/1.0 200 OK
    Properties for POT-1
    Can store 10 cup(s) of coffee
    Contains 5 cups of coffee
    
    Message: PROPFIND POT-2 HTCPCP/1.0
    response:
    Message: PROPFIND POT-3 HTCPCP/1.0
    response:
    And the output at the server...
    Code:
    STARTING UP COFFEEPOT SERVER...
    Retreiving machine status...
    --------------------------------------------
    -- NEW CLIENT (127.0.0.1) HAS CONNECTED
    --------------------------------------------
    PROPFIND Request sent by client (127.0.0.1)
    sending: HTCPCP/1.0 200 OK
    Properties for POT-1
    Can store 10 cup(s) of coffee
    Contains 5 cups of coffee
    
    PROPFIND Request sent by client (127.0.0.1)
    sending: HTCPCP/1.0 200 OK
    Properties for POT-2
    Can store 10 cup(s) of coffee
    Contains 3 cups of coffee
    
    PROPFIND Request sent by client (127.0.0.1)
    sending: HTCPCP/1.0 200 OK
    Properties for POT-3
    Can store 10 cup(s) of coffee
    Contains 8 cups of coffee
    As you can see from the above example, the server is correctly processing my data, but why isn't the client getting the servers response!!!???!?!?!?! ARRGGHH, Its getting annoying now, I have tried everything i can think of, but still at the same place!!!!!!


    The code the server uses to send messages for the propfind bit is:
    Code:
              /* DID THE CLIENT WANT TO DO A PROPFIND? */
              else if (strcasecmp(tokens[0], "PROPFIND") == 0) {
                printf("PROPFIND Request sent by client (%s)\n", clientIP);
                /* PROPFIND SERVER */
                if (strcasecmp(tokens[1], "SERVER") == 0) {
                  if (sprintf(sBuff, "%s %sServer Properties:\nCoffeepot server version: %s\nNumber of coffeepots: %d\r\n", VERSION, OK, VERSION, POTS) < 0) {
                    printf("ERROR! Could not build response for client (%s)\n", clientIP);
                  }
                  else {
                    n = write(newsockfd, sBuff, MAXBUFF);
                    if (n < 0) printf("ERROR! Could not send respond to client (%s)\n", clientIP);
                  }
                }
                /* PROPFIND POT-X */
                else if (strncasecmp(tokens[1], "POT-", 4) == 0) {
                  /* FIND OUT THE COFFEEPOT NUMBER CLIENT WISHES TO PROPFIND */
                  int potNo = tokens[1][4];
                  potNo -= '0';              /* CONVER FROM CHAR TO INT */
                  
                  /* IS THE POT NUMBER VALID? */
                  if ((potNo < 0) || (potNo > POTS)){
                    printf("ERROR! Client (%s) sent an un-recognised PROPFIND request!\n", clientIP);
                    /* RESPOND TO CLIENT */
                    if (sprintf(sBuff, "%s %sERROR! No such coffeepot (pot-%d) exists on this server!\r\n", VERSION, SERVICE_UNAVAILABLE, potNo) < 0) {
                      printf("ERROR! Could not build response for client (%s)\n", clientIP);
    	        }
                    else {
                      n = write(newsockfd, sBuff, MAXBUFF);
                      if (n < 0) printf("ERROR! Could not send response to client (%s)\n", clientIP);
                    }
                  }
                  else {
                    /* RESPOND TO CLIENT WITH THE PROPERTIES OF THE REQUESTED POT! */
                    if (sprintf(sBuff, "%s %sProperties for POT-%d\nCan store %d cup(s) of coffee\nContains %d cups of coffee\r\n", 
                       VERSION, OK, potNo, FULL, coffeepots[potNo - 1]) < 0) {
                      printf("ERROR! Could not build response for client (%s)\n", clientIP);
                    }
                    else {
                      printf("sending: %s\n", sBuff);
                      n = write(newsockfd, sBuff, MAXBUFF);
                      if (n < 0) printf("ERROR! Could not send response to client (%s)\n", clientIP);
                    }
    	      }
                  n = write(newsockfd, sBuff, MAXBUFF);
                  if (n < 0) printf("ERROR! Could not send response to client (%s)\n", clientIP);
                }
                /* BAD PROPFIND REQUEST */
                else {
                  printf("ERROR! Client (%s) sent an un-recognised PROPFIND request!\n", clientIP);
                  /* RESPOND TO CLIENT */
                  if (sprintf(sBuff, "%s %sERROR! Un-Recognised PROPFIND request!\r\n", VERSION, BAD_REQUEST) < 0) {
                    printf("ERROR! Could not build response for client (%s)\n", clientIP);
    	      }
                  else {
                    n = write(newsockfd, sBuff, MAXBUFF);
                    if (n < 0) printf("ERROR! Could not send response to client (%s)\n", clientIP);
                  }
                }
              }
    Many thanks to anybody that has a look at this!!! and thanks for your help everyone!!!!!!!!!!!!
    [/code]

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Maybe because you send MAXBUFF amount of data when your actual string is much shorter so you end up sending lots of zeros (assuming the buffer was zeroed) or even junk data. Use strlen to determine how much data you send.

    Since each line ends with \r\n and each message ends with \r\n\r\n you can use those as deliminators.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Chunked Encoding in HTTP 1.1

    Use read/write and use sleep(WAIT_TIME) in between a read and a write so as to allow time for full request to be sent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 03:15 AM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 01:21 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 08:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21