0

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.

Bert
  • 113
  • 1
  • 2
  • 8
  • Asking for opinions is off-topic on Stack Overflow. Naturally your receiving code won't work since it only asks for one part of the data and stops there. There are a lot of examples on how to properly receive data. And for how to implement multiple customers etc, that's way too broad issue to handle in one question. – Sami Kuhmonen Sep 21 '16 at 10:12
  • You are ignoring the return value of `NetworkStream.Read()`. You shouldn't expect all data to be received in a single `Read()` call because it almost always doesn't, and this is a very common mistake. You need to add [**message framing**](http://stackoverflow.com/a/37352525/3740093) so you know _how much_ to read, then you have to start checking your `bRead` variable and continue reading until you've read the sufficient amount data. – Visual Vincent Sep 22 '16 at 05:42

0 Answers0