![]() |
| | #1 |
| Junglist Join Date: Nov 2005 Location: Bristol
Posts: 118
| 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: 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 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);
}
}
}
[/code] |
| bobthebullet990 is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |