First of all start a new Standard EXE and then Ctrl + T to open the components window.
Now find Microsoft Winsock Control and tick the box to the left of it.
You will now see a new object in the toolbox, place 2 instances of the new object onto the form and name one of them wskServer and one of them wskClient
Now place 3 command buttons on your form and name them cmdListen, cmdConnect and cmdSend
Now I¡¯ll just give you a brief description of how this is going to work, when the user clicks cmdListen then wskServer will start listening for incoming connections and if the user clicks cmdConnect then wskClient will try to connect to the IP & Port specified, when the user clicks cmdSend it will send the data to the client / server its connected too.
Note: If you don¡¯t have anyone to send your application to you can test it out on your own computer just fine
The first thing we want to do is enabled the app to listen for connections and add 2 variables for later on so add this code in the General area of your code.
CODE
Dim wskListening As String 'This is so we can tell later whether or not we are acting as the server or the client
Dim wskConnecting As String
Dim wskConnecting As String
And add this below it
CODE
Private Sub cmdListen_Click()
wskServer.LocalPort = 8000 'Set the port that the client should connect too
wskServer.Listen 'Start listening for incoming connections
wskListening = "True"
End Sub
wskServer.LocalPort = 8000 'Set the port that the client should connect too
wskServer.Listen 'Start listening for incoming connections
wskListening = "True"
End Sub
Now we want to put in the code that will let make the client connect to the server. So add this code to your project
CODE
Private Sub cmdConnect_Click()
wskClient.RemoteHost = localhost 'replace this with the I.P of the computer you wish to connect too, this will attempt to connect to your own PC
wskClient.RemotePort = 8000 'Set the port it should try to connect too, this MUST be the same as the port the server is listening for on the other end
wskClient.Connect wskClient.RemoteHost, wskClient.RemotePort 'Connect to the server
wskConnecting = "True"
End Sub
wskClient.RemoteHost = localhost 'replace this with the I.P of the computer you wish to connect too, this will attempt to connect to your own PC
wskClient.RemotePort = 8000 'Set the port it should try to connect too, this MUST be the same as the port the server is listening for on the other end
wskClient.Connect wskClient.RemoteHost, wskClient.RemotePort 'Connect to the server
wskConnecting = "True"
End Sub
Now the next task is to get the server to accept the incoming connection. Very simple and short, add this to your project.
CODE
Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
wskServer.Close
wskServer.Accept (requestID) 'Accepts the incoming connection
End Sub
wskServer.Close
wskServer.Accept (requestID) 'Accepts the incoming connection
End Sub
Now your app can connect to 2 different computers
CODE
Private Sub cmdSend_Click()
If wskConnecting = "True" Then
wskClient.SendData ("Data from the client") 'Replace this with whatever you text you want
ElseIf wskListening = "True" Then
wskServer.SendData ("Data from the server")
End If
End Sub
Private Sub wskServer_DataArrival(ByVal bytesTotal As Long)
Dim data As String 'Set the string to store the data in
wskServer.GetData data 'Recieve the data and store it in the string called data
MsgBox data, vbOKOnly, "Data recieved" 'Display the data in a message box
End Sub
Private Sub wskClient_DataArrival(ByVal bytesTotal As Long)
Dim data As String 'Set the string to store the data in
wskClient.GetData data 'Recieve the data and store it in the string called data
MsgBox data, vbOKOnly, "Data recieved" 'Display the data in a message box
End Sub
If wskConnecting = "True" Then
wskClient.SendData ("Data from the client") 'Replace this with whatever you text you want
ElseIf wskListening = "True" Then
wskServer.SendData ("Data from the server")
End If
End Sub
Private Sub wskServer_DataArrival(ByVal bytesTotal As Long)
Dim data As String 'Set the string to store the data in
wskServer.GetData data 'Recieve the data and store it in the string called data
MsgBox data, vbOKOnly, "Data recieved" 'Display the data in a message box
End Sub
Private Sub wskClient_DataArrival(ByVal bytesTotal As Long)
Dim data As String 'Set the string to store the data in
wskClient.GetData data 'Recieve the data and store it in the string called data
MsgBox data, vbOKOnly, "Data recieved" 'Display the data in a message box
End Sub
And there you have it
If you have liked this tutorial then please show your appreciation register and leave your feedback.
Attached File(s)
discuss this topic to forum

