SocketAsyncEventArgs类的使用和详细说明

翻译:
说明:
我一直在寻找一种高性能的socket客户端。先前,我写过一个基于传统Socket类(BeginSend,BeginReceive,等等)的异步方式的程序。但是他没有我要的那么高效率的表现。然后,我发现一种基于事件的异步方式的方法(可以看我在MSDN杂志上,于2007年九月发布的“在.NET Freamework 3.5中获得连接”这篇文章)。
背景
由于减少了阻塞线程,异步编程模式(APM)在I / o密集型应用程序中应用很广而且很高效的。APM在.NET Freamwork第一个版的时候就被实施在里面了,而直到现在一直在改进,例如在C# 3.0中使用像lambda这种新的技术。专门为套接字编程,新模型提供了一个比较简单的编码的APM,更不用说性能优势。这是做对使用SocketAsyncEventArgs类来保持之间的上下文的I / O操作,这减少了对象分配和垃圾收集的工作。



以下是原文了:

Introduction

I was looking for a high performance code for a client socket. Previously, I wrote a code based on the traditional asynchronous programming model methods from the Socket class (BeginSendBeginReceive, and so on). But it didn't fill the performance requirements I needed. Then, I found the new model for event-based asynchronous operations (see "Get Connected with the .NET Framework 3.5" in the September 2007 issue of the MSDN Magazine).

Background

The Asynchronous Programming Model (APM) is used widely in I/O-bound applications to achieve high performance, due to the reduction of blocking threads. APM is implemented in the .NET Framework since its first version, and is improving since then, using new techniques like lambda expressions from C# 3.0. Specifically for socket programming, the new model for APM delivers an easier coding, not to mention the performance benefits. It is done towards the use of the SocketAsyncEventArgs class to keep the context between I/O operations, which reduces object allocation and the garbage collection working.

The SocketAsyncEventArgs class is also available in the .NET Framework 2.0 SP1, and the code from this article was written using Microsoft Visual Studio .NET 2008.

Using the Code

To begin with the SocketAsyncEventArgs class, I studied the example from MSDN, but there was something missing: the AsyncUserToken class. I understood the class should expose a property Socket, corresponding to the socket used to perform the I/O operations. But how to keep data received from client until the accept operation ends? Since UserToken is an Object, it could accept anything, so I created a Token class to track the accept operation. Below shown are the modified methods to use the instance of Token class as the UserToken.

// Process the accept for the socket listener.
private void ProcessAccept(SocketAsyncEventArgs e)
{
    Socket s = e.AcceptSocket;
    if (s.Connected)
    {
        try
        {
            SocketAsyncEventArgs readEventArgs = this.readWritePool.Pop();
            if (readEventArgs != null)
            {
                // Get the socket for the accepted client connection and put it into the 
                // ReadEventArg object user token.
                readEventArgs.UserToken = new Token(s, this.bufferSize);

                Interlocked.Increment(ref this.numConnectedSockets);
                Console.WriteLine("Client connection accepted. 
			There are {0} clients connected to the server",
                    this.numConnectedSockets);

                if (!s.ReceiveAsync(readEventArgs))
                {
                    this.ProcessReceive(readEventArgs);
                }
            }
            else
            {
                Console.WriteLine("There are no more available sockets to allocate.");
            }
        }
        catch (SocketException ex)
        {
            Token token = e.UserToken as Token;
            Console.WriteLine("Error when processing data received from {0}:\r\n{1}", 
			token.Connection.RemoteEndPoint, ex.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        // Accept the next connection request.
        this.StartAccept(e);
    }
}

// This method is invoked when an asynchronous receive operation completes. 
// If the remote host closed the connection, then the socket is closed.
// If data was received then the data is echoed back to the client.
private void ProcessReceive(SocketAsyncEventArgs e)
{
    // Check if the remote host closed the connection.
    if (e.BytesTransferred > 0)
    {
        if (e.SocketError == SocketError.Success)
        {
            Token token = e.UserToken as Token;
            token.SetData(e);

            Socket s = token.Connection;
            if (s.Available == 0)
            {
                // Set return buffer.
                token.ProcessData(e);
                if (!s.SendAsync(e))
                {
                    // Set the buffer to send back to the client.
                    this.ProcessSend(e);
                }
            }
            else if (!s.ReceiveAsync(e))
            {
                // Read the next block of data sent by client.
                this.ProcessReceive(e);
            }
        }
        else
        {
            this.ProcessError(e);
        }
    }
    else
    {
        this.CloseClientSocket(e);
    }
}

// This method is invoked when an asynchronous send operation completes.
// The method issues another receive on the socket to read any additional 
// data sent from the client.
private void ProcessSend(SocketAsyncEventArgs e)
{
    if (e.SocketError == SocketError.Success)
    {
        // Done echoing data back to the client.
        Token token = e.UserToken as Token;
        if (!token.Connection.ReceiveAsync(e))
        {
            // Read the next block of data send from the client.
            this.ProcessReceive(e);
        }
    }
    else
    {
        this.ProcessError(e);
    }
}

I also modified the code to show where you can manipulate the message received by the listener. In the example, I created the method ProcessData in the Token class to echoing back to the client the received message.

To control the listener lifetime, an instance of the Mutex class is used. The Start method, which is based on the original Init method creates the mutex, and the corresponding Stop method releases the mutex. These methods are suitable to implement the socket server as a Windows Service.

// Starts the server such that it is listening
// for incoming connection requests.
internal void Start(Int32 port)
{
    // Get host related information.
    IPAddress[] addressList = 
          Dns.GetHostEntry(Environment.MachineName).AddressList;
    // Get endpoint for the listener.
    IPEndPoint localEndPoint = 
          new IPEndPoint(addressList[addressList.Length - 1], port);

    // Create the socket which listens for incoming connections.
    this.listenSocket = new Socket(localEndPoint.AddressFamily, 
                        SocketType.Stream, ProtocolType.Tcp);

    if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
    {
        // Set dual-mode (IPv4 & IPv6) for the socket listener.
        // 27 is equivalent to IPV6_V6ONLY socket
        // option in the winsock snippet below,
        // based on http://blogs.msdn.com/wndp/archive/2006/10/24/
        //   creating-ip-agnostic-applications-part-2-dual-mode-sockets.aspx
        this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, 
                                         (SocketOptionName)27, false);
        this.listenSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, 
                               localEndPoint.Port));
    }
    else
    {
        // Associate the socket with the local endpoint.
        this.listenSocket.Bind(localEndPoint);
    }

    // Start the server.
    this.listenSocket.Listen(this.numConnections);

    // Post accepts on the listening socket.
    this.StartAccept(null);

    mutex.WaitOne();
}

