In a simple TCP server written in C, I have a function that reads a string from a client, and then checks in a if
-query if it matches "GET /index.html HTTP/1.0\r\n
". However, when a Client sends exactly this string from a terminal, the query always goes into the else
-case. How can this be?
This is the function that reads the string from the client:
void html_echo(int sockfd) {
ssize_t n;
char clientInputString[MAXLINE];
n = readline(sockfd, clientInputString, sizeof(clientInputString));
if (strcmp(clientInputString, "GET /index.html HTTP/1.0\r\n") == 0 ||
strcmp(clientInputString, "GET /index.html HTTP/1.1\r\n") == 0 ){
printf("Anfrage passt\n");
} else{
printf("Falsche Anfrage: %s, Länge: %lu\n", clientInputString, strlen(clientInputString));
}
}
This is the function that reads a whole line from the client:
ssize_t readline(int fd, void *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;
for( n=1; n<maxlen; n++ ) {
again:
if( (rc=read(fd, &c, 1)) == 1 ) {
*ptr++ = c;
if( c=='\n' )
break;
} else if( rc == 0 ) {
if( n == 1 )
return(0); /* EOF, no data */
else
break; /* EOF, some data */
} else {
if( errno == EINTR )
goto again; /* doit again */
return(-1); /* Error */
}
}
*ptr = '\0';
return(n);
}
This is how the string is sent by the client:
This is how the string is receeved by the server:
This is the content of the string, as per @user3386109's suggested function:
47
45
54
20
2f
69
6e
64
65
78
2e
68
74
6d
6c
20
48
54
54
50
2f
31
2e
30
5c
72
5c
6e
0a
I tried adding an additional \r\n
on the client side, because maybe the terminal appends some characters, but this didn't help. I also tried it without \r\n
at all, and it still doesn't work.