Home arrow Visual Basic arrow Page 5 - 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 servers point of view
(Page 5 of 7 )

For our chat application to run, it needs to have two instances available at the same time: one for the "client" and another for the "server". The client enters the servers IP address in the text box and clicks connect. Meanwhile, at the other end, the person running our chat app as the server clicks on the listen button and waits for a connection attempt from a client's machine.

We need to implement code for the cmdListen_Click() event. Double click on the listen button and enter the following code:

If txtName.text = "" Then

MsgBox "You must enter an alias first!", vbCritical, "Error!"


Exit Sub

End If

'Place the local IP into this textbox and wait for a chat invitation.

txtIP.text = wsChat.LocalIP


wsChat.LocalPort = 1234


cmdClose.Enabled = True

cmdListen.Enabled = False

cmdConnect.Enabled = False

txtName.Enabled = False

AddText "----- Waiting for Connection -----", txtIn

From the server's perspective, we don't care about the clients IP address. Any client that is running our application and has access to the Internet can connect to us, so we display our IP address in the text box. Next, we use the LocalPort member and the Listen method to tell Winsock which port to listen for connection requests on.

As with the client's implementation of the Connect method, we disable the listen, connect and alias controls, and enable the close button. We also use the AddText method to let us know that our chat application is waiting for a connection:

Waiting for a connection

Once a connection attempt has been detected, Winsock automatically calls the ConnectionRequest method. The prototype of the ConnectionRequest method looks like this:

Private Sub wschat_ConnectionRequest(ByVal requestID As Long)

It accepts just one argument (which is automatically provided by Winsock), which is a unique identifier representing the connection request from the client.

Switch to the code window and select the ConnectRequest method of our Winsock object. Enter the following code:


wsChat.Accept requestID

'If the remote system requests a connection, accept it and connect

AddText "----- Connection Established -----" & vbCrLf, txtIn

'Tell the user that the connection has been established

cmdSend.Enabled = True

txtName.Enabled = False


Our ConnectionRequest method starts by closing any previously open sockets. The Accept method tells Winsock that we would like to accept the attempting connection from the remote computer. We use the AddText method to show us that a connection has been established, and then enable the send button whilst disabling the alias text box.

Now that we're connected, we can send and accept messages. When a message is sent to us from the remote computer, the DataArrival method of our Winsock object is called automatically. Its prototype looks like this:

Private Sub wschat_DataArrival(ByVal bytesTotal As Long)

It accepts one parameter, which is automatically provided by Winsock: the size of the incoming data.

Add the following code to the DataArrival method of our Winsock object:

Dim incoming As String

'Tell the Winsock control to place the incoming data into a string.

'Then call the function to print the data into the "Incoming Data" textbox.

wsChat.GetData incoming

AddText incoming, txtIn

The DataArrival function uses the GetData method of our Winsock object to actually retrieve the data from the buffer. This data contains the message posted from the remote computer. We use the AddText method to display the incoming message:

The incoming message is displayed in our txtIn text box

The last part of our chat application that I want to discuss is the Error method. Whenever Winsock encounters an error, it automatically calls its Error method. The prototype of the Error method looks like this:

Private Sub wschat_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

When the Error method is called, Winsock automatically provides it with all of the arguments listed above. We are only interested in the Description argument. If a Winsock error occurs in our chat application, it is output using the AddText method, like this:

If Number <> 0 Then

AddText "----- Error [" & Description & "] -----" & vbCrLf, txtIn

Call cmdClose_Click

End If

As you can see from the code above, we check if the errors number isnít zero. If it is, then no error has occurred and Winsock is just passing dummy data around: no error has really occurred.
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