// Stop the server.
internal void Stop()
{
    mutex.ReleaseMutex();
}

Now that we have a socket server, the next step is to create a socket client using the SocketAsyncEventArgsclass. Although the MSDN says that the class is specifically designed for network server applications, there is no restriction in using this APM in a client code. Below, there is the SocketClien class, written in this way:

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

namespace SocketAsyncClient
{
    // Implements the connection logic for the socket client.
    internal sealed class SocketClient : IDisposable
    {
        // Constants for socket operations.
        private const Int32 ReceiveOperation = 1, SendOperation = 0;

        // The socket used to send/receive messages.
        private Socket clientSocket;

        // Flag for connected socket.
        private Boolean connected = false;

        // Listener endpoint.
        private IPEndPoint hostEndPoint;

        // Signals a connection.
        private static AutoResetEvent autoConnectEvent = 
                              new AutoResetEvent(false); 

        // Signals the send/receive operation.
        private static AutoResetEvent[] 
                autoSendReceiveEvents = new AutoResetEvent[]
        {
            new AutoResetEvent(false),
            new AutoResetEvent(false)
        };

        // Create an uninitialized client instance.
        // To start the send/receive processing call the
        // Connect method followed by SendReceive method.
        internal SocketClient(String hostName, Int32 port)
        {
            // Get host related information.
            IPHostEntry host = Dns.GetHostEntry(hostName);

            // Address of the host.
            IPAddress[] addressList = host.AddressList;

            // Instantiates the endpoint and socket.
            hostEndPoint = 
              new IPEndPoint(addressList[addressList.Length - 1], port);
            clientSocket = new Socket(hostEndPoint.AddressFamily, 
                               SocketType.Stream, ProtocolType.Tcp);
        }

        // Connect to the host.
        internal void Connect()
        {
            SocketAsyncEventArgs connectArgs = new SocketAsyncEventArgs();

            connectArgs.UserToken = clientSocket;
            connectArgs.RemoteEndPoint = hostEndPoint;
            connectArgs.Completed += 
               new EventHandler<socketasynceventargs />(OnConnect);

            clientSocket.ConnectAsync(connectArgs);
            autoConnectEvent.WaitOne();

            SocketError errorCode = connectArgs.SocketError;
            if (errorCode != SocketError.Success)
            {
                throw new SocketException((Int32)errorCode);
            }
        }

        /// Disconnect from the host.
        internal void Disconnect()
        {
            clientSocket.Disconnect(false);
        }

        // Calback for connect operation
        private void OnConnect(object sender, SocketAsyncEventArgs e)
        {
            // Signals the end of connection.
            autoConnectEvent.Set();

            // Set the flag for socket connected.
            connected = (e.SocketError == SocketError.Success);
        }

        // Calback for receive operation
        private void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            // Signals the end of receive.
            autoSendReceiveEvents[SendOperation].Set();
        }

