C 编程新手在这里寻找一点帮助!我有一个缓冲区,其中包含客户端程序提供的一些数据让我们这样说:
pcity:阿姆斯特丹 gcity
我的问题是,在服务器内部,我想遍历此循环并“弹出”我已经向客户端发送答复的命令和参数。在这个例子中,可以说,在我处理 pcityAmsterdam 部分之后,我希望缓冲区变成 gcity。
到目前为止,这是我的代码:
while(sizeof(incbuf) > 0)
{
if(incbuf[0] == 103) //get request
{
puts("get req");
memmove(incbuf, incbuf+1, strlen(incbuf));
//puts(incbuf);
key = strtok(incbuf, ":");
//printf("%s\n",key);
res = get(key);
printf("%s\n",&res);
if(res != NULL) //we got a match
{
puts("match");
if(servbuf[0] == '\0')
servbuf[0] = 102;
else
servbuf[strlen(servbuf)] = 102;
strcat(servbuf, res);
send(new_socket,servbuf,1024, 0);
}
else //not found
{
puts("not a match");
if(servbuf[0] == '\0')
servbuf[0] = 110;
else
servbuf[strlen(servbuf)] = 110;
strcat(servbuf, &res);
printf("BEFORE THE NOT FOUND: %s\n", servbuf);
send(new_socket,servbuf,1024, 0);
}
memmove(incbuf, incbuf+strlen(key), strlen(incbuf));
printf("%s\n", incbuf);
}
else if(incbuf[0] == 112) //put request
{
puts("put req");
memmove(incbuf, incbuf+1, strlen(incbuf));
char* keytoken = strtok(incbuf, ":"); //get rid of the type
printf("token: %s\n",keytoken );
char* key = malloc((strlen(keytoken)));
char* value = malloc((strlen(incbuf))); //Amsterdam--> key :city
strcpy(key, keytoken);
printf("key: %s\n",key );
char* valuetoken = strtok(NULL, ": ");
strcpy(value, valuetoken);
printf("value: %s\n",value );
put(key, value);
memmove(incbuf, incbuf+strlen(key)+strlen(value)+1, strlen(incbuf));
printf("AFTER MEMMOVE THIS IS THE BUF: %s\n", incbuf);
}
此时我知道 strtok 弄乱了我的 incbuf 数组,但我需要它来标记数组。可能是这里的设计问题?
有什么建议吗?
我设法通过变通办法做到了。 (将 incbuf 复制到临时缓冲区就成功了,感谢您指出。)
while(strlen(incbuf) > 0)
{
for (int i = 0; i < sizeof(incbuf); ++i)
tmpbuf[i] = incbuf[i];
if(incbuf[0] == 103) //get request
{
memmove(tmpbuf, tmpbuf+1, strlen(tmpbuf));
key = strtok(tmpbuf, " ");
res = get(key);
//printf("%p\n",(void*) &res);
if(res != NULL) //we got a match
{
if(servbuf[0] == '\0')
servbuf[0] = 102;
else
servbuf[strlen(servbuf)] = 102;
strcat(servbuf, res);
send(new_socket,servbuf,1024, 0);
memset(servbuf, '\0', sizeof(servbuf));
memmove(incbuf, incbuf+strlen(key), strlen(incbuf) - strlen(key));
}
else //not found
{
if(servbuf[0] == '\0')
servbuf[0] = 110;
else
servbuf[strlen(servbuf)] = 110;
strcat(servbuf, &res);
send(new_socket,servbuf,1024, 0);
}
memset(servbuf, '\0', sizeof(servbuf));
memmove(incbuf, incbuf+strlen(key)+2, strlen(incbuf) - strlen(key) +1);
//memmove(incbuf, incbuf+strlen(key), strlen(incbuf));
}
else if(incbuf[0] == 112) //put request
{
memmove(tmpbuf, tmpbuf+1, strlen(tmpbuf));
char* keytoken = strtok(tmpbuf, ":"); //get rid of the type
char* key = malloc((strlen(keytoken)));
char* value = malloc((strlen(tmpbuf))); //Amsterdam--> key :city
strcpy(key, keytoken);
char* valuetoken = strtok(NULL, ": ");
strcpy(value, valuetoken);
put(key, value);
memset(servbuf, '\0', sizeof(servbuf));
memmove(incbuf, incbuf+strlen(key)+2 + strlen(value)+1, strlen(incbuf) - strlen(key) -strlen(value) +1);
}
else //immediate disconnection of the server in case of invalid requests
{
printf("ERROR\n");
close(new_socket);
exit(1);
}
}
我是一名优秀的程序员,十分优秀!