Two Person Chat With The Winsock Control And Visual Basic
The Microsoft WinSock library allows you to connect to a remote machine and exchange data using either the User Datagram Protocol (UDP) or the Transmission Control Protocol (TCP). Both protocols can be used to create client-server applications. In this tutorial Jason will be teaching you how to create a direct-connect chat program that uses TCP. The program itself will be designed in such a way that it can act as both the client and server.
Two Person Chat With The Winsock Control And Visual Basic - Code from the clients point of view (Page 3 of 7 )
Our chat application uses the client/server model, yet functions as both a client and server itself! This page will look at the methods used to connect to the remote computer (AKA connecting to the server).
The chat form that I've just shown you is useless unless it actually functions. The Winsock control that we have added to our chat form exposes several methods that we can override to both capture and send data from and to another chat user. The details of these methods are shown below:
Close: This method is called whenever a connection between your computer and another computer is terminated
Connect: This method handles connecting to a remote computer using either UDP or TCP and a specified port
ConnectionRequest: This method is called whenever the Winsock library detects that another computer is requesting a connection to yours
DataArrival: This method is called when data arrives from a remote computer
Error: This method is called whenever the Winsock library encounters an error during its operations
SendComplete: Called when a block of data is successfully sent to another computer using the SendData method
SendProgress: This method allows you to monitor the progress of a send operation
Let's start adding our code. Double click on the frmMain form and go to the general declarations section of the code window by changing the options in the two drop-down lists at the top of the window, like this:
Add the following line to the general declarations section:
The "Option Explicit" command simply tells Visual Basic not to compile our application if any variables are used, but not dimensioned.
Now, let's add the code behind our cmdConnect_Click method, which will actually attempt to establish a connection to the remote computer:
'Before we can connect, we should check to see
'if there is an IP and name for the user.
If txtIP.text = "" Or txtName.text = "" Then
MsgBox "You must enter both an IP and alias first!", vbCritical, "Error!"
On Error Resume Next
'Connecting the IP that is placed in the txtIP.text value.
wsChat.Connect txtIP.text, 1234
cmdClose.Enabled = True
cmdListen.Enabled = False
cmdConnect.Enabled = False
txtName.Enabled = False
The cmdConnect_Click() method starts by making sure that the user has entered both an IP address and alias to use during the chat session. If they haven't, then a message box is displayed and control returns to the application.
Next, we close any open sockets using the Close method of our Winsock object. The Connect method of our Winsock object allows us to connect to a remote server. Its prototype is shown below:
Both arguments are optional, and can be set manually like this:
wsChat.RemoteHost = "127.0.0.1"
wsChat.RemotePort = 1234
In our application, we are using the value that has been entered into the "txtIP" text box as the remote hosts IP address. We use port 1234, which tells the Winsock library to bind our connection and any data sent to/from us to this port. We don't have to use port 1234, and we can use any port between 1024 and 49151. Ports are divided into three types and numerical ranges. The details of each of these types are shown below:
Well-known ports: The well-known ports are those used internally by Windows to allow us to connect to the Internet, use an FTP service, check our email, etc. They should never be used for alternate purposes and range from 0 through to 1023.
Registered Ports: Registered ports can be used from within our applications and are used by most secondary software services, such as ICQ. They range from 1024 to 49151.
Dynamic/Private Ports: Dynamic/private ports are those that range from 49152 through to 65535. They are not used by windows and should be avoided from within applications and secondary software services.