接收缓冲区显示 "Incomplete Sequence" 的 C 指针
C pointer with recv buffer displaying "Incomplete Sequence"
我正在 recv while 循环中将一些数据读入缓冲区。
我需要查看缓冲区前面的内容并找到剩余的字节。因此,我使用指针在缓冲区中导航以到达我想要的字符,以便将剩余的字节复制到另一个缓冲区中。
然而,当我查看指针位置(以及来自 strcpy
调用的内容)时,我的调试器仅显示前几个字节,后跟一条 "Incomplete Sequence" 消息,并且缓冲区副本仅包含几个字节,直到缓冲区中显示的字节为 [=13=]0
.
This post 描述了由于接收到的字符不是 NUL 终止而引起的问题。我这样做了,它似乎仍在发生。缓冲区本身看起来不错,但它总是看起来好像指针从未处于正确的位置。
我是 C 的新手。获取剩余项目的正确方法是什么,以便我可以完成复制剩余内容所需的工作?
// reading in 256 bytes and leaving last one open to add null term
// at the start of the loop
// buffer contains:
//`"[stuff to look past]377070[=11=]00JFIF[=11=]0[=11=]1\..."`
while ((receivedBytes = recv(sock, buffer, BUFSIZE - 1, MSG_CONFIRM)) > 0) {
buffer[BUFSIZE] = '[=11=]';
// stuff to do ...
// len is calculated as the length of the start of the buffer to look past
// so move the pointer to the start of the contents I want to copy
// but p = [stuff to look past]37707 <incomplete sequence 0>
// and no content past is read into the pointer
char * p = buffer;
p += len
// memcpy fails
memcpy(content, p, sizeof(content));
感谢您的任何见解。
移动声明
char * p = `buffer`;
循环外(和其他建议):
char * p = NULL;
char content[BUFSIZE]; //this needs to be defined somewhere in your code
//as an array if the sizeof macro is to work.
//char *content = calloc(BUFSIZE, 1);//or as a pointer with memory created.
//then freed when no longer needed.
while ((receivedBytes = recv(sock, buffer, BUFSIZE - 1, MSG_CONFIRM)) > 0) {
buffer[receivedBytes] = '[=11=]';
p += receivedBytes;//use receivedBytes to set position.
memcpy(content, p, receivedBytes);//use receivedBytes, not sizeof(content).
...
}
有很多关于 recv()
用法的参考,包括这些:
recv function | Microsoft Docs
recv() - Unix Linux System Call
您还提到了我需要查看缓冲区前面的内容并找到剩余的字节...。您是否考虑过使用 strstr(), or strchr。 return 都指向要搜索的子字符串或要搜索的字符。
例如,如果您有一个已知的唯一字符用作数据字符串中的定界符,比如“>”,那么 strchr() 可用于放置char
:
位置之后的内容指针
char *p = NULL;
const char source[] = {"this is >test string"};
char buff[20];
p = strchr(source, '>');
if(p)
{
// "this is >test string"
// p is here ^
p++;
// "this is >test string"
// p is here ^
strcpy(buff, p);
我正在 recv while 循环中将一些数据读入缓冲区。
我需要查看缓冲区前面的内容并找到剩余的字节。因此,我使用指针在缓冲区中导航以到达我想要的字符,以便将剩余的字节复制到另一个缓冲区中。
然而,当我查看指针位置(以及来自 strcpy
调用的内容)时,我的调试器仅显示前几个字节,后跟一条 "Incomplete Sequence" 消息,并且缓冲区副本仅包含几个字节,直到缓冲区中显示的字节为 [=13=]0
.
This post 描述了由于接收到的字符不是 NUL 终止而引起的问题。我这样做了,它似乎仍在发生。缓冲区本身看起来不错,但它总是看起来好像指针从未处于正确的位置。
我是 C 的新手。获取剩余项目的正确方法是什么,以便我可以完成复制剩余内容所需的工作?
// reading in 256 bytes and leaving last one open to add null term
// at the start of the loop
// buffer contains:
//`"[stuff to look past]377070[=11=]00JFIF[=11=]0[=11=]1\..."`
while ((receivedBytes = recv(sock, buffer, BUFSIZE - 1, MSG_CONFIRM)) > 0) {
buffer[BUFSIZE] = '[=11=]';
// stuff to do ...
// len is calculated as the length of the start of the buffer to look past
// so move the pointer to the start of the contents I want to copy
// but p = [stuff to look past]37707 <incomplete sequence 0>
// and no content past is read into the pointer
char * p = buffer;
p += len
// memcpy fails
memcpy(content, p, sizeof(content));
感谢您的任何见解。
移动声明
char * p = `buffer`;
循环外(和其他建议):
char * p = NULL;
char content[BUFSIZE]; //this needs to be defined somewhere in your code
//as an array if the sizeof macro is to work.
//char *content = calloc(BUFSIZE, 1);//or as a pointer with memory created.
//then freed when no longer needed.
while ((receivedBytes = recv(sock, buffer, BUFSIZE - 1, MSG_CONFIRM)) > 0) {
buffer[receivedBytes] = '[=11=]';
p += receivedBytes;//use receivedBytes to set position.
memcpy(content, p, receivedBytes);//use receivedBytes, not sizeof(content).
...
}
有很多关于 recv()
用法的参考,包括这些:
recv function | Microsoft Docs
recv() - Unix Linux System Call
您还提到了我需要查看缓冲区前面的内容并找到剩余的字节...。您是否考虑过使用 strstr(), or strchr。 return 都指向要搜索的子字符串或要搜索的字符。
例如,如果您有一个已知的唯一字符用作数据字符串中的定界符,比如“>”,那么 strchr() 可用于放置char
:
char *p = NULL;
const char source[] = {"this is >test string"};
char buff[20];
p = strchr(source, '>');
if(p)
{
// "this is >test string"
// p is here ^
p++;
// "this is >test string"
// p is here ^
strcpy(buff, p);