I have the following scenario in C: a simple client program sends HTTP-like requests to a server, which is then supposed to return the input of the specified file. So far, there are only three methods the server should support: GET, HEAD and a default answer for bad requests.
Here's part of the server program:
for(;;){
struct sockaddr_in clientAddr;
int clientAddrLen = sizeof(clientAddr);
int clientSocket;
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
char buf[BUFSIZE];
int bytesRecv;
bytesRecv = recv(clientSocket, (char*)&buf, BUFSIZE, 0);
while(bytesRecv > 0){
char *method;
method = strtok(buf, " ");
printf("method %s\n", method);
if(strncmp(method, head, 4) == 0){
/*HEAD*/
char answer[] = "method: HEAD\0";
send(clientSocket, answer, sizeof(answer) + 1, 0);
}else{
if (strncmp(method, get, 3) == 0){
/*GET*/
char answer[] = "method: GET\0";
send(clientSocket, answer, sizeof(answer) + 1, 0);
}else{
/*bad request*/
char answer[] = "HTTP/1.1 400 Bad Request\0";
send(clientSocket, answer, sizeof(answer) + 1, 0);
}
}
/*Read*/
bytesRecv = recv(clientSocket, (char*)&buf, BUFSIZE, 0);
}
And this is the part of the client program, that asks to enter a request on the command line and then sends the respective input:
char* server_reply[1024];
char* msg[100];
/*send requests*/
for(;;){
printf("Request:\n");
scanf("%s", &msg);
if(strcmp(msg, "quit") == 0){
break;
}
send(clientSocket, msg, sizeof(msg), 0);
if(recv(clientSocket, server_reply, sizeof(server_reply), 0) < 0){
printf("Failure in recv.");
}else{
printf("Server: %s\n", server_reply);
}
}
Now, the problem is the following: When I enter for example "GET HEAD asd" on the client side, I get three answers from the server, namely
method: GET
method: HEAD
HTTP/1.1 400 Bad Request
when it actually should only be "method: GET". I really don't understand this beaviour ... Thanks for your help!