        // Calback for send operation
        private void OnSend(object sender, SocketAsyncEventArgs e)
        {
            // Signals the end of send.
            autoSendReceiveEvents[ReceiveOperation].Set();

            if (e.SocketError == SocketError.Success)
            {
                if (e.LastOperation == SocketAsyncOperation.Send)
                {
                    // Prepare receiving.
                    Socket s = e.UserToken as Socket;

                    byte[] receiveBuffer = new byte[255];
                    e.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
                    e.Completed += 
                      new EventHandler<socketasynceventargs />(OnReceive);
                    s.ReceiveAsync(e);
                }
            }
            else
            {
                ProcessError(e);
            }
        }

        // Close socket in case of failure and throws
        // a SockeException according to the SocketError.
        private void ProcessError(SocketAsyncEventArgs e)
        {
            Socket s = e.UserToken as Socket;
            if (s.Connected)
            {
                // close the socket associated with the client
                try
                {
                    s.Shutdown(SocketShutdown.Both);
                }
                catch (Exception)
                {
                    // throws if client process has already closed
                }
                finally
                {
                    if (s.Connected)
                    {
                        s.Close();
                    }
                }
            }

            // Throw the SocketException
            throw new SocketException((Int32)e.SocketError);
        }

        // Exchange a message with the host.
        internal String SendReceive(String message)
        {
            if (connected)
            {
                // Create a buffer to send.
                Byte[] sendBuffer = Encoding.ASCII.GetBytes(message);

                // Prepare arguments for send/receive operation.
                SocketAsyncEventArgs completeArgs = new SocketAsyncEventArgs();
                completeArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
                completeArgs.UserToken = clientSocket;
                completeArgs.RemoteEndPoint = hostEndPoint;
                completeArgs.Completed += 
                  new EventHandler<socketasynceventargs />(OnSend);

                // Start sending asynchronously.
                clientSocket.SendAsync(completeArgs);

                // Wait for the send/receive completed.
                AutoResetEvent.WaitAll(autoSendReceiveEvents);

                // Return data from SocketAsyncEventArgs buffer.
                return Encoding.ASCII.GetString(completeArgs.Buffer, 
                       completeArgs.Offset, completeArgs.BytesTransferred);
            }
            else
            {
                throw new SocketException((Int32)SocketError.NotConnected);
            }
        }

        #region IDisposable Members

        // Disposes the instance of SocketClient.
        public void Dispose()
        {
            autoConnectEvent.Close();
            autoSendReceiveEvents[SendOperation].Close();
            autoSendReceiveEvents[ReceiveOperation].Close();
            if (clientSocket.Connected)
            {
                clientSocket.Close();
            }
        }

        #endregion
    }
}

Points of Interest

I had an experience with a socket server running in a clustered environment. In this scenario, you can't use the first entry in the address list from the host. Instead, you should use the last address, as shown in the Start method. Another technique presented here is how to set the dual mode for an IP6 address family, which is helpful if you want to run the server in a Windows Vista or Windows Server 2008, which enables IP6 by default.

Both programs use command-line arguments to run. In the client example, you should inform "localhost" as the name of the host instead of the machine name if both the server and the client are running in a machine out of a Windows domain.

History

  • 15 January, 2008 - Original version posted
  • 28 September, 2010 - Updated article and server example

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Marcos Hidalgo Nunes

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
注:更多资料请根据压缩文件中的《更多资料.txt》文件的介绍免费获取 =====★★★★史上最全的IOCP资料大全★★★★============== 目的:研究和分享基于IOCP通讯模型的服务器端及即时通讯客户端相关技术 语言:Delphi\C++ 欢迎给位朋友加入 -------------------------前言------------------------ 最近在编写即时通讯工具,于是便参考和搜罗了网上大量的文章和源码, 对IOCP涉及的相关技术进行了广泛和深入的研究。 IOCP涉及的关键知识点有很多很多,这方面的文章也非常多, 但是很多讲述的都是某方面的,为了帮大家甄选资料,我决定分享给大家。 以下是我搜集的部分IOCP相关的资料目录,有需要的请加我QQ和QQ群,无偿分享: --------------------------IOCP部分相关知识点------------------ 线程池,Socket连接池、数据库连接池、内存池及内存管理 防DDos攻击、防只连接不发送消息及Setsockopt相关设置 WSAENOBUFS及0缓冲的WSARecive投递 优雅的链接关闭方法及shutdown、TIME_WAIT 及注册表设置:TcpNumConnections/MaxUserPort 多核多线程、生产消费者模型、读写者模型、多线程无锁环形队列及LockFreeList概念 Socket重用、端口重用 心跳、粘包、乱序 ------------------------我收集的文章及源码的部分目录---------------------- ------------------------供大家搜索资料时参考----------------------------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值