Kep*_*boy 19
您可以使用Ping类(.NET 2.0及更高版本)
Ping x = new Ping();
PingReply reply = x.Send(IPAddress.Parse("127.0.0.1"));
if(reply.Status == IPStatus.Success)
Console.WriteLine("Address is accessible");
您可能希望在生产系统中使用异步方法以允许取消等.
GEO*_*HET 11
假设您的意思是通过TCP套接字:
IPAddress IP;
if(IPAddress.TryParse("127.0.0.1",out IP)){
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
try{
s.Connect(IPs[0], port);
}
catch(Exception ex){
// something went wrong
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息:http://msdn.microsoft.com/en-us/library/4xzx2d41.aspx?ppud = 4
声明字符串地址和int端口,您就可以通过TcpClient类进行连接了.
System.Net.Sockets.TcpClient client = new TcpClient();
try
{
client.Connect(address, port);
Console.WriteLine("Connection open, host active");
} catch (SocketException ex)
{
Console.WriteLine("Connection could not be established due to: \n" + ex.Message);
}
finally
{
client.Close();
}
Run Code Online (Sandbox Code Playgroud)