Socket获取客户端IP地址及端口号

来源:互联网 发布:php调用接口post请求 编辑:程序博客网 时间:2024/05/19 00:16
 

 

Socket类包含一些非常有用的属性,这些属性允许确定关于正在连接的客户端的信息。其中关键信息之一是返回EndPoint对象的RemoteEndPoint属性。EndPoint类本身是抽象的,要想收集有用的信息,需·要把EndPoint强制类型转换为IPEndPoint对象。IPEndPoint类包含远程客户端的IP地址和端口号。具体用法如下:

 
  s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '''使用TCP协议        Dim localEndPoint As New IPEndPoint(IPAddress.Parse("192.168.0.52"), 1024) '''指定IP和Port        s.Bind(localEndPoint)    '''绑定到该Socket        s.Listen(100)  '''侦听,最多接受100个连接 While (True)            Dim bytes(1024) As Byte '''用来存储接收到的字节            Dim ss As Socket = s.Accept() '''若接收到,则创建一个新的Socket与之连接            ' Dim ipstr = (IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))            ss.Receive(bytes)  '''接收数据,若用ss.send(Byte()),则发送数              Dim ss As Socket = s.Accept()             IPstr1= CType(ss.RemoteEndPoint, IPEndPoint).Address.ToString()           Portstr1= CType(ss.RemoteEndPoint, IPEndPoint).Port.ToString()   End While

原创粉丝点击