HOWTO: Create & Use a Client/Server Using Winsock TCP Controls
ID: Q152057
|
The information in this article applies to:
-
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0
-
Microsoft Internet ActiveX Controls, version 1.0
SUMMARY
The Internet Control Pack contains the Winsock control that allows you to
connect to a remote machine and exchange data dynamically between computers
in both directions. This article demonstrates the steps required to create
a minimal client and server relationship for exchanging data between
computers. The Winsock TCP control, a connection-based control, is used to
create the client and server.
MORE INFORMATIONPhase One - Create the Server
- Start a new project in Visual Basic. Form1 is created by default.
- From the Tools menu, choose Custom Controls, and add a reference to
Microsoft Winsock Controls.
- Add a Winsock TCP control to the Form, and change the control's index
property to zero.
- Add the following code to the general declarations section of Form1:
Private gSockInstance
Private Sub Form_Load()
gSockInstance = 0
TCP1(0).LocalPort = 1007
TCP1(0).Listen
End Sub
Private Sub TCP1_ConnectionRequest(Index As Integer, _
ByVal requestID As Long)
gSockInstance = gSockInstance + 1
Load TCP1(gSockInstance)
TCP1(gSockInstance).Accept requestID
End Sub
Private Sub TCP1_DataArrival(Index As Integer, ByVal bytesTotal _
As Long)
Dim vta
TCP1(Index).GetData vta, vbString
MsgBox vta, 0, "Server"
End Sub
- Choose Start from the Run menu, or press the F5 key to start the
program.
- Minimize Visual Basic.
Phase Two - Create a Client
- Start a second copy of Visual Basic. A new project (Project1) with a
default form (Form1) is created.
- From the Tools menu, choose Custom Controls, and add a reference to
Microsoft Winsock Controls.
- Add a Winsock TCP control and two Command buttons to the Form. Change
the Captions of the Command buttons to "Connect" and "SendData".
- Add the following code to the Connect button Click event:
Private Sub Command1_Click()
With TCP1
.RemoteHost = "machine name you specify"
.RemotePort = 1007
.Connect
End With
End Sub
- Add the following code to the SendData button Click event:
Private Sub Command2_Click()
TCP1.SendData "This is how we begin"
End Sub
- Add the following code to the TCP1_Connect event:
Private Sub TCP1_Connect()
If (TCP1.State = sckConnected) Then
MsgBox "connection successful"
Else
MsgBox "connection failed"
End If
End Sub
For demonstration purposes of this article, the client and server
execute on the same machine. TCP1.RemoteHost is specified as the machine
where the server is running. However, a simple change to the RemoteHost
property will allow you to connect to any machine with a Point-to-Point
connection.
- Choosing Start from the Run menu, or press the F5 key to start the
program.
- Press the Connect button, and the message box is displayed stating that
the connection was successful.
- Press the SendData button, and a message box containing the data that
was sent is displayed from the server.
Additional query words:
Keywords : kbnetwork kbAPI kbSDKPlatform kbVBp400 kbWinsock kbGrpNet
Version : WINDOWS:1.0,4.0
Platform : WINDOWS
Issue type :
|