I am receiving a C Binary Tree as a string from a TCP Socket. I need to dump the entire C Tree to a binary file. I do not want to interact with the data, simply write it all to a file for another program to interact with. I do not know the structure of the C Tree nor do I need to; I just need to write it to a file. I have tried using File Streams, Stream Writers, Binary Writers and everything else I can think of, but as the file can contain numeric values up to 65535 I cannot load it into a byte array which seems to be the common method for storage prior to writing.
I am using C# in Visual Studio 2005.
Sam HobbsPosted May 13, 2011, 5:06 PM
byte[] ba = new byte[256];
for (int b = 0; b < 256; ++b)
ba[b] = (byte)b;
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
char[] ca = new char[256];
int n = d.GetChars(ba, 0, ba.Length, ca, 0);
for (int b = 0; b < 256; ++b)
if (ca[b] != b)
Console.WriteLine("{0} {1}", (byte)ca[b], b);
As I siad, I don't know what a SocketPacket is but I assume that SocketPacket.dataBuffer is a byte[] and if so then you already have a byte[]. I assume you could and should write from there.
Sam HobbsPosted May 16, 2011, 2:02 PM
Bert WheelerPosted May 16, 2011, 9:26 AM
Bert WheelerPosted May 13, 2011, 1:16 PM
private void OnDataReceived(IAsyncResult asyn)
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
try
{
int iRx = 0;
iRx = socketData.m_currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(socketData.dataBuffer,
0, iRx, chars, 0);
System.String szData = new System.String(chars);
string strHeader = szData.Substring(0, 4);
if (strHeader == "iFIL") // Incoming Binary file;
{
// Add the data to the strFile variable Start position is +1 as header and
// data are semi-colon seperated - iFIL;DATA_STRING
string strFile = szData.Substring(5, szData.Length - 5);
using (BinaryWriter b = new BinaryWriter(File.Open("test.bin", FileMode.Create)))
{
char[] c = strFile.ToCharArray();
int length = c.Length;
for (int count = 0; count < length; count++)
{
b.Write(Convert.ToByte((c[count])));
}
b.Flush();
b.Close();
}
}
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
Sam HobbsPosted May 13, 2011, 12:24 PM
The following also have samples. Note that they either use StreamReader or they use the NetworkStream directly. I apologize for not posting this yesterday but I assumed it was easy enough to find these.
VulpesPosted May 13, 2011, 6:19 AM
Here's some Tcp client/server code which you can play around with on a single machine:
Unless you can ascertain otherwise, I wouldn't use a BinaryReader for this because it assumes that the stream is prefixed with the number of bytes sent which it may not be. I've therefore used the networkstream's Read() method which means that you have to implement some sort of buffering scheme, because you don't know how many bytes there are to read in the transmission.
Although I've converted the byte stream to a string here for illustration (using UTF-8), you shouldn't need to do that in your application.
Bert WheelerPosted May 12, 2011, 11:26 PM
Sam HobbsPosted May 12, 2011, 1:41 PM
Bert WheelerPosted May 12, 2011, 8:11 AM
Sam HobbsPosted May 11, 2011, 5:06 PM
If possible, it would be better to simply send a list of strings and then re-create the binary tree in C# but I assume that is not an option.
Bert WheelerPosted May 11, 2011, 3:03 PM
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(strFile);
using (BinaryWriter b = new BinaryWriter(File.Open(strIncomingFile, FileMode.Append)))
{
b.Write(byteArray);
b.Flush();
b.Close();
}
Bert WheelerPosted May 11, 2011, 11:19 AM
Thank you,
Bert
Bert WheelerPosted May 7, 2011, 8:51 AM
Sam HobbsPosted May 6, 2011, 8:58 PM
VulpesPosted May 6, 2011, 6:12 AM
Sam HobbsPosted May 5, 2011, 11:57 PM
Hopefully it is as easy as Vulpes indicates to use the data as a Unicode string but I would not do that. Note that "read the data into a char array" implies conversion from 8 bits to 16 bits but don't do that; it is futile to try. It is probably not what Vulpes meant to say.
VulpesPosted May 5, 2011, 10:43 AM