Contact Info

Crumbtrail

ActiveXperts.com » Network Component » Network Component Objects » TCP

ActiveXperts Network Component Tcp Object

The Network Component 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 Network Component Tcp object basically for two purposes:

The Tcp object is part of the Network Component. Overview of all Network Component objects:

DnsServer & DnsRecord - Ftp & FtpFile - Http - Icmp - IPtoCountry - Msn - Ntp - Radius - Rsh - Scp - SFtp - Ssh - SnmpManager - SnmpTrapManager - SnmpMibBrowser - Tcp - Tftp - TraceRoute - Udp - Xen - Wake-on-LAN - Xen (Citrix)


Tcp Sample code

VBScript client/server sample: CLIENT.VBS

   ' Create a socket instance
   Set objTcp       = CreateObject ( "AxNetwork.Tcp" )
   Set objConstants = CreateObject ( "AxNetwork.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 ( "AxNetwork.Tcp" )
   Set objConstants = CreateObject ( "AxNetwork.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 Network Component Socket object to establish a telnet session to a server or device and read its contents or configuration, for instance:

VBScript sample: Telnet; show web page contents

   ' Create a socket instance
   Set objTcp       = CreateObject ( "AxNetwork.Tcp" )
   Set objConstants = CreateObject ( "AxNetwork.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 AxNetwork
   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

You can download the full samples here.