通过套接字接收的数据不能用于 C++
所以我正在进行套接字通信,并且我收到了正确的消息...但是,当我尝试从接收缓冲区中提取它时...它不起作用。我的代码看起来像这样:
void CClientSocket::OnReceive(int nErrorCode)
{
char buff[1000];
int ibuf = Receive(buff,sizeof(buff));
buff[sizeof(buff)-1] ='\0';
CSocket::OnReceive(nErrorCode);
}
当我在调试中运行时, buff 的值包含正确的消息......以及填充数组未使用部分的一堆随机字符。但是,我无法使用该数据...如果我尝试转换为 CString 甚至将字符传输到不同的数组...则不起作用。我很困惑,我真的不知道该怎么办。
如果您需要更多信息或者我有任何不清楚的地方,请告诉我。
编辑:我尝试提取数据的一些代码
CString string;
string = CString(buff);
和
char *msg;
msg = new char[ibuf]
for(int i = 0; i < ibuf; i++){
msg[i] = (char)buff[i];
}
编辑:一些说明 当我说“没有任何作用”时,我的意思是当我尝试转换它时,结果是空的 CString 或空的 char 等......没有错误。
so I'm getting socket communication, AND I'm getting the proper message...however when I try to extract it from the receive buffer...it doesn't work. My code looks something like this:
void CClientSocket::OnReceive(int nErrorCode)
{
char buff[1000];
int ibuf = Receive(buff,sizeof(buff));
buff[sizeof(buff)-1] ='\0';
CSocket::OnReceive(nErrorCode);
}
When I run in debug, the value of buff contains the proper message...along with a bunch of random characters filling up the unused portion of the array. However, I can't use that data...if I try to convert to CString or even to transfer the characters to a different array...nothing works. I'm baffled and I really don't know what to do.
Let me know if you need more info or if I am unclear in any way.
EDIT: some code I have tried to extract the data
CString string;
string = CString(buff);
AND
char *msg;
msg = new char[ibuf]
for(int i = 0; i < ibuf; i++){
msg[i] = (char)buff[i];
}
EDIT: some clarification
when i said 'nothing works' I meant that when I tried to convert it, the result was and empty CString or empty char etc...there are no errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将零字节放在 buff[ibuf] 处,而不是第二行。但在此之前,添加一个错误检查(确保 ibuf > 0,如果不是,则将其视为连接丢失。)此外,如果您这样做,您应该 Receive(buff , sizeof(buff)-1); (注意
-1
- 这说明了终止字符 - 感谢 @Altnitak 指出了这一点。)另一方面......我还会争辩说这不是治疗套接字的方法 代码。字符串不是通过网络自动发送的。他们将会分裂。如果您控制协议,我建议您放弃以字符串为中心的宇宙视图,并构建网络通信,以便消息以整数(
uint16_t
或uint32_t
,并确保调用htons
/htonl
等)指示后面有效负载的长度。然后继续缓冲传入的字节,并在有足够的字节来满足指定的长度时处理它们。如果您不控制协议,请找到一个方便的标记,您可以使用它来决定何时处理内容,并缓冲字节直到到达它。 (例如,如果您正在执行 HTTP,您可能会缓冲字符,直到在处理标头时看到换行符,然后为响应内容决定特定的缓冲区大小...)
Rather than that second line, put the zero byte at
buff[ibuf]
. But before that, add an error check (make sureibuf > 0
and if not, treat it as a connection drop.) Also, if you do it this way you shouldReceive(buff, sizeof(buff)-1);
(note the-1
- this accounts for the terminating character - thanks @Altnitak for pointing this out.)On the other hand... I would also argue that this is no way to treat socket code. Strings are not sent atomically over the network. They will be split. If you control the protocol, I suggest you depart the string-centered view of the universe, and structure your network communications so that messages begin with an integer (
uint16_t
oruint32_t
, and make sure to callhtons
/htonl
etc.) indicating the length of the payload that follows. Then keep buffering incoming bytes and process them when you have enough to satisfy the lengths specified.If you do not control the protocol, find a convenient marker that you can use to decide when to process content, and buffer bytes until you reach it. (For example if you're doing HTTP, you might buffer chars until you see a newline while processing headers, then decide on a certain buffer size for the contents of the response...)