Initializing a Computer as a Server and Accepting Connections (Visual Basic)
Take the following steps to start a project and to add some code that will let you initialize a computer as a server and accept connections.
1. |
Start Visual Basic. Select Standard EXE and click Open. |
|
2. |
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). |
|
3. |
Add a command button and name it as follows: |
|
|
Name |
Caption |
|
ServerInit |
Init Server |
4. |
Define global variables as follows: |
Public hServer as Integer ' handle to server
Public WithEvents LEADNet1 As LEADRasterInet
5. |
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
6. |
Code the ServerInit_Click procedure as follows: |
Private Sub ServerInit_Click()
Dim nRet As Integer
' initialize server on port 1000
nRet = LEADNet1.InetServerInit(1000)
hServer = LEADNet1.InetServerHandle
If nRet
<> 0 Then
MsgBox "Error initializing server:"
+ CStr(nRet)
End If
End Sub
7. |
Code the LEADNet1_InetAccept procedure as follows: |
Private Sub LEADNet1_InetAccept(ByVal
iServer As Integer)
Dim hComputer As Integer
Dim hName As String
nRet = LEADNet1.InetAcceptConnect(hServer)
hComputer = LEADNet1.InetConnectedComputer
If nRet
<> 0 Then
MsgBox "Error accepting connection:"
+ CStr(nRet)
Exit Sub
End If
LEADNet1.InetGetHostName
hComputer, HOST_NAME_DESCRP
hName = LEADNet1.InetHostName
'add to our list
LEADNet1.SendList(LEADNet1.SendListNum) = hComputer
MsgBox "Connection
accepted from:" + hName
End Sub
8. |
Code the main form's Form_Unload procedure as follows: |
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
'disconnect all connections
For i = 0 To LEADNet1.ConnectListNum
- 1
LEADNet1.InetDisconnect
LEADNet1.ConnectList(i)
Next i
'release the Inet object
Set LEADNet1 = Nothing
End Sub
9. |
Run your program to test it. |
10. |
Save the project to use as the server for other tutorials. Create an EXE that you can run from outside Visual Basic. |