Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have small c# server
C#
while (clientSocket.Connected)
            {
                byte[] streamBuffer = new byte[4];
                int streamLenght = this.clientStream.Read(streamBuffer, 0, 4);
                if (streamLenght == 0) break;
                int sizeofnewdata = BitConverter.ToInt32(streamBuffer, 0);
                streamBuffer = new byte[sizeofnewdata];
                streamLenght = this.clientStream.Read(streamBuffer, 0, sizeofnewdata);
                if (streamLenght == 0) break;
            }


Idea is to use first 4 bytes as an size header for new data that will arrive.
Problem is, client is being written in C++.

What would be easiest way to send first just 4 bytes using send() in c++ so I can decode them via BitConverter.ToInt32 in C#?

EDIT:
Question is how to convent sizeof() to unsigned char and then send that unsigned char over socket so i can decode it as Int32

Also, client is not using .NET framework.

Thank you in advance
Filip
Posted
Updated 2-Nov-10 1:25am
v4

1 solution

newzion wrote:
What would be easiest way to send first just 4 bytes using send() in c++

Using the send[^] function, of course.
:)
 
Share this answer
 
Comments
newzion 2-Nov-10 7:23am    
Haha, now I get I didn't ask what I really wanted. Question is how to convent sizeof() to unsigned char and then send that unsigned char over socket
CPallini 2-Nov-10 7:57am    
If you need to send an 'Int32' compatible tipe, just use it, for instance:
__int32 data_size = sizeof(may_data);
send( mysocket, &data_size, (int) sizeof(data_size), 0);
«_Superman_» 2-Nov-10 13:52pm    
You could simply put 4 instead of using sizeof and then typecasting it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900