Click here to Skip to main content
13,560,107 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Codeproject,

Let's assume I have a byte array as follows:
byte[] MainArray = new byte[4096];

I then make another array out of a string:
byte[] String = Encoding.ASCII.GetBytes("hello world");

I then copy that array to the main array:
Array.Copy(String, 0, MainArray, 0, String.Length);

How would I after transfering this byte array over TCP know that it's Hello World, because when decoding it I have also decoded the rest of the MainArrays empty area.

[Edit]Code block added[/Edit]
Posted 16-Mar-13 5:34am
Updated 16-Mar-13 7:48am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

you can remove empty array. Your code should look like
byte[] MainArray = new byte[4096];
byte[] String = Encoding.ASCII.GetBytes("hello world");
Array.Copy(String, 0, MainArray, 0, String.Length);
MainArray = MainArray.Where(v => v > 0).ToArray();

the statement
MainArray = MainArray.Where(v => v > 0).ToArray();
will redefine your array with actual data.
Yvar Birx 16-Mar-13 12:58pm
Cheers, you're the best. ;3
S. M. Ahasan Habib 16-Mar-13 13:05pm
Thanks. If solution will help to solve your problem then you accept it as answer.
Yvar Birx 16-Mar-13 13:43pm
Yes I will, I only currently have a small problem, I hope you can make something out of this:

Client sends this:
public void UpdateBufferSize(int Size)
/* Create a byte array for an integer. */
byte[] ByteInt = Helper.ConvertIntToByteArray(Size);

/* Create a one byte bigger array to send. */
byte[] ToSend = new byte[BufferSize];

/* Change the first byte. */

/* Copy the int in the ToSend. */
Array.Copy(ByteInt, 0, ToSend, 1, ByteInt.Length);

/* Sleep the thread to prevent bugs. */

/* Send the package. */
if (this._TcpClient.Connected)
this._TcpClient.GetStream().Write(ToSend, 0, ToSend.Length);


Then the server does this:
/* Handle change buffer size. */
if (_Package[0] == IDs.ID_CHANGEBUFFERSIZE)
/* Create the Integer array. */
byte[] IntegerArray = new byte[_Package.Length + 1];

/* Copy the array into it. */
Array.Copy(_Package, 1, IntegerArray, 0, _Package.Length - 1);

/* Clear the array so theres only the int left. */
IntegerArray = IntegerArray.Where(v => v > 0).ToArray();

/* Update the buffer size. */
_BufferSize = (int)Helper.ConvertByteArrayToInt32(IntegerArray);

/* Show message. */
System.Windows.Forms.MessageBox.Show("Buffer has been changed to: " + _BufferSize.ToString());

Then, I boot everything up like so:
static void Main(string[] args)
AFS_Network.Network.TCPServer Server = new AFS_Network.Network.TCPServer(1337);

AFS_Network.Network.TCPClient Client = new AFS_Network.Network.TCPClient("", 1337, 4096);


And the error I get is from my byte[] to int:
public static double ConvertByteArrayToInt32(byte[] b)

return BitConverter.ToInt32(b, 0);


The error is: Destination array is not long enough to copy all the items in the collection. Check array index and length.

I hope you can help me, I am sorry if it's a bit too much.
S. M. Ahasan Habib 16-Mar-13 13:56pm
which line of code you got this error? I need to address the actual statement where it throws.
Please visit the link. It will help you
Yvar Birx 16-Mar-13 16:00pm
The line is:
return BitConverter.ToInt32(b, 0);
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Either don't send the large array - use one the size of the data you want to transfer, or prefix your actual data bytes with the number of relevant ones and only convert those at the other end.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.180527.3 | Last Updated 16 Mar 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100