socket通讯---TcpClient

版权声明:本文为博主原创文章,博主的辛苦付出希望大家尊重,未经博主允许不得抄袭,转载请标明出处。 https://blog.csdn.net/LongtengGensSupreme/article/details/83789928

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;
}

猜你喜欢

转载自blog.csdn.net/LongtengGensSupreme/article/details/83789928