socket通讯---TcpClient

IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipa = ipe.AddressList[0];
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(192.168.1.188, 3344);
client.LingerState.Enabled = false;
StreamWriter writer = new StreamWriter(client.GetStream());
string report = string.Concat(new object[] { "<D><", "", "&", 123, "&", 12, "&", 34, "&", 56, "&", GetHostIp(), "&" + comment + "></D>" });
writer.WriteLine(report);
writer.Close();
client.Close();

private static string GetHostIp()
{
string localIP = null;
IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in ipe.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
return localIP;
}

原文地址:https://www.cnblogs.com/1175429393wljblog/p/9916194.html