gpt4 book ai didi

c - 遍历客户端程序接收到的缓冲区

转载 作者:太空宇宙 更新时间:2023-11-04 04:15:49 25 4
gpt4 key购买 nike

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);
}
}

关于c - 遍历客户端程序接收到的缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52536453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com