Devnunux
Devnunux

Reputation: 21

Attempting to create an asynchronous tcp server

I'm attempting to learn how to create asynchronous tcp server in C#, and i've got a small problem with my code.

I tried to modify this code

and that's what i did :

public class ClientContext 
{
    public TcpClient client { get; set; }
    public NetworkStream stream { get; set; }
    public byte[] buffer { get; set; }
    public string message { get; set; }
    // public MemoryStream message = new MemoryStream ();

    public ClientContext () 
    {
        buffer = new byte[14];
        message = "";
    }
}

public class TCPServer
{

    public TCPServer ()
    {
        // Setup the server side
        TcpListener listener = new TcpListener (new IPEndPoint (IPAddress.Parse("127.0.0.1"), 14000));
        listener.Start ();
        // Server side ok, wait for client to connect
        listener.BeginAcceptTcpClient (OnClientAccepted, listener);

        Console.WriteLine ("Press enter to exit...");
        Console.ReadLine ();
        listener.Stop ();
    }

    private void OnClientAccepted (IAsyncResult ar)
    {
        // A client connected
        TcpListener listener = (TcpListener)ar.AsyncState;

        if (listener == null)
            return;

        try {
            // Create a new client context to store connection infos about dat client
            ClientContext context = new ClientContext ();
            context.client = listener.EndAcceptTcpClient (ar);
            context.stream = context.client.GetStream ();
            // The client is now ready, read what it has to say
            context.stream.BeginRead (context.buffer, 0, context.buffer.Length, OnClientRead, context);
        } finally {
            listener.BeginAcceptTcpClient (OnClientAccepted, listener);
        }
    }

    private void OnClientRead (IAsyncResult ar)
    {
        // The client wants to say something
        ClientContext context = (ClientContext)ar.AsyncState;
        context.message = "";

        if (context == null)
            return;

        try {
            // Read what it says
            if(context.stream.CanRead) {

                do {

                    context.stream.Read (context.buffer, 0, context.buffer.Length);
                    context.message += Encoding.ASCII.GetString (context.buffer);
                    //length -= context.message.Length;

                } while (context.stream.DataAvailable); //  && length < readBuffer.Length

                OnMessageReceived(context);
            }   

        } catch (Exception) {
            context.client.Close ();
            context.stream.Close ();
            context = null;
        } finally {
            // If we are still connected to the client, read what it has to say...
            if (context != null)
                context.stream.BeginRead (context.buffer, 0, context.buffer.Length, OnClientRead, context);
        }

    }

    private void OnMessageReceived (ClientContext context)
    {
        // Display what the client said
        Console.WriteLine ("Message reçue : " + context.message);
    }
}

The problem is : i've got a client that sends 50 "hello world !!" message to the server, and the server only print 25 / 26 "hello world !!" in the console. I've look at the socket using SocketSniff and i saw that the server socket received 50 "hello world !!", so i must have failed something with my code, but what ?

Do you guys have any idea ?

Upvotes: 2

Views: 462

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294427

context.stream.Read (context.buffer, 0, context.buffer.Length);
context.message += Encoding.ASCII.GetString (context.buffer);

Here you're not checking the return from Stream.Read. Read could had put a single byte in the buffer, but you're then decoding the entire buffer, which contains left-over bytes from previous Read calls. This must be:

int bytesRead = context.stream.Read (context.buffer, 0, context.buffer.Length);
context.message += Encoding.ASCII.GetString (context.buffer, 0, bytesRead);

But the problem you're having is different. You are doing a dangerous mix of sync and async with ... volatile results. You should not issue sync Read inside the BeginRead callback! Instead you must call EndRead:

int bytesRead = context.stream.EndRead(ar);
context.message += Encoding.ASCII.GetString (context.buffer, 0, bytesRead);
OnMessageReceived(context);
context.stream.BeginRead (context.buffer, 0, context.buffer.Length, OnClientRead, context);

There is no need to check DataAvailable nor CanRead.

Upvotes: 1

Related Questions