试试这个。
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetNetworkRequirement(NetworkSelectionCharacteristics.NonCellular);
string serverName = "www.google.com";
int portNumber = 80;
DnsEndPoint hostEntry = new DnsEndPoint(serverName, portNumber);
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = socket;
socketEventArg.Completed += socketEventArg_Completed;
// Make an asynchronous Connect request over the socket.
socket.ConnectAsync(socketEventArg);
void socketEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
Socket socket = e.UserToken as Socket;
if (e.SocketError == SocketError.Success)
{
}
}