Home arrow Visual Basic arrow Page 3 - Two Person Chat With The Winsock Control And Visual Basic

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.

Author Info:
By: Jason Brimblecombe
Rating: 5 stars5 stars5 stars5 stars5 stars / 320
January 12, 2002
  1. · Two Person Chat With The Winsock Control And Visual Basic
  2. · Creating the chat form
  3. · Code from the clients point of view
  4. · Code from the clients point of view (contd.)
  5. · Code from the servers point of view
  6. · Using our chat application
  7. · Conclusion

print this article

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:

Moving to the general declarations section

Add the following line to the general declarations section:

Option Explicit

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!"


Exit Sub

End If

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:

Connect([RemoteHost], [RemotePort])

Both arguments are optional, and can be set manually like this:

wsChat.RemoteHost = ""

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.
IANA has more details about these ports here.

After the Connect method of our Winsock object has been called, we enable the close button and disable the listen, connect and alias controls:

Disabling controls after the connect button is pressed
blog comments powered by Disqus

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials