title: How to show the client's IP address in server side in VB.NET?
body: <p>I'm using VB.NET with .NET Framework 2.0.</p>
  
  <p>I created one chat application.</p>
  
  <h3>Server code</h3>
  
  <pre><code>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 &amp;gt; 0 Then 'if any msg (ACK) received the it is &amp;gt;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
  </code></pre>
  
  <h3>Client code</h3>
  
  <pre><code>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 &amp;gt; 0 Then 'if any msg (ACK) received the it is &amp;gt;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
  </code></pre>
  
  <p>I want to see the client's IP address on the server side.</p>
  
  <p>I would like to be able to show from which server the client is on server-side.</p>
  
  <p>For example:</p>
  
  <blockquote>
    <p>Server got a connection from IP address 10.10.63.75 port 35689</p>
  </blockquote>
  
  <p>If it is possible I want to show the client's machine's name...</p>
  
  <p>Example:</p>
  
  <blockquote>
    <ul>
    <li>Dipankar-PC Windows 8</li>
    </ul>
  </blockquote>
  
  <p>How can I achieve this?</p>