Mar 13

Revisiting VB 6 after some time, today we are going to develop a chat tool–a simple client and server text.

You only need a working knowledge of VB, as this example doesn’t attempt to make a complex program; we are aiming for something short (not much coding), simple and easy to follow.

Let’s begin. First, we should delve a bit into the architecture. The tool we are going to build comprises two parts: the server part, which would wait for connections to be made (just like a web server waits for users to connect), and the client part, which would initiate connections to the server (the way a browser initiates connection to a web server).

To store the respective parts of our project, create two subfolders in the folder where you are going to save your project. Name the first one “Client” and other one “Served.

The Server:
Open Visual Basic 6 and Select “Standard Exe” for our new project in the dialog box that appears. Once the interface is displayed with a form visible in the center, go to File > Save Project. This will ask you to provide a name for your form. We are going to code the server first, so select the Server folder just created. Name your form frmServer. Next, another dialog box will ask you to save your project. Name it prjServer. Once this is done, you should make it your habit to press CTRL+S while you code, to make sure your code is not lost in case the application hangs or some other catastrophe occurs.

Now let’s change the default text, ‘Form1’, that appears in the title bar. To do this, we are going to edit the ‘Caption’ property of the form. Properties can be accessed through the Properties box displayed at the bottom right of VB interface. This is a two-column, table-like box, where the first column displays a property’s name and the corresponding cell in the second column displays its setting or value. Whatever item is selected on the form, its properties are listed in properties box, from where you can edit them. Your form is selected by default (indicated by the resize handles on its boundaries) and the properties box has the ‘Caption’ properly highlighted. Write ‘Server’ in place of ‘Form1’.

Now, add two text boxes and a command button to the form (by first selecting the text box icon from the toolbar on the left-hand side of the VB interface, and drawing it on the form according to the size you desire; you can also simply-double-click a control icon on the toolbox to have it placed over the form). The two text boxes are for chat purposes, while the command button is required to initiate the operation of sending text to the client. Set the ‘Name’ property of the first text box to ‘txtChatln’, of the second text box to ‘txtChatOut’ and lastly, name the command button ‘cmdSend’ and set its caption property to ‘Send’ (default was ‘Command1’). These names are for our own convenience and could be anything
From the ‘Text’ property of the two text boxes, remove ‘Text1’ & ‘Text2’ respectively (the default texts) and leave the fields blank. The second text box (txtChatln), which you should resize, is used for handling incoming chat text, and so should have the extra feature of being able to scroll as new text arrives. To add this functionality, set txtChatln’s ‘MultiLine’ property to ‘True’ and the ‘ScrollBar’ property to ‘2–Vertical’. This is available as the second choice in the drop-down list that appears when you attempt to change ‘ScrollBar’ property.

Now it’s time to add socket control–the brains of our application. This control is not listed in the default list that appears in the toolbox; you will have to bring it there. Here’s how: Right-click on the toolbox, and select ‘Components’. A component list dialog box is displayed. Scroll down and check ‘Microsoft Winsock Control 6.0’, and press OK. You will see that a new control with an icon showing two PCs has now been added to the toolbox. Using this, place a socket on your form and name it ‘sckServer’. On the form it would look as though it is the same icon as in the toolbox.

When you press F5 to test run the project, you will observe that socket control is not visible–this is normal, as this control is invisible at run-time. Users do not have to interact with sockets like the text box and command buttons. We give commands to sockets using only codes.

Time to add code: infusing blood into this body! Double- click the form (make sure it’s not a control but the form on which you are clicking). The coding pane has got two dropdown lists on the top. As you have double-clicked the form, you will see ‘Form’ in the first dropdown (on left) and ‘Load’ in the other dropdown (on right). The first shows the item and the second shows the event associated with that item. This means that you are now coding for the form’s Load event. An event is something that happens with an item (the form is loaded when the application starts, so Load is an event of the form). Whenever you will double-click a form or control, you will enter its coding window; see its name in the first dropdown and the event for which code is being entered in the second dropdown.

Add the following code in the form’s load event.
sckServer.LocalPort = 6000

This will make the server listen (wait for connections) on port 6000 as soon as the application starts.

Now that you are in the coding window, if you want to go back to your form design window, simply double-click Form1 in the project explorer box on the right-hand side of the VB interface. You can also use menu ‘Windows > Form1 (Form)’.

