简体   繁体   中英

c++ data array with 0x00 character

I'm trying to send some data by socket with 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. 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. 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];

//Do stuff with s

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

strlen finds the lenght of a string by counting how many caracters are until it finds 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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