SocketType 列舉
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定 Socket 類別的執行個體 (Instance) 所表示的通訊端 (Socket) 類型。
public enum class SocketType
public enum SocketType
type SocketType =
Public Enum SocketType
- 繼承
名稱 | 值 | Description |
---|---|---|
Dgram | 2 | 支援資料包 (Datagram),這些資料包是固定 (一般為小型) 最大長度的無連線、不可靠訊息。 訊息可能會遺失或重複而抵達的順序也可能會混亂。
Socket 類型的 Dgram 在傳送和接收資料之前並不需要先連線,並且可以與多個對等端通訊。
Dgram 會使用資料報通訊協定 ( |
Raw | 3 | 支援存取基礎傳輸通訊協定。 藉由使用 Raw,您即可使用網際網路控制訊息通訊協定 (ICMP) ( |
Rdm | 4 | 支援無連線、訊息導向、可靠傳遞的訊息,以及保留資料中的訊息界限。 Rdm (可靠傳遞的訊息,Reliably Delivered Message) 訊息不會重複且依照順序抵達。 此外,當訊息遺失時也會通知寄件人。 如果您使用 Rdm 初始化 Socket,在傳送和接收資料之前並不需要連接遠端主機。 利用 Rdm,您可以與多個對等端通訊。 |
Seqpacket | 5 | 提供跨越網路定序式位元組資料流的連接導向和可靠的雙向傳輸。 Seqpacket 並不會複製資料,但會保留資料流中的邊界。 類型為 Seqpacket 的 Socket 可與單一對等端通訊,而在可以開始通訊之前必須連接遠端主機。 |
Stream | 1 | 支援可靠、雙向、連接架構的位元組資料流,而不會導致資料重複且不需保留界限。 這個類型的 Socket 可與單一對等端通訊,而在可以開始通訊之前必須連接遠端主機。
Stream 會使用傳輸控制通訊協定 ( |
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.Dgram時System.Net.Sockets.SocketType,System.Net.Sockets.ProtocolType一律ProtocolType.Udp為 。 System.Net.Sockets.SocketType當 為 SocketType.Stream時,System.Net.Sockets.ProtocolType一律ProtocolType.Tcp為 。 如果您嘗試使用不相容的組合來建立 Socket , Socket 則會擲回 SocketException。
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |