Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Codeproject,

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

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

I then copy that array to the main array:
C#
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
Updated 16-Mar-13 7:48am
v3

you can remove empty array. Your code should look like
C#
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
C#
MainArray = MainArray.Where(v => v > 0).ToArray();
will redefine your array with actual data.
 
Share this answer
 
Comments
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. */
ToSend[0] = IDs.ID_CHANGEBUFFERSIZE;

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

/* Sleep the thread to prevent bugs. */
Thread.Sleep(5);

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

System.Windows.Forms.MessageBox.Show("Updated");
}

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("127.0.0.1", 1337, 4096);
Client.UpdateBufferSize(1239);

Console.ReadLine();
}

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
http://stackoverflow.com/questions/2011214/problem-to-convert-byte-array-to-double
Yvar Birx 16-Mar-13 16:00pm    
The line is:
return BitConverter.ToInt32(b, 0);
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.
 
Share this answer
 

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