Hello all.
I am mostly a C# developer, and I am working on a research project which involves networking between a server written in C# and the client written in C++.
I have defined a protocol that will move info to/from the server, C# socket functions gets bytes from the network, while C++ char*.
So I am running in problem to make C++ to handle and convert values to bytes, I know there is no naive type of byte in C++, but I think char will do that, because a char is a one byte size type in C++. (Visual C++).
the first segment of the protocol is a 2 byte value, that tell the length of the message, in both C++ and C# this value is an unsigned short, which is 2 bytes.
while converting unsigned short to char[2], I run into trouble when the value of the short is less than 255, becuase that way it fits in one byte, then what is received on the C# side is not correctly interpreted.
this is the code I am using to convert from unsigned short to char[2]
unsigned short m_length = 6;
char *data = new char[m_length];
memset(data,'\0 ',m_length); // All the data block to be sent.
char clen[2];
*(unsigned short *)clen = m_length;
// clen is clen[0] = 6, clen[1] = 0, becuase it is intel and stores the value in
// little ending.
strcat(data,cle n);
// I spect that data[0] will have 6 and data[1] 0.
strcat(data,"O" );
// and after this I need data[2] = 'O'.
Becuase clen[1] is 0 byte it is ignore when I call the strcat, I am not sure if the string concatenation function is the right to use in here, but the thing is that somehow I need to conver all to char * to be able to send it over the socket.
Some one has a more better approach to solve this?
I am mostly a C# developer, and I am working on a research project which involves networking between a server written in C# and the client written in C++.
I have defined a protocol that will move info to/from the server, C# socket functions gets bytes from the network, while C++ char*.
So I am running in problem to make C++ to handle and convert values to bytes, I know there is no naive type of byte in C++, but I think char will do that, because a char is a one byte size type in C++. (Visual C++).
the first segment of the protocol is a 2 byte value, that tell the length of the message, in both C++ and C# this value is an unsigned short, which is 2 bytes.
while converting unsigned short to char[2], I run into trouble when the value of the short is less than 255, becuase that way it fits in one byte, then what is received on the C# side is not correctly interpreted.
this is the code I am using to convert from unsigned short to char[2]
unsigned short m_length = 6;
char *data = new char[m_length];
memset(data,'\0 ',m_length); // All the data block to be sent.
char clen[2];
*(unsigned short *)clen = m_length;
// clen is clen[0] = 6, clen[1] = 0, becuase it is intel and stores the value in
// little ending.
strcat(data,cle n);
// I spect that data[0] will have 6 and data[1] 0.
strcat(data,"O" );
// and after this I need data[2] = 'O'.
Becuase clen[1] is 0 byte it is ignore when I call the strcat, I am not sure if the string concatenation function is the right to use in here, but the thing is that somehow I need to conver all to char * to be able to send it over the socket.
Some one has a more better approach to solve this?
Comment