vb.net - 如何在VB.NET中的服务器端显示客户端的IP地址?

标签 vb.net

我将 VB.NET 与 .NET Framework 2.0 一起使用。

我创建了一个聊天应用程序。

服务器代码

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Drawing
Imports System.IO

Public Class Form1
    Dim handler As Socket
    Public th As Thread
    Public th1 As Thread
    Public Data As String
    Dim i As Integer
    Public msg As Byte()

    Delegate Sub SetDisplay(ByVal [Text] As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.listner = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Text = "Server Started"
        Connect()
    End Sub

    Public Sub Connect()
        Try
            ipHostinfo = Dns.Resolve(Dns.GetHostName())
            ipAdd = ipHostinfo.AddressList(0)
            localEndPoint = New IPEndPoint(ipAdd, 11000) ' ip + port
            listner.Bind(localEndPoint)
            listner.Listen(10)
            th = New System.Threading.Thread(AddressOf Acceptstart)
            th.Start()
            i = 1
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub Acceptstart() 'Accept the client's request
        handler = listner.Accept()
        th1 = New System.Threading.Thread(AddressOf Receive)
        th1.Start()
        i = 2
    End Sub

    ' ---'Transfer button ' sending message ----
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            msg = Encoding.ASCII.GetBytes(Txtmsg.Text) 'getting msg from textbox
            handler.Send(msg)
            Txtmsg.Text = ""  'deleting msg from textbox after sending
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Public Sub Receive() ' to receive msg
        Try
            Dim bytes(100000) As Byte
            Dim bytesRec As Integer
            bytes = New Byte(100000) {}

A:          While True ' Always receiving msg
                bytesRec = handler.Receive(bytes) 'bytesRec is a variable to store received msg (ACK)
                If bytesRec > 0 Then 'if any msg (ACK) received the it is >0
                    Data = Encoding.ASCII.GetString(bytes, 0, bytesRec) ' storing received ACK in Data
                    Exit While
                End If
            End While


C:          Proccessdata(Data) ' to display received msg (ACK) in Listbox

            GoTo A
        Catch ex As Exception
            MessageBox.Show("Server Problem:" + ex.Message.ToString())
        End Try

    End Sub

    Public Sub Proccessdata(ByVal str As String) 'to display the received msg (ACK) in Listbox (ACK)
        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 ACK string in Listbox

        End If
    End Sub

    '' form close ' Close socket connection ----
    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If i = 1 Then
            th.Abort()
        End If
        If i = 2 Then
            th.Abort()
            th1.Abort()
        End If
        If handler IsNot Nothing Then
            handler.Shutdown(SocketShutdown.Both) 'while exiting or closing then release the socket
            handler.Close()
        End If
        Dim p1 As Process = Process.GetCurrentProcess()
        p1.Kill()
    End Sub
End Class

客户端代码

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

我想在服务器端查看客户端的IP地址。

我希望能够在服务器端显示客户端来自哪个服务器。

例如:

Server got a connection from IP address 10.10.63.75 port 35689

如果可能的话我想显示客户端机器的名称...

示例:

  • Dipankar-PC Windows 8

我怎样才能实现这个目标?

最佳答案

AcceptStart中,handlerSocket类型并且包含您需要的内容。你可以做

Dim ip As IPEndPoint = handler.RemoteEndPoint
TextMsg.Text = TextMsg.Text + ip.Address + "on port " + ip.Port + Environment.NewLine

关于vb.net - 如何在VB.NET中的服务器端显示客户端的IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29101028/

相关文章:

c++ - 我们可以像在 C++ 中一样使用十六进制字节和字符吗?

javascript - 在 JavaScript vb.net 中为 HiddenField 设置新值

vb.net 使用自定义顺序对对象列表进行排序

c# - winform应用程序的图标绘制工具有哪些?

VB.NET 2008 后台 worker

c# - Windows API 和 .net 语言

vb.net - Visual Studio 宏/任何其他遍历所有项目并设置项目属性

mysql - 如何将采购数据添加到库存以及从销售数据减去库存数据

html - 从字体到跨度(大小和颜色)和背面的正则表达式(VB.NET)

asp.net - ASPX : Placeholder not declared