By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,374 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,374 IT Pros & Developers. It's quick & easy.

Byte 0 problem over socjets C++ and C#

P: 5

I am fighting to sync a C++ client with a C# server, I have managed to create a byte array in C++ stored as char* to be sent over the network to the server written in C#.

on the C# side, I get the data on a byte array which I successfully convert to an object with similarities to the object the data came from in C++, then I construct an object with the reply, encode it to a byte array and I send it back to the client in C++

Here I am stuck, while getting the data in C++ it is not arriving complete, or I am missing where to look for that data.

ok, and example to be more precise..

fom C++
I construct a char* representation of a byte array for this data

unsigned short = 6
char[2] = "OP"
char = 101
char = 0

the first value is the length of the package, a short is 2 bytes, a char is one byte, that makes 6 bytes total so, the byte array is this

[6,0,79,80,101,0], the first two bytes is the short number in little endian becuase of intel arch, the 79 is the O, the 80 is the P, and the rest as is.

on C#, I get this information correctly and I decode it to the object with no problem, then I code a new message.

ushort = 6
byte[2] = OK
byte = 101
byte = 0

[6,0,79,75,101,0] and I send this to C++.

on C++ I get the info on a char* variable, becuase sockets are this way there, but here come the problem, the second byte "0" I think is percieved on the char* as a null character terminator, so, I end up with only 6 on the data received from the network

anyone can give a tip about how to deal with this? 0 byte value on the network package.


Aug 7 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 463
To recap: the C++ to C# is working fine, but the C# to C++ is only giving you a string with a 6 in it (as far as you can tell).

This could definitely be a string vrs binary issue, but there are ways to check. First you need to see how many bytes of data you're getting over the socket. What are you using to read the socket?

You need some IO routine that reads binary data. In C, fread and fwrite work with buffers and tell you how much they got/sent. You can't use printf with a string because of the zero problem. In C++, istreams have a read method that also fills up a buffer.

Once you know how many bytes made it to C++, then you figure out where the problem is.
Aug 8 '07 #2

P: 5

I am getting the data from C# on the C++ side this way

err = recv(m_ClientSocket, Buff, 65535, 0);

err is now 6,
and the 65535 is the max my data can be.

so, on this Buff variable, strlen do not apply? how I can get the data this way?
I need to put the first 2 bytes, into a short, the third and forth into char[2], and the other 2 in 2 char variables, in the case that the info is bigger, that mean that the information from C# is not only one message, but that is another story, I need to figure out how to get one message and do the same in case more than one arrive in the reame recv.
Aug 8 '07 #3

P: 104
If you are getting '6' as the return value of 'recv' function, then it means the 6 bytes has been received successfully and is safe in the buffer. So, you have to concentrate in how you handle the buffer.
For Eg: Did you try using 'memcpy' function for extracting data or Just writing the whole buffer to a file [ fwrite(buff, 1, 6, fp) ]
Aug 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.