using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace IStation { public static class TcpHelper { private const string _separator = ":";//IP与Port的分隔符 /// /// 获取远程连接的IP地址 /// /// /// public static string GetRemoteIP(this TcpClient tcpClient) { return tcpClient.GetRemoteIPAndPort().IP; } /// /// 获取远程连接的Port /// /// /// public static int GetRemotePort(this TcpClient tcpClient) { return tcpClient.GetRemoteIPAndPort().Port; } /// /// 获取IP和Port /// /// /// public static TcpIPAndPort GetRemoteIPAndPort(this TcpClient tcpClient) { var ipAndPort = tcpClient.Client.RemoteEndPoint.ToString(); var arr = ipAndPort.Split(new string[] { _separator }, StringSplitOptions.RemoveEmptyEntries); return new TcpIPAndPort(arr[0],Convert.ToInt32(arr[1])); } } }