- 积分
- 268
- 帖子
- N币
-
- 主题
- A币
-
- 注册时间
- 2015-1-1
- 最后登录
- 1970-1-1
|
发表于 2016-1-19 18:38:43
|
显示全部楼层
在server端第一次接收到数据时读取访客IP即可,可参考下这段代码
public class RemoteUser
{
public string remoteIp { get; set; }
public string remoteIpPort = "";
public string remotePort = "";
public bool isFirstRecieve = true;
public void OnRecieveData(object o, bool isSyn, byte[] buffers, int bufferRead)
{
bool isIp = false;
if (isFirstRecieve)
{
isFirstRecieve = false;
string datas = Encoding.UTF8.GetString(buffers);
this.remoteIpPort = datas;
if (datas.Contains(":"))
{
string[] ipPort = datas.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (ipPort.Length >= 2)
{
string ip = ipPort[0];
string port = ipPort[1];
this.remoteIp = ip;
this.remotePort = port;
Console.Write("接收到访客IP:" + ip);
isIp = true;
}
}
}
if(!isIp)
{
string datas = Encoding.UTF8.GetString(buffers);
Console.Write("tcp接收到字符串:" + datas);
}
}
} 这代码加在什么地方 |
|