共用方式為


Socket 建構函式

定義

初始化 Socket 類別的新執行個體。

多載

名稱 Description
Socket(SafeSocketHandle)

初始化該類別的新實例 Socket ,以啟用指定的套接字句柄。

Socket(SocketInformation)

使用從 返回DuplicateAndClose(Int32)的指定值初始化該類別的新Socket實例。

Socket(SocketType, ProtocolType)

使用指定的套接字類型與協定初始化該類別的新 Socket 實例。 若作業系統支援 IPv6,此建構子會產生雙模式套接字;否則,它會建立一個 IPv4 插槽。

Socket(AddressFamily, SocketType, ProtocolType)

使用指定的位址族、套接字類型與協定初始化該類別的新 Socket 實例。

Socket(SafeSocketHandle)

來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs

初始化該類別的新實例 Socket ,以啟用指定的套接字句柄。

public:
 Socket(System::Net::Sockets::SafeSocketHandle ^ handle);
public Socket(System.Net.Sockets.SafeSocketHandle handle);
new System.Net.Sockets.Socket : System.Net.Sockets.SafeSocketHandle -> System.Net.Sockets.Socket
Public Sub New (handle As SafeSocketHandle)

參數

handle
SafeSocketHandle

物件將要封裝的套筒 Socket 的套筒把手。

例外狀況

handlenull

handle 無效。

handle 不是套接字,或無法存取有關套接字的資訊。

備註

此方法會將從所提供SafeSocketHandle的資料填充實Socket例。 不同作業系統對於查詢 socket handle 或檔案描述符的屬性與設定,提供不同程度的支援。 結果 Socket 實例中某些公開 API 可能因作業系統而異,例如 ProtocolTypeBlocking

適用於

Socket(SocketInformation)

來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs

使用從 返回DuplicateAndClose(Int32)的指定值初始化該類別的新Socket實例。

public:
 Socket(System::Net::Sockets::SocketInformation socketInformation);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public Socket(System.Net.Sockets.SocketInformation socketInformation);
public Socket(System.Net.Sockets.SocketInformation socketInformation);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
new System.Net.Sockets.Socket : System.Net.Sockets.SocketInformation -> System.Net.Sockets.Socket
new System.Net.Sockets.Socket : System.Net.Sockets.SocketInformation -> System.Net.Sockets.Socket
Public Sub New (socketInformation As SocketInformation)

參數

socketInformation
SocketInformation

套接字資訊由 返回。DuplicateAndClose(Int32)

屬性

備註

如果你多次呼叫 Socket 建構子,每次呼叫的參數都是相同的位元組陣列,你會建立多個擁有相同底層套接字的受管理 Sockets。 這種做法是強烈不建議的。

適用於

Socket(SocketType, ProtocolType)

來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs

使用指定的套接字類型與協定初始化該類別的新 Socket 實例。 若作業系統支援 IPv6,此建構子會產生雙模式套接字;否則,它會建立一個 IPv4 插槽。

public:
 Socket(System::Net::Sockets::SocketType socketType, System::Net::Sockets::ProtocolType protocolType);
public Socket(System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType);
new System.Net.Sockets.Socket : System.Net.Sockets.SocketType * System.Net.Sockets.ProtocolType -> System.Net.Sockets.Socket
Public Sub New (socketType As SocketType, protocolType As ProtocolType)

參數

socketType
SocketType

這是其中一項 SocketType 價值。

protocolType
ProtocolType

這是其中一項 ProtocolType 價值。

例外狀況

與 的socketTypeprotocolType組合會導致無效的套接字。

備註

參數 socketType 指定類別的類型 SocketprotocolType 參數則指定所 Socket使用的協定。 這兩個參數並非獨立。 通常 Socket 這種類型是協議中隱含的。 如果型別與協定型別的組合 Socket 產生無效 Socket,此建構子會拋出一個 SocketException

備註

