This is the basic code required to make a Server form, so now lets create a Client.
Add a new form, called frmClient, and add a WinSock control to the form, naming it tcpClient. Then, add the other controls listed below:
Control Name | Control Type | Caption/Text |
txtSend | TextBox | |
txtRecieve | TextBox | |
txtPCName | TextBox | |
cmdSend | CommandButton | Send |
cmdConnect | CommandButton | Connect |
You will notice that this is exactly the same as the Server form, except for the cmdConnect button. There is less code required for the Client form, and is shown below:
Private Sub Form_Load()
tcpClient.RemotePort = 100
'// Get our Server Name, and set the form's caption
Caption = "WinSock Tutorial - TCP Client @ " &
tcpClient.LocalHostName
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData
txtReceive.Text = strData
End Sub
Private Sub cmdSend_Click()
'// send the data
tcpClient.SendData txtSend.Text
End Sub
Private Sub cmdConnect_Click()
'// connect
tcpClient.RemoteHost = txtPCName.Text '// Change this to the name of the PC
tcpClient.Connect
End Sub
The Form_Load procedure sets the RemotePort properties. The RemotePort property is the port that the server is watching. In the code for the Server form, we set this to 100.
The next procedure is the same as in the Server form, with only the name of the WinSock control changed. This fills txtReceive with the data that has arrived.
The cmdSend_Click() procedure sends the data contained in txtSend, and the cmdConnect_Click() procedure attempts to connect to the PC which is entered in txtPCName. The Server form must already be open on this PC for this to work!
Finally, we will create another simple form, called frmStartup, with two command buttons (cmdServer and cmdClient). This will simply let you open which forms you want. The code you need is below:
Private Sub cmdServer_Click()
frmServer.Show
End Sub
Private Sub cmdClient_Click()
frmClient.Show
End Sub
Don't forget to change the start up form to frmStartup. To do this, click Project|Properties, and select frmStartup as the Startup Object
Comments