尝试创建异步tcp服务器

时间:2014-01-29 11:59:23

标签: c# asynchronous tcplistener networkstream

我正在尝试学习如何在C#中创建异步tcp服务器,我的代码遇到了一个小问题。

I tried to modify this code

这就是我所做的:

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);
    }
}

问题是:我有一个发送50个“hello world !!”的客户端。消息到服务器,服务器只打印25/26“hello world !!”在控制台中。我使用SocketSniff查看套接字,我看到服务器套接字收到了50个“hello world !!”,所以我必须使用我的代码失败,但是什么?

你们有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

这里您没有检查Stream.Read的回复。 Read可能在缓冲区中放入了一个字节,但是您正在解码整个缓冲区,其中包含先前Read调用的剩余字节。这必须是:

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

但你遇到的问题是不同的。你正在使用... volatile结果进行同步和异步的危险组合。 您不应在BeginRead回调中发出同步阅读!而是必须致电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);

无需检查DataAvailableCanRead