如果這個建構子拋出 , SocketException請使用該 SocketException.ErrorCode 屬性來取得特定的錯誤碼。 取得此程式碼後,請參閱 Windows Sockets 版本 2 API 錯誤代碼 文件,以獲得錯誤的詳細說明。

備註

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱 .NET Framework 中的網路追蹤

適用於

Socket(AddressFamily, SocketType, ProtocolType)

來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs
來源:
Socket.cs

使用指定的位址族、套接字類型與協定初始化該類別的新 Socket 實例。

public:
 Socket(System::Net::Sockets::AddressFamily addressFamily, System::Net::Sockets::SocketType socketType, System::Net::Sockets::ProtocolType protocolType);
public Socket(System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType);
new System.Net.Sockets.Socket : System.Net.Sockets.AddressFamily * System.Net.Sockets.SocketType * System.Net.Sockets.ProtocolType -> System.Net.Sockets.Socket
Public Sub New (addressFamily As AddressFamily, socketType As SocketType, protocolType As ProtocolType)

參數

addressFamily
AddressFamily

這是其中一項 AddressFamily 價值。

socketType
SocketType

這是其中一項 SocketType 價值。

protocolType
ProtocolType

這是其中一項 ProtocolType 價值。

例外狀況

addressFamily組合 、 socketTypeprotocolType 會產生無效的套接字。

範例

下列程式代碼範例示範如何建立 類別的 Socket 實例。

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

public class Sample
{

  public static string DoSocketGet(string server)
  {
    //Set up variables and String to write to the server.
    Encoding ASCII = Encoding.ASCII;
    string Get = "GET / HTTP/1.1\r\nHost: " + server +
                 "\r\nConnection: Close\r\n\r\n";
    Byte[] ByteGet = ASCII.GetBytes(Get);
    Byte[] RecvBytes = new Byte[256];
    String strRetPage = null;

    // IPAddress and IPEndPoint represent the endpoint that will
    //   receive the request.
    // Get first IPAddress in list return by DNS.

    try
    {

      // Define those variables to be evaluated in the next for loop and
      // then used to connect to the server. These variables are defined
      // outside the for loop to make them accessible there after.
      Socket s = null;
      IPEndPoint hostEndPoint;
      IPAddress hostAddress = null;
      int conPort = 80;

      // Get DNS host information.
      IPHostEntry hostInfo = Dns.GetHostEntry(server);
      // Get the DNS IP addresses associated with the host.
      IPAddress[] IPaddresses = hostInfo.AddressList;

      // Evaluate the socket and receiving host IPAddress and IPEndPoint.
      for (int index=0; index<IPaddresses.Length; index++)
      {
        hostAddress = IPaddresses[index];
        hostEndPoint = new IPEndPoint(hostAddress, conPort);


        // Creates the Socket to send data over a TCP connection.
        s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );


        // Connect to the host using its IPEndPoint.
        s.Connect(hostEndPoint);

        if (!s.Connected)
        {
          // Connection failed, try next IPaddress.
          strRetPage = "Unable to connect to host";
          s = null;
          continue;
        }

        // Sent the GET request to the host.
        s.Send(ByteGet, ByteGet.Length, 0);

      } // End of the for loop.


      // Receive the host home page content and loop until all the data is received.
      Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
      strRetPage = "Default HTML page on " + server + ":\r\n";
      strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

      while (bytes > 0)
      {
        bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
        strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
      }

    } // End of the try block.

