简体   繁体   English

具有0x00字符的c ++数据数组

[英]c++ data array with 0x00 character

I'm trying to send some data by socket with winsock. 我正在尝试使用Winsock通过套接字发送一些数据。 I am encountering the problem where my data (which is actually an array of char) may contain some zeros but the char 0x00 represents the null character and all the following characters are cut as it represents the terminating character. 我遇到的问题是我的数据(实际上是char的数组)可能包含一些零,但char 0x00表示空字符,并且以下所有字符均被截断,因为它表示终止字符。 Does anyone have an idea on how I should solve this? 有人对我应该如何解决这个问题有想法吗? What alternative could I use ? 我可以使用什么替代方法?

What I want to do is to send bytes with winsock which might represents integers, char arrays or something else. 我想做的是用winsock发送字节,它可能代表整数,char数组或其他内容。 The receiving part will take care of reading correctly, but this is not the concern of this post. 接收部分将正确阅读,但这与本文无关。

I think you might be doing something like this : ` 我想你可能正在做这样的事情:

char s[100]; 字符s [100];

//Do stuff with s //用s做东西

send(sock, s, strlen(s), flags); send(sock,s,strlen(s),flags); ` `

strlen finds the lenght of a string by counting how many caracters are until it finds NULL (0). strlen通过计数字符串的长度来找到字符串的长度,直到找到NULL(0)。

make the first two bytes in your data stream, be the length of the buffer you are sending. 使数据流中的前两个字节成为要发送的缓冲区的长度。 then send that many bytes. 然后发送那么多字节。

on the other end, read the two bytes, and then you know exactly how many bytes should be coming to you. 在另一端,读取两个字节,然后您确切知道要传入多少个字节。

that will let you work around the issue. 这样您就可以解决此问题。

also google network byte order. 也谷歌网络字节顺序。 there are some handy function calls, that make portability easy. 有一些方便的函数调用,使移植变得容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM