0

Currently developing a simple message server using sockets and sometimes the TCPClient receives the correct number of bytes but each byte is 0.

Here's the sender code.

        try
        {
            //c.clientSocket.NoDelay = true;

            // Send back an OK                    
            var clientStream = c.clientSocket.GetStream();                

            var Response = JsonConvert.SerializeObject(new Packet("SERVER", c.ClientName, new List<Payload>() { new Payload(MessageLibrary.Commands.OK, null) }));

            var msg = System.Text.Encoding.ASCII.GetBytes(Response);

            clientStream.Write(msg, 0, msg.Length);                
        }
        catch (Exception ex)
        {
            if (ExceptionRaised != null)
                ExceptionRaised(c.ClientName, ex);
        }

Response = "{\"TimeStamp\":\"2016-03-18T08:15:15.0881326+00:00\",\"Sender\":\"SERVER\",\"Payload\":[{\"Command\":\"OK\",\"CommandValue\":\"\"}],\"Destination\":\"GBCO0101\"}"

Msg contains 139 bytes

So this seems ok, here is the receiving code.

    static void OnDataRecieved(IAsyncResult result)
    {
        TcpClient client = result.AsyncState as TcpClient;

        // Get a stream object for reading and writing
        try
        {
            NetworkStream stream = client.GetStream();

            int ReadBytes = stream.EndRead(result);

            if (ReadBytes == 0)
            {
                // Client gone
                Console.WriteLine("Server lost");
            }
            else
            {
                // Translate data bytes to a ASCII string.
                var data = System.Text.Encoding.ASCII.GetString(ClientReadBuffer, 0, ReadBytes);

                ClientReadBuffer = new byte[ClientReadBuffer.Length];

                stream.BeginRead(ClientReadBuffer, 0, ClientReadBuffer.Length, new AsyncCallback(OnDataRecieved), client);

                ProcessData(data);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("lost connection");
            Console.WriteLine(ex.Message);
        }
    }

If I take a look at ProcessData(data); I can see that data = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

ReadBytes = 139

So the right amount of bytes seems to be correct but the data itself is wrong. What can cause this?

Gaz83
  • 2,293
  • 4
  • 32
  • 57
  • Probably that is what the server really sends? – Ian Mar 18 '16 at 08:31
  • @Ian then why is it doing that? – Gaz83 Mar 18 '16 at 08:32
  • 1
    Impossible. Are you really using `ClientReadBuffer` on the first `stream.BeginRead()` (it's not included in the code above)? And why do you create a new instance of it for every read? Waste of resources. Just reuse it. – jgauffin Mar 18 '16 at 08:33
  • @jgauffin Its amazing how a simple comment can solve all the problems. To answer your question, yes I was doing that. BUT, for some reason I was doing a `stream.BeginRead` in another part of the program which did not reset the `ClientReadBuffer`. Took that line out and all is working. – Gaz83 Mar 18 '16 at 08:41
  • @Gaz83: Great! I added it as an answer :) – jgauffin Mar 18 '16 at 08:48

1 Answers1

1

It's unlikely.

Are you really using ClientReadBuffer on the first stream.BeginRead() (it's not included in the code above)? You probably have one somewhere that doesn't do the read in the same way.

And why do you create a new instance of it for every read? Waste of resources. Just reuse it.

Another thing is that TCP is stream based. Don't expect the bytes received to match the buffer that you sent. See this question for instance.

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • [...or this one](http://stackoverflow.com/a/35240061/3740093) (though it's in VB.NET but the classes can be compiled to a DLL) – Visual Vincent Mar 19 '16 at 10:21