C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 02-15-2006, 11:47 AM   #1
Junglist
 
bobthebullet990's Avatar
 
Join Date: Nov 2005
Location: Bristol
Posts: 118
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]
bobthebullet990 is offline   Reply With Quote
Old 02-15-2006, 12:35 PM   #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.
Quantum1024 is offline   Reply With Quote
Old 02-21-2008, 07:36 PM   #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.
gregarious is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-blocking socket connection problem cbalu Linux Programming 25 06-03-2009 02:15 AM
socket message sending and receiving problem black C Programming 5 01-15-2007 04:46 AM
Problem with network code cornholio Linux Programming 1 12-20-2005 01:21 AM
sockets problem programming kavejska C Programming 0 07-25-2005 07:01 AM
Client/Server Socket Receive Problem mariabair Networking/Device Communication 6 12-25-2003 10:01 AM


All times are GMT -6. The time now is 11:14 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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