我是新来的。我有一个问题,我完全不知道是什么原因造成的!我希望有人能帮助我。我正在开发一个带有套接字的小型TCP服务器,它可以接收来自客户端的字符串,并且必须在上面做一些事情。该程序在此功能中保持阻止,永远不会返回:
int parse_request(char * request, char *start, char**headers, char *body)
该函数的核心是两个嵌套的strtok():的组合
line = strtok_r (request, "n", &saveptr1);
while (line != NULL) {
if (strcmp(line, "rn") == 0 || strcmp(line, "r") == 0) bdy = 1;
else {
if (i == 1) {
/* the first line (command) */
printf("linea iniziale: ");
start = line;
start[strlen(line)] = ' ';
printf ("%sn",start);
printf("n");
}
else {
if (bdy == 0) {
/* the headers */
temp = line;
subline = strtok_r (temp, ":", &saveptr2);
head = subline;
head[strlen(subline)] = ' ';
subline = strtok_r (NULL, ":", &saveptr2);
if (subline != NULL) {
value = subline;
value[strlen(subline)] = ' ';
}
else value = "none";
if (strcmp(head, "Connection") == 0 && strcmp(value, "close") == 0) retval = 0;
if (strcmp(head, "Content-Length") == 0) ignoreboby = 0;
headers[j] = head;
headers[j+1] = value;
printf("header -> %s : %sn", headers[j], headers[j+1]);
j = j + 2;
}
else {
headers[j] = ' ';
if (ignoreboby != 1){
/* the body */
printf("body: ");
body = line;
body[strlen(line)] = ' ';
printf ("%sn",body);
}
else {
body = " ";
**printf("body ignoredn");**
}
}
}
}
//printf("kkk");
line = strtok_r (NULL, "n", &saveptr1);
i++;
}
程序在打印"忽略正文"或"正文:%s\n,正文"后立即阻止。
有人有主意吗?我真的有麻烦了!感谢
编辑:真正的问题是我创建和传递参数的方式吗?
char command[] = " ", body[] = " ";
char **headers;
headers = malloc(8192);
if (!headers) {
printf("Error in malloc()");
closesocket(s);
}
int x = parse_request(buf, command, headers, body);
这是因为您没有更改line
,所以您被困在while
循环中。