您的位置:首页 > 编程语言 > C#

C# 高效Socket运行机制系列文章一 SocketAsyncEventArgs

2013-03-06 18:08 537 查看
        微软创建了SocketAsyncEventArgs类来帮助你编写可扩展、高性能socket服务器代码。

        SocketAsyncEventArgs使用网络套接字类System.Net通过异步方法完成I / O端口通讯。在以下的文章中我们会实现一个基于windows TCP/IP协议的可靠的,可扩展的Socket通讯。Microsoft 对相关技术的说明可以参考SocketAsyncEventArgs

I/O Completion Ports。

        SocketAsyncEventArgs帮助我们访问一个套接字,它有异步工作的优点,可以设置缓冲区空间、对象池,同时响应完成事件Completed event。有一个状态对象,可以通过属性访问套接字。

        这篇文章的目的是帮助你理解的基本使用SocketAsyncEventArgs类。

        对于SocketAsyncEventArgs类,微软的网站说,它需要“平台:Windows 7,Windows Vista、Windows XP SP3、Windows Server 2008、Windows Server 2003。NET Framework Client Profile框架支持:4、3.5
SP1,3.0 SP1,2.0 SP1。()。NET Framework Client Profile框架客户概要支持:4、3.5 SP1。”

Properties

 NameDescription

AcceptSocketGets or sets the socket to use or the socket created for accepting a connection with an asynchronous socket method.

BufferGets the data buffer to use with an asynchronous socket method.

BufferListGets or sets an array of data buffers to use with an asynchronous socket method.

BytesTransferredGets the number of bytes transferred in the socket operation.

ConnectByNameErrorGets the exception in the case of a connection failure when a
DnsEndPoint was used.

ConnectSocketThe created and connected
Socket object after successful completion of the
ConnectAsync method.

CountGets the maximum amount of data, in bytes, to send or receive in an asynchronous operation.

DisconnectReuseSocketGets or sets a value that specifies if socket can be reused after a disconnect operation.

LastOperationGets the type of socket operation most recently performed with this context object.

OffsetGets the offset, in bytes, into the data buffer referenced by the
Buffer property.

ReceiveMessageFromPacketInfoGets the IP address and interface of a received packet.

RemoteEndPointGets or sets the remote IP endpoint for an asynchronous operation.

SendPacketsElementsGets or sets an array of buffers to be sent for an asynchronous operation used by the

Socket.SendPacketsAsync method.

SendPacketsFlagsGets or sets a bitwise combination of
TransmitFileOptions values for an asynchronous operation used by the
Socket.SendPacketsAsync method.

SendPacketsSendSizeGets or sets the size, in bytes, of the data block used in the send operation.

SocketClientAccessPolicyProtocolObsolete. Gets or sets the protocol to use to download the socket client access policy file.

SocketErrorGets or sets the result of the asynchronous socket operation.

SocketFlagsGets the results of an asynchronous socket operation or sets the behavior of an asynchronous operation.

UserTokenGets or sets a user or application object associated with this asynchronous socket operation.
Methods

 NameDescription

DisposeReleases the unmanaged resources used by the SocketAsyncEventArgs instance and optionally disposes of the managed resources.

Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from
Object.)

FinalizeFrees resources used by the SocketAsyncEventArgs class. (Overrides
Object.Finalize().)

GetHashCodeServes as a hash function for a particular type. (Inherited from
Object.)

GetTypeGets the Type of the current instance. (Inherited from
Object.)

MemberwiseCloneCreates a shallow copy of the current
Object. (Inherited from
Object.)

OnCompletedRepresents a method that is called when an asynchronous operation completes.

SetBuffer(Int32, Int32)Sets the data buffer to use with an asynchronous socket method.

SetBuffer(Byte[], Int32, Int32)Sets the data buffer to use with an asynchronous socket method.

ToStringReturns a string that represents the current object. (Inherited from
Object.)
Events

 NameDescription

CompletedThe event used to complete an asynchronous operation.
下一扁文章我会针对TCP协议进行进一步的说明
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: