Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Codeproject,

I have a buffer that is the size of 4096.

(byte[] NewInt = new byte[4096])

The Int is written inside the NewInt, but obviously, the int is not 4096 bytes long. How would I make C# know that for instance the int stops at the 7th byte?
Posted

1 solution

Have you looked at the ArraySegment structure[^]
 
Share this answer
 
Comments
Yvar Birx 13-Mar-13 15:11pm    
Yes, I did. But I can only use the array, I am using this to tell a TCP Server or Client what to change their buffer size to, and I can't send a smaller array without making it the buffer size it already has.
OriginalGriff 13-Mar-13 15:18pm    
"I can't send a smaller array without making it the buffer size it already has."

That doesn't make a lot of sense to me - remember I cant' see your screen - perhaps if you give a little more detail?
Yvar Birx 13-Mar-13 15:23pm    
Assuming you had a TCP server and client.

You would have to create a buffer size like so:

// -- Convert the object client to a TCPClient.
TcpClient _TcpClient = (TcpClient)_Client;

// -- Create a stream.
NetworkStream _ClientStream = _TcpClient.GetStream();

// -- The message.
byte[] _Package = new byte[4096];

Now, problem is, let's say we want to send a bigger file to the client, perhaps, 12096? What we then have to do is tell the client that what we are about to send is bigger.

A -> B : I will send you a message of 12096 bytes
B -> A : Ok, I am ready
A -> B : [send those 12096 bytes]
B -> A : I got it

The way I am going to do it is by sending a byte array like so:

byte[] NewPackage = new byte[4096];
NewPackage[0] = ID_CHANGEBUFFER.

And then using the Array.Copy/ Methods to put the new Integer after the first byte. It's easy to get the Integer array out of it, but we will have the rest of the empty array standing there, and converting the byte array to an int again will be difficult because of the not used bytes in the array.

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