如何向客户展示';VB.NET中服务器端的IP地址?
我正在使用VB.NET和.NET Framework 2.0 我创建了一个聊天应用程序 服务器代码 客户端代码 我想在服务器端查看客户端的IP地址 我希望能够显示客户端位于服务器端的服务器 例如: 服务器从IP地址10.10.63.75端口35689获得连接 如果可能的话,我想显示客户机的名称 例如:如何向客户展示';VB.NET中服务器端的IP地址?,vb.net,Vb.net,我正在使用VB.NET和.NET Framework 2.0 我创建了一个聊天应用程序 服务器代码 客户端代码 我想在服务器端查看客户端的IP地址 我希望能够显示客户端位于服务器端的服务器 例如: 服务器从IP地址10.10.63.75端口35689获得连接 如果可能的话,我想显示客户机的名称 例如: Dipankar PC Windows 8 如何实现这一点?在AcceptStart中,handler属于Socket类型,包含您需要的内容。你能行 Dim ip As IPEndPoint
- Dipankar PC Windows 8
如何实现这一点?在
AcceptStart
中,handler
属于Socket
类型,包含您需要的内容。你能行
Dim ip As IPEndPoint = handler.RemoteEndPoint
TextMsg.Text = TextMsg.Text + ip.Address + "on port " + ip.Port + Environment.NewLine
Dim ip As IPEndPoint=handler.RemoteEndPoint TextBox1.Text=TextBox1.Text+ip.Address.ToString()+“on port”+ip.port.ToString()+Environment.NewLine
工作正常。。。非常感谢您能显示客户的计算机名吗?例如-我的计算机名是Dipankar PC是否可以在服务器端显示?如果您的计算机名已在网络的DNS中注册,您可以通过类似于Dim host的方式获得它,即IPHostEntry=DNS.GetHostEntry(“192.168.99.201”)
。
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text.Encoder
Public Class Conn
Public msg As Byte()
Public th As Thread
Public Data As String
Dim i As Integer
Delegate Sub SetDisplay(ByVal [Text] As String)
Private Sub Conn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.sender = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
End Sub
'--------Connect button --------
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Connect()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub Connect()
Try
ipHostinfo = Dns.Resolve(Txtserver.Text) ' Server ip from textbox
ipAdd = ipHostinfo.AddressList(0)
remoteEP = New IPEndPoint(ipAdd, 11000) ' ip + port
sender.Connect(remoteEP)
th = New System.Threading.Thread(AddressOf Receive)
th.Start()
i = 1
Button1.Text = "Connected"
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'--------------------Send ACK button----------------
Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
msg = Encoding.ASCII.GetBytes(Txtmsg.Text) 'sending ACK from textbox
Me.sender.Send(msg)
Txtmsg.Text = "" ' deleting from textbox
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub Receive() ' to receive msg
Dim bytes(100000) As Byte
Dim bytesRec As Integer
A: While True ' Always receiving msg
bytes = New Byte(100000) {}
bytesRec = sender.Receive(bytes) 'bytesRec is a integer variable to store received msg number of bytes as integer
If bytesRec > 0 Then 'if any msg (ACK) received the it is >0
Data = Encoding.ASCII.GetString(bytes, 0, bytesRec)
Exit While
End If
End While
Proccessdata(Data) ' to display received msg in Listbox
GoTo A
End Sub
Public Sub Proccessdata(ByVal str As String) ' received msg from server
If Me.List.InvokeRequired Then
Dim d As New SetDisplay(AddressOf Proccessdata)
Me.Invoke(d, New Object() {str})
Else
Me.List.Items.Add(str) 'displaying received msg string in Listbox
End If
End Sub
Private Sub Conn_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If i = 1 Then
th.Abort()
Me.sender.Shutdown(SocketShutdown.Both)
Me.sender.Close()
End If
Dim p1 As Process
Dim p As Process() = Process.GetProcessesByName("client.exe")
For Each p1 In p
p1.Kill()
Next
End Sub
End Class
Dim ip As IPEndPoint = handler.RemoteEndPoint
TextMsg.Text = TextMsg.Text + ip.Address + "on port " + ip.Port + Environment.NewLine