Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
Yvar Birx2.4K
Edited 16-Mar-13 7:48am
ProgramFOX124.5K
v3
Rate this: bad
good
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.
  Permalink  
Comments
Yvar Birx at 16-Mar-13 12:58pm
   
Cheers, you're the best. ;3
S. M. Ahasan Habib at 16-Mar-13 13:05pm
   
Thanks. If solution will help to solve your problem then you accept it as answer.
Yvar Birx at 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 at 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 at 16-Mar-13 16:00pm
   
The line is:
return BitConverter.ToInt32(b, 0);
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 424
1 CHill60 410
2 OriginalGriff 355
3 PIEBALDconsult 245
4 BillWoodruff 208


Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 16 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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