如何通过TcpClient取得remote ip和port
来源:互联网 发布:3d马克思软件 编辑:程序博客网 时间:2024/05/17 23:03
http://blog.csdn.net/wwwsq/article/details/1634251
在我们用TcpListener.AcceptTcpClient()接收连接之后,我们迫切需要有一个方法可以得到对方的ip和port。这样,我们才可以查看是谁从哪里连到了我们的server上。
切入点在于TcpClient有一个protected的成员Client,这是一个System.Net.Sockets.Socket类型的对象。而我们知道System.Net.Sockets.Socket对象是可以得到remote ip和port的。
我们想到了用反射(Reflection),于是有了以下代码:
Socket GetSocket(TcpClient cln)
{
PropertyInfo pi = cln.GetType().GetProperty("Client", BindingFlags.NonPublic | BindingFlags.Instance);
Socket sock = (Socket)pi.GetValue(cln, null);
return sock;
}
string GetRemoteIP(TcpClient cln)
{
string ip = GetSocket(cln).RemoteEndPoint.ToString().Split(':')[0];
return ip;
}
public int GetRemotePort(TcpClient cln)
{
string temp = GetSocket(cln).RemoteEndPoint.ToString().Split(':')[1];
int port = Convert.ToInt32(temp);
return port;
}
0 0
- [C#] 如何通过TcpClient取得remote ip和port
- 如何通过TcpClient取得remote ip和port
- Liferay portlet 如何取得客户端的IP地址(portal get remote IP)
- Liferay portlet 如何取得客户端的IP地址(portal get remote IP)
- 通过网络socket获取对方 ip 和port
- idhttp 在delphi7下如何设置代理IP和Port
- idhttp 在delphi7下如何设置代理IP和Port
- 如何取得wap和web用户的真实IP
- 转载:如何取得wap和web用户的真实IP
- TCPClient为远程和本地设置端口和IP
- 通过nonblock socket file descriptor测试IP和Port是否能建立connection的注意点
- 如何取得本机IP
- Request如何取得客户端Ip
- 如何在tomcat启动时取得port的值?
- iptables remote port forwarding
- Remote/Local port scanned
- 如何获取Client和Server自己的还有对方的IP/port
- JAVA获取本地IP和PORT
- 只在某一函数内使用#define
- JS实现新打开网页最大化or全屏显示
- 系统下实现电脑服务器自动重启
- Nginx安装部署
- 十大编程算法助程序员走上高手之路
- 如何通过TcpClient取得remote ip和port
- Android UI线程和非UI线程
- HTML5技术学习总结
- 类访问权限
- vim常用命令
- android volley使用技巧
- myeclipse快捷键
- 贪心:POJ2376
- iOS 多个按钮,选中其中一个其他按钮不可选