Socket.Connect : Socket « System.Net.Sockets « VB.Net by API






Socket.Connect

   


Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class DateTimeClient
  Public Shared Sub Main()
    Dim args As String() = Environment.GetCommandLineArgs()

    Dim serverHost As String = "127.0.0.1"

    Dim hostadd As IPAddress = Dns.Resolve(serverHost).AddressList(0)
    Dim EPhost As New IPEndPoint(hostadd, 13)
    Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)


    Try
      clientSocket.Connect(EPhost)
    Catch ex As Exception
      Console.WriteLine(ex.ToString())
    End Try

    Try
      Dim recvbytes(1024) As Byte
      Dim i As Integer = clientSocket.Receive(recvbytes, 0, clientSocket.Available, SocketFlags.None)

      Dim datetime As String = Encoding.ASCII.GetString(recvbytes, 0, i)

      Console.WriteLine("Receive {0} bytes.", i)
      Console.WriteLine("Current server date/Time: {0}", datetime)

      clientSocket.Shutdown(SocketShutdown.Both)
      clientSocket.Close()
    Catch ec As Exception
      Console.WriteLine(ec.StackTrace.ToString())
      Exit Sub
    End Try
  End Sub
End Class

   
    
    
  








Related examples in the same category

1.New Socket
2.Socket.Bind
3.Socket.Listen
4.Socket.Receive