Karla News

Visual Basic WinSock Tutorial

Visual Basic

Winsock is a control that is used to make connections between computers over the network or the Internet. Basically, it is a set of functions and variables, that lets us send and receive data from one computer to another. Winsock is located in the MSWINSCK.OCX file, from the “system32” folder.

In order to use Winsock in Visual Basic, the MSWINSCK.OCX file must be imported. This can be done by selecting “Components” from the Project menu. Then, we must check the Microsoft Winsock Control. Now we can start using Winsock in our application. To see how winsock works, in this tutorial we will write a basic chat client and a chat server, that will send, receive and process information between two computers.

The Client-Side part of the chat program

First of all, we will create the client-side part of the chat program. Create a new Visual Basic project, and import the Winsock Control. Put winsock on the form, and rename it “winsck”. Now, we must put additional controls on our form, that will be used as parameters for the winsock. We will need a textbox control that will be used for the host address. Put a textbox on the form and rename it “host_address”. Then, in order to connect to the host, we will need to specify the connection port. Add another textbox, and rename it “host_port”. Now that we have the connection parameters, we only need some data to send. The data will be in another textbox, with the name “data_text”. The application will need some command buttons too, so the user can manipulate the winsock along with the parameters. Create two command buttons, with the name “connect_button” and “send_button. The first one will be used to connect to the host, and the second to send the text from “data_text”. We will also need another textbox with the name “incoming_text”, where we can view the incoming data. Now that all the controls are set, we must write the code.

See also  How to Secure an Excel Spreadsheet

In order to send data to the host, we must make the connection between computers. The button “connect_button” will be used to do that. Double-click on it and write the connection code:

Private Sub connect_button_Click()
winsck.RemoteHost = host_address.Text
winsck.RemotePort = host_port.Text
winsck.Connect
End Sub

The code is very easy to understand. At the event “click” on the command button, the winsock will connect to the host:port from the textboxes.Since we have a connection, our application needs to know how to receive data. This will be done with the help of “DataArrival” event. Double-click on the winsock control and write the following code:

Private Sub winsck_DataArrival(ByVal bytesTotal As Long)
Dim incoming_string As String
winsck.GetData incoming_string, vbString
incoming_text.Text = incoming_text.Text & _incoming_string & vbCrLf
End Sub

This piece of code is telling the winsock to put the incoming data in the variable “incoming_string” and then to show it in our “incoming_text” textbox, and create a new line.Now we must write the code for sending information through the winsock. Double-click on the “send_button” and write the following code:

Private Sub send_button_Click()
winsck.SendData data_text.Text
End Sub

This is pretty self-explanatory. Winsock is taking care of everything with this simple “SendData” function. The client of the chat program is ready. Feel free to do a nice graphical design to make the application look good.

The Server-Side part of the chat program

For the server we will need to create a second form. The controls from this form will be the same as in Form1, with a little exception. The server does not need a “host_address” to connect, because the server is just listening to the incoming data, and the client connects to it. The rest of the controls will be the same. Now, we can write the code to make our server connectable. The code is for “connect_button”:

See also  Tips to Make Your Computer Run Faster

Private Sub connect_button_Click()
winsck.LocalPort = host_port.Text
winsck.Listen
End Sub

Now, we need to handle the connection from our client. We will use the ConnectionRequest method. Double-click on the winsock control and write the following code:

Private Sub winsck_ConnectionRequest(ByVal requestID As Long)
If winsck.State sckClosed Then
winsck.Close
End If
winsck.Accept requestID
incoming_text.Text = incoming_text.Text & _sockMain.RemoteHostIP & _”is connected.” & vbCrLf
End Sub

This code checks the connection and the requestID and then it will show the IP address of the client. The requestID is an identification number, which is very useful when you are handling more connections. Now the server is connectable, but it can’t send or receive any data from the client. This can be solved with the following code for the “send_button” command button.

Private Sub send_button_Click()
winsck.SendData data_text.Text
End Sub

Private Sub winsck_DataArrival(ByVal bytesTotal As Long)
Dim incoming_string As String
winsck.GetData incoming_string, vbString
incoming_text.Text = incoming_text.Text & _incoming_string & vbCrLf
End Sub

The code is almost the same with the one from the client. The server is now ready for use. We must now compile the code and make an “.exe” file. Then we can test the chat application. Connect to the localhost, (127.0.0.1) and type something in the “data_text”. The text will appear in the server chat. This was a basic winsock usage, but it can be used to build extremely powerful application, that can handle many connections, and process data in real-time.