SocketType 列舉

定義

指定 Socket 類別的執行個體 (Instance) 所表示的通訊端 (Socket) 類型。

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

欄位

Dgram 2

支援資料包 (Datagram),這些資料包是固定 (一般為小型) 最大長度的無連線、不可靠訊息。 訊息可能會遺失或重複而抵達的順序也可能會混亂。 Socket 類型的 Dgram 在傳送和接收資料之前並不需要先連線,並且可以與多個對等端通訊。 Dgram 會使用資料包通訊協定 (ProtocolTypeUdp) 和 AddressFamily . InterNetwork address 系列。

Raw 3

支援存取基礎傳輸通訊協定。 藉由使用 Raw,您即可使用網際網路控制訊息通訊協定 (ICMP) (ProtocolType.Icmp) 和網際網路群組管理通訊協定 (IGMP) (ProtocolType.Igmp) 之類的通訊協定來通訊。 您的應用程式在傳送時必須提供完整的 IP 表頭。 接收的資料包也會傳回原來的 IP 表頭和選項。

Rdm 4

支援無連線、訊息導向、可靠傳遞的訊息,以及保留資料中的訊息界限。 Rdm (可靠傳遞的訊息,Reliably Delivered Message) 訊息不會重複且依照順序抵達。 此外,當訊息遺失時也會通知寄件人。 如果您使用 Rdm 初始化 Socket,在傳送和接收資料之前並不需要連接遠端主機。 利用 Rdm,您可以與多個對等端通訊。

Seqpacket 5

提供跨越網路定序式位元組資料流的連接導向和可靠的雙向傳輸。 Seqpacket 並不會複製資料,但會保留資料流中的邊界。 類型為 SeqpacketSocket 可與單一對等端通訊,而在可以開始通訊之前必須連接遠端主機。

Stream 1

支援可靠、雙向、連接架構的位元組資料流,而不會導致資料重複且不需保留界限。 這個類型的 Socket 可與單一對等端通訊,而在可以開始通訊之前必須連接遠端主機。 Stream 會使用傳輸控制通訊協定 (ProtocolTypeTcp) 和 AddressFamily . InterNetwork address 系列。

Unknown -1

指定未知的 Socket 類型。

範例

下列範例使用 SocketType.Stream 做為建構函式的參數 Socket

//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

備註

Socket必須先使用 AddressFamily 、、 SocketType 和 來 ProtocolType 建立資料,才能傳送和接收資料。 列舉 SocketType 提供數個選項來定義您想要開啟的 Socket 型別。

注意

SocketType 有時候會隱含地指出在 中使用哪 ProtocolType 一個 AddressFamily 。 例如,當 為 SocketType.DgramSystem.Net.Sockets.SocketTypeSystem.Net.Sockets.ProtocolType 一律 ProtocolType.Udp 為 。 System.Net.Sockets.SocketType當 為 SocketType.Stream 時, System.Net.Sockets.ProtocolType 一律 ProtocolType.Tcp 為 。 如果您嘗試使用不相容的組合來建立 SocketSocket 則會擲回 SocketException

適用於

另請參閱