SocketType 枚举

定义

指定 Socket 类的实例表示的套接字类型。Specifies the type of socket that an instance of the Socket class represents.

public enum class SocketType
public enum SocketType
type SocketType = 
Public Enum SocketType
继承
SocketType

字段

Dgram 2

支持数据报,即最大长度固定(通常很小)的无连接、不可靠消息。Supports datagrams, which are connectionless, unreliable messages of a fixed (typically small) maximum length. 消息可能会丢失或重复并可能在到达时不按顺序排列。Messages might be lost or duplicated and might arrive out of order. Socket 类型的 Dgram 在发送和接收数据之前不需要任何连接,并且可以与多个对方主机进行通信。A Socket of type Dgram requires no connection prior to sending and receiving data, and can communicate with multiple peers. Dgram 使用数据报协议 (ProtocolType.Udp) 和 AddressFamily.InterNetworkDgram uses the Datagram Protocol (ProtocolType.Udp) and the AddressFamily.InterNetwork 地址族。address family.

Raw 3

支持对基础传输协议的访问。Supports access to the underlying transport protocol. 通过使用 Raw,可以使用 Internet 控制消息协议 (ProtocolType.Icmp) 和 Internet 组管理协议 (ProtocolType.Igmp) 这样的协议来进行通信。Using Raw, you can communicate using protocols like Internet Control Message Protocol (ProtocolType.Icmp) and Internet Group Management Protocol (ProtocolType.Igmp). 在发送时,您的应用程序必须提供完整的 IP 标头。Your application must provide a complete IP header when sending. 所接收的数据报在返回时会保持其 IP 标头和选项不变。Received datagrams return with the IP header and options intact.

Rdm 4

支持无连接、面向消息、以可靠方式发送的消息,并保留数据中的消息边界。Supports connectionless, message-oriented, reliably delivered messages, and preserves message boundaries in data. RDM(以可靠方式发送的消息)消息会依次到达,不会重复。Rdm (Reliably Delivered Messages) messages arrive unduplicated and in order. 此外,如果消息丢失,将会通知发送方。Furthermore, the sender is notified if messages are lost. 如果使用 Rdm 初始化 Socket,则在发送和接收数据之前无需建立远程主机连接。If you initialize a Socket using Rdm, you do not require a remote host connection before sending and receiving data. 利用 Rdm,您可以与多个对方主机进行通信。With Rdm, you can communicate with multiple peers.

Seqpacket 5

在网络上提供排序字节流的面向连接且可靠的双向传输。Provides connection-oriented and reliable two-way transfer of ordered byte streams across a network. Seqpacket 不重复数据,它在数据流中保留边界。Seqpacket does not duplicate data, and it preserves boundaries within the data stream. Seqpacket 类型的 Socket 与单个对方主机通信,并且在通信开始之前需要建立远程主机连接。A Socket of type Seqpacket communicates with a single peer and requires a remote host connection before communication can begin.

Stream 1

支持可靠、双向、基于连接的字节流,而不重复数据,也不保留边界。Supports reliable, two-way, connection-based byte streams without the duplication of data and without preservation of boundaries. 此类型的 Socket 与单个对方主机通信,并且在通信开始之前需要建立远程主机连接。A Socket of this type communicates with a single peer and requires a remote host connection before communication can begin. Stream 使用传输控制协议 (ProtocolType.Tcp) 和 AddressFamilyInterNetworkStream uses the Transmission Control Protocol (ProtocolType.Tcp) and the AddressFamily.InterNetwork 地址族。address family.

Unknown -1

指定未知的 Socket 类型。Specifies an unknown Socket type.

示例

下面的示例使用SocketType.Stream作为Socket构造函数的参数。The following example uses SocketType.Stream as a parameter to the Socket constructor.

//Creates the Socket for sending data over TCP.
Socket^ s = gcnew Socket( AddressFamily::InterNetwork, SocketType::Stream,
   ProtocolType::Tcp );

// Connects to host using IPEndPoint.
s->Connect( EPhost );
if ( !s->Connected )
{
   strRetPage = "Unable to connect to host";
}
// Use the SelectWrite enumeration to obtain Socket status.
if ( s->Poll( -1, SelectMode::SelectWrite ) )
{
   Console::WriteLine( "This Socket is writable." );
}
else if ( s->Poll(  -1, SelectMode::SelectRead ) )
{
   Console::WriteLine( "This Socket is readable." );
}
else if ( s->Poll(  -1, SelectMode::SelectError ) )
{
   Console::WriteLine( "This Socket has an error." );
}
   //Creates the Socket for sending data over TCP.
   Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
      ProtocolType.Tcp );

   // Connects to host using IPEndPoint.
   s.Connect(EPhost);
   if (!s.Connected)
   {
      strRetPage = "Unable to connect to host";
   }
   // Use the SelectWrite enumeration to obtain Socket status.
    if(s.Poll(-1, SelectMode.SelectWrite)){
         Console.WriteLine("This Socket is writable.");
    }
    else if (s.Poll(-1, SelectMode.SelectRead)){
          Console.WriteLine("This Socket is readable." );
    }
    else if (s.Poll(-1, SelectMode.SelectError)){
         Console.WriteLine("This Socket has an error.");
    }

'Creates the Socket for sending data over TCP.
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

' Connects to host using IPEndPoint.
s.Connect(EPhost)
If Not s.Connected Then
   strRetPage = "Unable to connect to host"
End If
' Use the SelectWrite enumeration to obtain Socket status.
If s.Poll(- 1, SelectMode.SelectWrite) Then
   Console.WriteLine("This Socket is writable.")
Else
   If s.Poll(- 1, SelectMode.SelectRead) Then
      Console.WriteLine(("This Socket is readable. "))
   Else
      If s.Poll(- 1, SelectMode.SelectError) Then
         Console.WriteLine("This Socket has an error.")
      End If
   End If 
End If 

注解

必须先AddressFamily使用、SocketType和创建数据, 然后才能发送和接收数据。 ProtocolType SocketBefore a Socket can send and receive data, it must first be created using an AddressFamily, a SocketType, and a ProtocolType. 枚举提供了若干用于定义要打开的类型Socket的选项。 SocketTypeThe SocketType enumeration provides several options for defining the type of Socket that you intend to open.

备注

SocketType有时隐式指示ProtocolType AddressFamily在中使用的。SocketType sometimes implicitly indicates which ProtocolType is used within an AddressFamily. 例如, 如果System.Net.Sockets.SocketTypeSocketType.Dgram, System.Net.Sockets.ProtocolType则始终ProtocolType.Udp为。For example, when the System.Net.Sockets.SocketType is SocketType.Dgram, the System.Net.Sockets.ProtocolType is always ProtocolType.Udp. System.Net.Sockets.SocketType当为SocketType.Stream时, System.Net.Sockets.ProtocolType 始终ProtocolType.Tcp为。When the System.Net.Sockets.SocketType is SocketType.Stream, the System.Net.Sockets.ProtocolType is always ProtocolType.Tcp. 如果尝试Socket使用不兼容的组合创建, Socket将引发SocketExceptionIf you try to create a Socket with an incompatible combination, Socket throws a SocketException.

适用于

另请参阅