I am in the process of making an application that, simply put, has to be able to receive JSON files that contain BASE64 encoded files from various sources (customers / clients) so we can read them into a database at our company (server).
This JSON gets made with parameters at the customer, and of course the file that gets dropped in a folder.
This is the code I use at the moment, I know that sending the entire file at once instead of in chunks is a bad idea, but this was just to see if it works.
Client:
// Serialize the object to JSON
string json = JsonConvert.SerializeObject(sf);
// Set up the connection
try
{
TcpClient tcp = new TcpClient("127.0.0.1", "12345");
using (NetworkStream nst = tcp.GetStream())
{
byte[] dataToSend = Encoding.ASCII.GetBytes(json);
nst.Write(dataToSend, 0, dataToSend.Length);
nst.Flush();
}
tcp.Close();
Server:
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener tl = new TcpListener(ip, "12345");
TcpClient tc = tl.AcceptTcpClient();
using (NetworkStream ns = tc.GetStream())
{
byte[] buffer = new byte[tc.ReceiveBufferSize];
int bRead = ns.Read(buffer, 0, tc.ReceiveBufferSize);
json = Encoding.ASCII.GetString(buffer, 0, bRead);
sfi = JsonConvert.DeserializeObject<g.SSWFile>(json);
}
tl.Stop();
But this 'works' for small files, and in happy path scenarios where only one customer sends one file at a time it will work perfectly, but happy path scenarios never happen...
So in short: I need to find a robust way to make sure that multiple customers can interact with our server, and make sure that our server can send an 'OK' or 'ERROR' message back so the file can either be archived or put in an error directory at the customer. Also making sure that the big file sizes of the JSON will not pose a problem.
The options I've seen are:
- WCF (Duplex?): least code, least performance, streaming
- NetworkStream and TcpClient / TcpReader: More code, more performance, streaming (depending on how you code it in)
I am not an expert on WCF or network streaming, so that's why I come here to ask for your opinions.