【问题标题】:C# Async Socket Server - BeginReceive in CallbackC# 异步套接字服务器 - 在回调中开始接收
【发布时间】:2018-11-19 20:31:18
【问题描述】:

我有一个用 C# 编写的简单异步套接字服务器(几乎是 Microsoft 的 example),但是这个示例的问题是它只接受来自客户端的一条消息然后关闭。我希望这台服务器在收到任何消息后保持活跃。

这个问题在here 之前被问过,答案& cmets 解释说解决这个问题只是在SendCallback 函数中调用handler.beginReceive,但是要做到这一点需要传入一个state 变量.这是我不确定使用异步编程做的事情,因为我对它很陌生。

通过下面的示例,我如何将 state 对象从 Send 函数传送到 SendCallback 函数?

服务器代码:

// Asynchronous Server Socket Example
// http://msdn.microsoft.com/en-us/library/fx6588te.aspx

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

// State object for reading client data asynchronously
public class StateObject
{
    // Client  socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 1024;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}

public class GetState
{

}

public class AsynchronousSocketListener
{
    // Thread signal.
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    public AsynchronousSocketListener()
    {
    }

    public static void StartListening()
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        // The DNS name of the computer
        // running the listener is "host.contoso.com".
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // Create a TCP/IP socket.
        Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections.
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection...");
                listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                // Wait until a connection is made before continuing.
                allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }

    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
    }

    public static void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;

        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer, 0, bytesRead));

            // Check for end-of-file tag. If it is not there, read 
            // more data.
            content = state.sb.ToString();
            if (content != null)
            {
                // All the data has been read from the 
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                    content.Length, content);
                // Echo the data back to the client.
                Send(handler, content, state);
            }
            else
            {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
            }
        }
    }

    private static void Send(Socket handler, String data, StateObject state)
    {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket handler = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            /// ******* WIP ******** //
            //handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
            //handler.Shutdown(SocketShutdown.Both);
            //handler.Close();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }


    public static int Main(String[] args)
    {
        StartListening();
        return 0;
    }
}

【问题讨论】:

    标签: c# sockets asynchronous server


    【解决方案1】:

    我会这样做:

    public class AsynchronousSocketServer
    {
        private Socket listener;
        private byte[] buffer = new byte[8192]; // Buffer to store data from clients.
    
        public void StartListening()
        {
            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener.Bind(new IPEndPoint(localIPAddress, listeningPort));
            listener.Listen(20);
            listener.BeginAccept(OnSocketAccepted, null);
        }
    
        private void OnSocketAccepted(IAsyncResult result)
        {
            // This is the client socket, where you send/receive data from after accepting. Keep it in a List<Socket> collection if you need to.
            Socket client = listener.EndAccept(result);
    
            // Pass in the client socket as the state object, so you can access it in the callback.
            client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnDataReceived, client); // Start receiving data from this client.
            listener.BeginAccept(OnSocketAccepted, null); // Start a new async accept operation to accept incoming connections from other clients.
        }
    
        private void OnDataReceived(IAsyncResult result)
        {
            // This is the client that sent you data. AsyncState is exactly what you passed into the state parameter in BeginReceive
            Socket client = result.AsyncState as Socket;
            int received = client.EndReceive(result);
    
            // Handle received data in buffer, send reply to client etc...
    
            // Start a new async receive on the client to receive more data.
            client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnDataReceived, client);
        }
    }
    

    如您所见,它包含大量递归回调。基本思想是您调用BeginAccept 开始接受第一个连接,然后一旦您的回调触发您接受与EndAccept 的连接并开始一个新的BeginAccept 以开始接受更多连接。当您开始与客户端通信时,同样的逻辑适用于BeginReceiveEndReceive。使用这种逻辑,服务器将不断地接受传入的连接,客户端也将能够不断地向服务器发送数据。

    在您的示例中,您从 AsyncState 获得了 listener,但您没有再次调用 BeginAccept 来接受更多传入连接,这可能解释了为什么您的服务器只接受 1 个连接并关闭。

    对于BeginSend 中的state 参数,您实际上可以将您的BeginReceive 放在您的Send() 方法之后以省去麻烦:

    Send(handler, content, state);
    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
    

    【讨论】:

      【解决方案2】:

      最好只启动一个后台线程并使用阻塞调用。

      这是一个使用 TcpListener 和 NetworkStream 的 TcpServer

      public class TcpServer
      {
          public void Run(string address, int port)
          {
              var listener = new TcpListener(IPAddress.Parse(address), port);
              listener.Start();
              while (true)
              {
                  TcpClient tcpclient = null;
                  NetworkStream netstream = null;
                  try
                  {
                      tcpclient = listener.AcceptTcpClient();
                      Console.WriteLine("Client connected from " + tcpclient.Client.LocalEndPoint.ToString());
                      netstream = tcpclient.GetStream();
                      var responsewriter = new StreamWriter(netstream) { AutoFlush = true };
                      while (true)
                      {
                          if (IsDisconnected(tcpclient))
                              throw new Exception("Client disconnected gracefully");
                          if (netstream.DataAvailable)             // handle scenario where client is not done yet, and DataAvailable is false. This is not part of the tcp protocol.
                          {
                              string request = Read(netstream);
                              Console.WriteLine("Client sent: " + request);
                              responsewriter.Write("You sent: " + request);
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      netstream.Close();
                      tcpclient.Close();
                      Console.WriteLine(ex.Message);
                  }
              }
          }
      
          private bool IsDisconnected(TcpClient tcp)
          {
              if (tcp.Client.Poll(0, SelectMode.SelectRead))
              {
                  byte[] buff = new byte[1];
                  if (tcp.Client.Receive(buff, SocketFlags.Peek) == 0)
                      return true;
              }
              return false;
          }
      
          private string Read(NetworkStream netstream)
          {
              byte[] buffer = new byte[1024];
              int dataread = netstream.Read(buffer, 0, buffer.Length);
              string stringread = Encoding.UTF8.GetString(buffer, 0, dataread);
              return stringread;
          }
      }
      

      运行它

      static void Main()
      {
          ThreadPool.QueueUserWorkItem(w => {
              var asyncserver = new TcpServer();
              asyncserver.Run("192.168.0.7", 5055); // or whatever your local IP Address is
          });
          Console.WriteLine("Press [Enter] to quit");
          Console.ReadLine();
      }
      

      【讨论】:

      • 感谢您的意见。您能否详细说明为什么您建议的方法更好?
      • 这是一个偏好问题,但我认为 Begin.. 方法和您必须使用 state 参数和 ManualResetEvent 编写的代码有些丑陋,而不是我想要维护的东西
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      相关资源
      最近更新 更多