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的分隔符
|
|
/// <summary>
|
/// 获取远程连接的IP地址
|
/// </summary>
|
/// <param name="tcpClient"></param>
|
/// <returns></returns>
|
public static string GetRemoteIP(this TcpClient tcpClient)
|
{
|
return tcpClient.GetRemoteIPAndPort().IP;
|
}
|
|
/// <summary>
|
/// 获取远程连接的Port
|
/// </summary>
|
/// <param name="tcpClient"></param>
|
/// <returns></returns>
|
public static int GetRemotePort(this TcpClient tcpClient)
|
{
|
return tcpClient.GetRemoteIPAndPort().Port;
|
}
|
|
/// <summary>
|
/// 获取IP和Port
|
/// </summary>
|
/// <param name="tcpClient"></param>
|
/// <returns></returns>
|
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]));
|
}
|
|
|
|
|
|
}
|
}
|