You are on page 1of 5

A Client server Chat program using the Winsock control

Level:

See how easy it is to create a simple chat program. This tutorial explains how to create both the
client and server sides oI the program using the Winsock control.
In this tutorial we will learn how to write a simple client server chat program in Visual Basic 6
using the Winsock control. We will right both the server and client side oI this program. One oI
the ways computers can communicate to each other through the internet is by using TCP/IP. In
windows there is a Winsock.dll that programmers can leverage to help in this communication.
This DLL is very annoying to use in Visual Basic. Luckily, we can use the Winsock control
instead - which wraps a lot oI the Winsock.dll commands into an easy control Ior us.
You might want to download the source code Ior this tutorial .

To start lets create the client side oI our program. Open a new Visual Basic 6 Project (Standard
EXE). In order to use the Winsock control we must add it to our toolbox. You can do this by
selecting Project -~ Components Irom the menu bar or by pressing Ctrl-T. Then click the check
box next to the MicrosoIt Winsock Control 6.0.



Now on Form1 create the Iollowing controls with the Iollowing property values

%pe: Winsock
Name: sockMain

%pe: TextBox
Name: txtHost
%ext: 127.0.0.1

%pe: TextBox
Name: txtPort
%ext: 12345

%pe: TextBox
Name: txtStatus
MultiLine: True
ScrollBars: 3 - Both

%pe: TextBox
Name: txtSend

%pe: CommandButton
Name: cmdConnect
%ext: &Connect

%pe: CommandButton
Name: cmdSend
%ext: &Send

Add some labels to organize stuII and arrange your Iorm to look something like this:



Now lets write our code
Double click on the Connect button and add this code:
Private Sub cmdConnect_Click()
sockMain.RemoteHost = txtHost.Text
sockMain.RemotePort = txtPort.Text
sockMain.Connect
End Sub
This code is pretty easy to Iollow. We just setup our socket to point to the remote host speciIied
and the remote port, and then we tell it to connect.

Next wire up the Send button:
Private Sub cmdSend_Click()
sockMain.SendData txtSend.Text
End Sub
Again very simple, all we do is Winsock control to send the data we want it to. Next we will
want to receive data when the other person sends it to us. The Winsock control again makes this
easy. Every time data arrives the Winsock controls DataArrival event will Iire. So lets wire in
our code Ior that.
Private Sub sockMain_DataArrival(ByVal bytesTotal As Long)
Dim strData As String

sockMain.GetData strData, vbString
txtStatus.Text = txtStatus.Text & _
strData & vbCrLf
End Sub
So in this event we call sockMain.GetData. This method takes two parameters. The Iirst is the
variable to store the data in. The second is what type this variable is. Once we have our strData
variable Iilled we append it to status textbox.
Server Side of VB6 Program
Now lets create the server side oI our program. Normally you would create a new Visual Basic
program just Ior the dedicated server, but to make things easier we are just going to create a
second Iorm. So add a Form2 to your project. And add these controls to it.

%pe: Winsock
Name: sockMain

%pe: TextBox
Name: txtPort
%ext: 12345

%pe: TextBox
Name: txtStatus
MultiLine: True
ScrollBars: 3 - Both

%pe: TextBox
Name: txtSend

%pe: CommandButton
Name: cmdListen
%ext: &Listen

%pe: CommandButton
Name: cmdSend
%ext: &Send

The way a client server relationship works is that the server listens on the port speciIied. Then
when a remote computer (the client) connects to that server at the speciIied port the server
creates a connection with the client and they can communicate back and Iorth. This is why the
server does not have a txtHost text box - because he will listen Ior any computer that wants to
connect.

So lets write our Listening code:
Private Sub cmdListen_Click()
sockMain.LocalPort = txtPort.Text
sockMain.Listen
End Sub
That`s it - when the user presses the listen button sockMain will start listening on the port
speciIied. Now lets move on in our tutorial. Next we will write the code to handle an incoming
connect. When someone connects to the port we are listening on we have to decide what to do
with him. We do this in the Winsock controls ConnectionRequest event.
Private Sub sockMain_ConnectionRequest(ByVal requestID As Long)
If sockMain.State < sckClosed Then
sockMain.Close
End If

sockMain.Accept requestID

txtStatus.Text = txtStatus.Text & _
"Accepted connection from: " & _
sockMain.RemoteHostIP & vbCrLf
End Sub
The Socket controls ConnectRequest event passes with it a requestID - this identity's who is
trying to connect. This is very useIul iI you are allowing more than one connection. Since in this
basic tutorial we are only allowing one person to connect at a time our code is very simple. First
we check to see iI the connection is already open we just accept the connection. Then we write a
line to our status box se we know.

Lastly lets wire up our send button and our data arrival event Ior Form2 like we did Ior Form1:
Private Sub cmdSend_Click()
sockMain.SendData txtSend.Text
End Sub

Private Sub sockMain_DataArrival(ByVal bytesTotal As Long)
Dim strData As String

sockMain.GetData strData, vbString
txtStatus.Text = txtStatus.Text & _
strData & vbCrLf
End Sub
Also add a quick line of code in the Form1 load event to show Form2 as well.
Private Sub Form_Load()
Form2.Show
End Sub
Now lets test out our program. Press F5 to run it. Then click the listen button on our server
(Form2). Next click the connect button on our client (Form1). Now the two Iorms can chat back
and Iorth. II you would like you can download the source code Ior this tutorial .
This tutorial explained how to create a simple client server connection. II you would like to
expand upon this make a server that allows multiple clients to connect to it check out this
tutorial: A multiple client server chat program using Winsock control.

You might also like