ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


ActiveSocket - Tcp Object

The ActiveSocket Tcp object is based on the Transport Control protocol (TCP) protocol. Transport Control Protocol is a Transport Layer host-to-host protocol that provides reliable, connection-oriented service for IP traffic. TCP transports a stream of data in both directions between end stations. TCP does this by breaking the data into segments for transmission across a network running Internet Protocol.

You can use the ActiveSocket Tcp object basically for two purposes:

  • To create your own client/server applications; your server-application listens for an incoming connection on a specific port, while your client application makes a connection to it; client and server can do bidirectional communication, with support for ASCII and binary data, in your LAN or through the internet;
  • To automate Telnet sessions; for instance, network switches are usually configured manually from a telnet session. You can use ActiveSocket to automate these Telnet sessions, hiding passwords and specific configuration menu's from the operators.

The Tcp object is part of the ActiveSocket component. Overview of all ActiveSocket objects:

Tcp Sample code

VBScript client/server sample: CLIENT.VBS

   ' Create a socket instance
   Set objTcp       = CreateObject ( "ActiveXperts.Tcp" )
   Set objConstants = CreateObject ( "ActiveXperts.ASConstants" )

   objTcp.Protocol  = objConstants.asSOCKET_PROTOCOL_RAW
   objTcp.StartListening 1500  ' Listen for connection on port 1500
   WScript.Echo "StartListening, result: " & objTcp.LastError
   If objTcp.LastError <> 0 Then
     WScript.Quit
   End If

   Do while objTcp.ConnectionState=objConstants.asSOCKET_CONNSTATE_LISTENING
     ' Wait for an incoming connection
   Loop
   If objTcp.ConnectionState<>objConstants.asSOCKET_CONNSTATE_CONNECTED Then 
     WScript.Quit
   End If

   Do While objTcp.ConnectionState=objConstants.asSOCKET_CONNSTATE_CONNECTED 
            And str <> "Quit"
     If objTcp.HasData Then
       str = objTcp.ReceiveString
       WScript.Echo "ReceiveString: " & str
     End If
     objTcp.Sleep 100
   Loop

   objTcp.Disconnect

VBScript client/server sample: SERVER.VBS

   ' Create a socket instance
   Set objTcp       = CreateObject ( "ActiveXperts.Tcp" )
   Set objConstants = CreateObject ( "ActiveXperts.ASConstants" )

   objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW

   ' Make a connection to port 1500 on remote server
   objTcp.Connect "127.0.0.1", 1500
   WScript.Echo "Connect, result: " & objTcp.LastError 
   If objTcp.LastError <> 0	 Then
     WScript.Quit
   End If

   objTcp.SendString "Hello, world!"
   WScript.Echo "SendString, result: " & objTcp.LastError

   objTcp.SendString "Quit"
   WScript.Echo "SendString, result: " & objTcp.LastError

   ' And finally, disconnect
   objTcp.Disconnect 

Use the ActiveSocket Socket object to establish a telnet session to a server or device and read its contents or configuration, for instance:

  • To log on to a mail server and list all e-mails, and delete the ones that match a pre-defined pattern;
  • To log on to a switch or router and make changes to the configuration, automatically;
  • To log on to a telnet server and run a pre-defined command sequence;
  • To log on to an IT temperature device and read the temperature;
  • To log on to a print server and reboot it.

VBScript sample: Telnet; show web page contents

   ' Create a socket instance
   Set objTcp       = CreateObject ( "ActiveXperts.Tcp" )
   Set objConstants = CreateObject ( "ActiveXperts.ASConstants" )

   objTcp.Protocol  = objConstants.asSOCKET_PROTOCOL_RAW
   objTcp.StartListening 1500  ' Listen for connection on port 1500
   WScript.Echo "StartListening, result: " & objTcp.LastError
   If objTcp.LastError <> 0 Then
     WScript.Quit
   End If

   Do while objTcp.ConnectionState=objConstants.asSOCKET_CONNSTATE_LISTENING
     ' Wait for an incoming connection
   Loop
   If objTcp.ConnectionState<>objConstants.asSOCKET_CONNSTATE_CONNECTED Then 
     WScript.Quit
   End If

   Do While objTcp.ConnectionState=objConstants.asSOCKET_CONNSTATE_CONNECTED 
            And str <> "Quit"
     If objTcp.HasData Then
       str = objTcp.ReceiveString
       WScript.Echo "ReceiveString: " & str
     End If
     objTcp.Sleep 100
   Loop

   objTcp.Disconnect

Visual Basic .NET sample: Telnet demo - login to a telnet server

Imports ASOCKETLib
   Module Module1
     Public m_objTcp As Tcp
     Public m_objConstants As SocketConstants

     Private Sub ReadFromPort(ByVal TimeOut As System.Int32)
       Dim StartTime As System.Int32
       Dim strStringReceived As String
       Dim bSomethingRead As System.Boolean
       Dim lConnectionState As System.Int32

       StartTime = Environment.TickCount()

       Console.WriteLine("Attempting to receive data..." & vbCrLf)
       Do
         If(m_objTcp.ConnectionState <> m_objConstants.asCONN_CONNECTED) Then
           Exit Do
         End If
         System.Threading.Thread.Sleep(200)
         strStringReceived = m_objTcp.ReceiveString()
         If (strStringReceived <> "") Then
           Console.WriteLine(strStringReceived & vbCrLf)
         End If
       Loop Until (Environment.TickCount() > StartTime + TimeOut)

     End Sub

     Sub Main()
         Dim strHost As String     = "library.uah.edu"
         Dim strLogin As String    = "guest"
         Dim strPassword As String = "guest"

         m_objTcp = New Tcp()
         m_objConstants = New SocketConstants()

         m_objTcp.Protocol = m_objConstants.asPROTOCOL_TELNET
         m_objTcp.Connect( strHost, 23)
         If( m_objTcp.LastError = m_objConstants.asERR_SUCCESS )Then
            System.Threading.Thread.Sleep( 5000 )
            ReadFromPort(3000) ' Receive data for 3 seconds
            Console.WriteLine("Send: '" & strLogin & "'" & vbCrLf)
            m_objTcp.SendString(strLogin, True)
            ReadFromPort(3000) ' Receive data for 3 seconds
            Console.WriteLine("Send: '" & strPassword & "'" & vbCrLf)
            m_objTcp.SendString(strPassword, True)
            ReadFromPort(3000) ' Receive data for 3 seconds
            m_objTcp.Disconnect()
            Console.WriteLine("Disconnected by client." & vbCrLf)
         End If
     End Sub
   End Module

On ftp.activexperts-labs.com, you can find a lot of ActiveSocket samples. These samples are also part of the ActiveSocket installation.

Visit ftp.activexperts-labs.com