//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.") If s.Poll(- 1, SelectMode.SelectRead) Then Console.WriteLine(("This Socket is readable. ")) If s.Poll(- 1, SelectMode.SelectError) Then Console.WriteLine("This Socket has an error.") End If End If End If

Socket 必须先使用 、 AddressFamily a SocketType 和 a ProtocolType 创建数据,然后才能发送和接收数据。 枚举 SocketType 提供了多个选项,用于定义要打开的类型 Socket

SocketType 有时隐式指示在 ProtocolType 一个 AddressFamily . 例如,当为 System.Net.Sockets.SocketType SocketType.Dgram 时, System.Net.Sockets.ProtocolType 始终 ProtocolType.Udp 为 。 当是 System.Net.Sockets.SocketType SocketType.Stream 时, System.Net.Sockets.ProtocolType 始终 ProtocolType.Tcp 是。 如果尝试使用不兼容的组合创建一个 Socket Socket 将引发一个 SocketException