I'm trying to write out a Byte[] array representing a complete file to a file. This file may have a .csv or .db extension.
I have set up a synchronous client and server application based on MSDN Synchronous Sockets
After searching through S.O I discovered this (Can a Byte[] Array be written to a file in C#?) post which seems to indicate that it is possible to write a byte array to a file by using:
File.WriteAllBytes(string path, byte[] bytes)
My problem is that this method seems to work for .csv files but not for .db files.
Why is this the case?
Some code below.
//Client-side
public static void StartClient()
{
byte[] bytes = new byte[9000];
try
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
Socket sender = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(remoteEP);
byte[] msg= File.ReadAllBytes("CsvOrDbPath");
int bytesSent = sender.Send(msg);
int bytesRec = sender.Receive(bytes);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se) {
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
//Server-side
public static string data = null;
public static void StartListening()
{
byte[] bytes = new byte[9000];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
Socket handler = listener.Accept();
data = null;
while (true)
{
int bytesRec = handler.Receive(bytes);
int expecting = 8; //expected number of bytes
string path = "CSVorDbPath";
// Need to encode back into .db file format somehow
// Works for csv
File.WriteAllBytes(path, bytes);
break;
}
//Echo the data back to the client.
//byte[] msg = Encoding.ASCII.GetBytes(data);
//handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
When I use a .csv file this procedure works to an extent and I receive the same file on my server machine. When I use a .db file the procedure generates the .db file but it is not openable.
I suspect the issue is due to me not encoding the .db data into it's appropriate format - please can someone explain what is going on?