Similarly, add the following code to the command button which we have named cmdSend. Do this by double-clicking the button when you are in the form design window. Note that the event listed in the second dropdown is ‘Click’ which signifies that when this code will be executed, the button’s event will be a click.

sckServer.SendData txtChatOut.Text
& vbCrLf

The first line tells the socket (sckServer) to send the data present in the text box (txtChatOut) along with a new line character, so that every line of text starts with a new line on the other side (i.e. the client, which we will be coding after we are done with the server). The second line empties the text box we are using in anticipation of the next text a user might type.

Now double-click the socket control we have placed on form to add code in its ‘ConnectionRequest’ event (Fig 3b). Note that this event is not selected by default when you enter the coding window. Select this event from the second dropdown (on the right), which lists all the possible events for our socket (sckServer).

If sckServer. State <> sckClosed Then
sckServer. Close
sckServer. Accept requestlD

This is the way to accept connections when a connection request arrives at the socket. Note that we are coding a server (as we have instructed the socket to listen), and that’s why we have to tell sockets how to accept connections when requests come from clients. It’s not a problem if you don’t understand what these two lines do at the moment, as this code has nothing to do with the functionality aspects of our program and is only used to accept connections. (For inquisitive readers, in short, the first line checks the socket state and the second line invokes an accept method with the Request ID returned by an incoming connection request).

Now select ‘DataArrival’ event from the second dropdown on the top, which currently displays ConnectionRequest. Add the following code.

Dim ChatData As String
sckServer.GetData ChatData
txtChatln.Text = txtChatln.Text & ChatData

The variable “ChatData” of type “String” is declared (first line) and used with the “GetData” method of the socket to get incoming data (second line). Finally, the third line updates the incoming chat text box with new data. The reason for assigning txtChatln & ChatData to txtChatln is to make sure that the current contents are net overwritten by the new data arrival.

The Client
Open another VB 6 file and select ‘Standard Exe’ for the new project. Once the interface is displayed, with a form visible in the center, save the form and project in the folder Client (already created) with names frmClient and prjClient respectively.

Set the ‘Caption’ property of this form to ‘Client’. In exactly the same way as for the server part, add a Winsock control to this form (right-click toolbox, select ‘Components’, select ‘Microsoft Winsock 6.0’). Name it ‘sckClient’.

Add two text boxes and a command button. Name the text boxes ‘txtChatln’ and ‘txtChatOut’. Set txtChatln’s ‘MultiLine’ property to ‘True’ and ‘ScrollBar’ property to ‘2–Vertical’. This is available as the second choice in the dropdown tist that appears when you attempt to change the ‘ScrollBar’ properties. Remove ‘Text1’ and ‘Text2’ from ‘Text’ properties of both text boxes and leave these fields blank. For the command button, set the ‘Name’ property to ‘cmdSend’, and its ‘Caption’ property set to ‘Send’. After resizing the form and positioning controls over it, let’s do the coding part. For the form’s lead event, add the following code:

sckClient.RemoteHost = “localhost”
sckClient.RemotePort = 6000

For communications, we need two pieces of information: the address of the remote machine and its port number. For a remote host, we have specified a local host–this is your own PC, because at this time, the server is also running on the same PC. As we have already set our server to listen on port 6000, the same is specified over here as well. As we are building and running this application on the same PC, we have used Iocalhost (you can also enter in place of Iocalhost, along with the inverted commas as seen in line 1), but in case you are using this chat program to communicate over a network, you need to specify the IP address of the machine on which you have set your server to listen, instead of Iocalhost. The last line instructs our socket to connect to the server.

Now add the code for the send button. This works exactly the same way as the send button of the server.

sckClient.SendData txtChatOut.Text&
txtChatOut.Text = “”

Starting Chat
Go to your server project and press F5 to run it. Your server is in listening mode now. Recall that we had placed listen code in Ioad event of server form. Come back to the client project and press F5 to run it too. Client will make connection with the server as we have just added connection code in Ioad event of client form. At this time we have not added any visual indication that a connection has been established or what the status of the connection is, we need to test it by typing some text in either the client or the server, in the text box just above the send button (this is the txtChatOut text box on both forms). This text would appear on the other part’s bigger text box with scrollbar (the txtChatln text box on both forms).

You can go to ‘File > Make you project.exe’ menu to create standalone executable files for both client and server.

Remember while you testing to run the server first and then the client (otherwise the client will attempt to connect to a non-existent server!) and if you happen to close either part, close the other one too and start afresh.

This is a very basic chat application with minimal features and as
little code as possible.