    catch(SocketException e)
    {
      Console.WriteLine("SocketException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
    catch(ArgumentNullException e)
    {
      Console.WriteLine("ArgumentNullException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
    catch(NullReferenceException e)
    {
      Console.WriteLine("NullReferenceException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
    catch(Exception e)
    {
      Console.WriteLine("Exception caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

    return strRetPage;
}
   public static void Main()
   {
      Console.WriteLine(DoSocketGet("localhost"));
   }
 }
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Net.Sockets

 _

Public Class Sample
   
   
   Public Shared Function DoSocketGet(server As String) As String
      'Set up variables and String to write to the server.
    Dim ASCII As Encoding = Encoding.ASCII
    Dim [Get] As String = "GET / HTTP/1.1" + ControlChars.Lf + ControlChars.NewLine + "Host: " + server + ControlChars.Lf + ControlChars.NewLine + "Connection: Close" + ControlChars.Lf + ControlChars.NewLine + ControlChars.Lf + ControlChars.NewLine
    Dim ByteGet As [Byte]() = ASCII.GetBytes([Get])
    Dim RecvBytes(256) As [Byte]
    Dim strRetPage As [String] = Nothing


      
      ' IPAddress and IPEndPoint represent the endpoint that will
      '   receive the request.
      ' Get first IPAddress in list return by DNS.
      Try

 

         ' Define those variables to be evaluated in the next for loop and 
         ' then used to connect to the server. These variables are defined
         ' outside the for loop to make them accessible there after.
         Dim s As Socket = Nothing
         Dim hostEndPoint As IPEndPoint
         Dim hostAddress As IPAddress = Nothing
         Dim conPort As Integer = 80
         
         ' Get DNS host information.
         Dim hostInfo As IPHostEntry = Dns.Resolve(server)
         ' Get the DNS IP addresses associated with the host.
         Dim IPaddresses As IPAddress() = hostInfo.AddressList
         
         ' Evaluate the socket and receiving host IPAddress and IPEndPoint. 
      Dim index As Integer = 0
      For index = 0 To IPaddresses.Length - 1
        hostAddress = IPaddresses(index)
        hostEndPoint = New IPEndPoint(hostAddress, conPort)


        ' Creates the Socket to send data over a TCP connection.
        s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)


        ' Connect to the host using its IPEndPoint.
        s.Connect(hostEndPoint)

        If Not s.Connected Then
          ' Connection failed, try next IPaddress.
          strRetPage = "Unable to connect to host"
          s = Nothing
          GoTo ContinueFor1
        End If


        ' Sent the GET request to the host.
        s.Send(ByteGet, ByteGet.Length, 0)


ContinueFor1:
      Next index  ' End of the for loop.
      



      ' Receive the host home page content and loop until all the data is received.

      'Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
      Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)

      strRetPage = "Default HTML page on " + server + ":\r\n"
      strRetPage = "Default HTML page on " + server + ":" + ControlChars.Lf + ControlChars.NewLine

      Dim i As Integer

      While bytes > 0

        bytes = s.Receive(RecvBytes, RecvBytes.Length, 0)

        strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)

      End While


      ' End of the try block.
    Catch e As SocketException
         Console.WriteLine("SocketException caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      Catch e As ArgumentNullException
         Console.WriteLine("ArgumentNullException caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      Catch e As NullReferenceException
         Console.WriteLine("NullReferenceException caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      Catch e As Exception
         Console.WriteLine("Exception caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      End Try
      
      Return strRetPage
   End Function 'DoSocketGet
    
   Public Shared Sub Main()
    Console.WriteLine(DoSocketGet("localhost"))
   End Sub
End Class

備註

參數 addressFamily 指定類別所使用的位址方案 SocketsocketType 參數指定類別的型別 SocketprotocolType 參數則指定所 Socket使用的協定。 這三個參數並非獨立。 有些位址家族會限制可與之搭配使用的協定,且通常 Socket 協定中隱含了該類型。 如果位址族、 Socket 類型和協定類型的組合產生無效 Socket,則此構造子會拋出一個 SocketException

備註

如果這個建構子拋出 , SocketException請使用該 SocketException.ErrorCode 屬性來取得特定的錯誤碼。 取得此程式碼後,請參閱 Windows Sockets 版本 2 API 錯誤代碼 文件,以獲得錯誤的詳細說明。

備註

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱 .NET Framework 中的網路追蹤

另請參閱

適用於