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 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!"
txtName.SetFocus
Exit Sub
End If
'Place the local IP into this textbox and wait for a chat invitation.
txtIP.text = wsChat.LocalIP
wsChat.Close
wsChat.LocalPort = 1234
wsChat.Listen
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:
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.Close
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
txtOut.SetFocus
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 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:
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.