Connecting to a Remote Computer (Visual Basic)

Take the following steps to start a project and to add some code that will let you connect to a remote computer that is acting as a server.

1.

Run the server exe you created in Initializing a Computer as a Server and Accepting Connections from out side Visual Basic.

2.

Start Visual Basic. On the New tab, select Standard EXE and click Open to create a new application.

3.

Add the LEAD Raster Object, LEAD Raster Variant Object and LEAD Raster Internet Object to your project.

 

For VB5: On the Project pull-down menu, use the References option, and select the LEAD Raster Object Library (14.0), LEAD Raster Variant Object Library (14.0) and LEAD Raster Internet Object Library (14.0).

4.

Add a Text box to the main form and name it as follows:

 

Name

 

RemoteComputer.

 

Set the Text property to the name of the computer you will use as a server, or set it to 207.238.49.190.

5.

Add two command buttons to the form and give them the following names:

 

Name

Caption

 

Connect

Connect To Remote

 

Disconnect

Disconnect From Remote

6.

Define global variables as follows:

Public WithEvents LEADNet1 As LEADRasterInet

7.

Code the Form_Load() procedure as follows:

Private Sub Form_Load()
   Dim Factory As New LEADRasterFactory
   Dim LEADRaster As LEADRaster
   Dim szLic As String
   Dim szKey As String

   'create a temp LEADRaster object and use it
   szLic = "LEADTOOLS OCX Copyright (c) 1991-2004 LEAD Technologies, Inc."
   Set LEADRaster = Factory.CreateObject("LEADRaster.LEADRaster", szLic)
   'create the Inet object
   Set LEADNet1 = CreateObject("LEADRasterInet.LEADRasterInet")

   LEADNet1.EnableMethodErrors = False
End Sub

8.

Code the Connect_Click procedure as follows:

Private Sub Connect_Click()
   Dim nRet As Integer
   'connect to remote computer on port 1000
   nRet = LEADNet1.InetConnect(RemoteComputer.Text, 1000)

   If nRet <> 0 Then
      MsgBox "Error connecting to:" + RemoteComputer.Text + ":" + CStr(nRet)
      Exit Sub
   End If
End Sub 

9.

Code the Disconnect_Click procedure as follows:

Private Sub Disconnect_Click()
   Dim nRet As Integer

   'we are assuming on one connection in this example, therefore the handle
   'to the remote computer will be in the first position in the SendList
   nRet = LEADNet1.InetDisconnect(LEADNet1.SendList(0))

   If nRet <> 0 Then
      MsgBox "Error disconnecting from remote:" + CStr(nRet)
   End If
End Sub 

10.

Code the LEADNet1_InetConnected event as follows:

Private Sub LEADNet1_InetConnected(ByVal iComputer As Integer)

   Dim sRemote As String 

   'get remote host name
   LEADNet1.InetGetHostName iComputer, HOST_NAME_DESCRP
   sRemote = LEADNet1.InetHostName

 

   'add this connection to our SendList
   LEADNet1.SendList(LEADNet1.SendListNum) = iComputer
   MsgBox "Successfully connected to remote computer:" + sRemote
End Sub

11.

Code the LEADNet1_InetDisconnected event as follows:

Private Sub LEADNet1_InetDisconnected(ByVal iComputer As Integer)

   MsgBox "Disconnect from remote computer" 

End Sub

12.

Run your program to test it. Enter the name of the computer that is serving as the server in the text box. For testing purposes you might want to run both the server and the remote programs on the same computer.

13.

Save the project to use as a starting point for other tutorials.