Two Person Chat With The Winsock Control And Visual Basic - Code from the clients point of view (contd.)
(Page 4 of 7 )
The Connect method of our Winsock object handles the connection loop. Switch back to your form using Shift+F7 and double click on the Winsock control on frmMain. Use the drop-down list at the top of the code window to selects its Connect method. Enter the following code:
Do
DoEvents
Loop Until wsChat.State = sckConnected Or wsChat.State = sckErrorBecause the Winsock control has to connect to the remote computer using either the local network or the Internet, we need to implement a control loop that allows it to process its tasks before continuing. We use a do...loop control to take care of this. The loops conditional statement is:
Until wsChat.State = sckConnected Or wsChat.State = sckErrorThe "state" member of our Winsock object is a numerical representation of our connection. If it is sckConnected, then the Winsock library connected to the remote computer successfully. If it's sckError, then an error occurred while trying to connect to the remote computer.
The last part of our Winsock objects Connect method that you should enter is some simple event handling:
If wsChat.State = sckConnected Then
'Tell the user that the connection has been established
AddText "----- Connection Established -----" & vbCrLf, txtIn
cmdSend.Enabled = True
txtName.Enabled = False
txtOut.SetFocus
Else
'Tell the user that the connection has been established
AddText "----- Connection Failed -----" & vbCrLf, txtIn
End IfIf the Winsock object was able to connect to the remote server, then we use the AddText method to add text to our message window telling us that we are connected. If the connection failed, we use the AddText method to tell us that the connection failed.
The AddText method is a custom sub-routine, and simply adds text to a text box. Its function prototype looks like this:
Private Sub AddText(ByVal text As String, ByRef Box As TextBox)Its code look like this:
'Take the text box passed as a reference and
'add the "text" variable to it
Box.text = Box.text & text & vbCrLf
Box.SelStart = Len(Box.text)Once we're connected, we're ready to send a message to the remote computer. To do this, we simply type some text into the outgoing text box and click send, like this:

We need to add some code behind the send message button, so double-click on it and enter the following code:
'Send the data to the remote user
wsChat.SendData "[" & txtName.text & "] " & txtOut.text
AddText "[" & txtName.text & "] " & txtOut.text, txtIn
'Clear out typed text and refocus on the box
txtOut.text = ""
txtOut.SetFocusThe SendData method of our Winsock object handles the sending of data to the remote computer. It takes just one argument, which is the data to send. Once the data has been sent, we display what we’ve just sent in the chat text box:

The last method that we need to add to our chat application from the client's perspective is the code behind the close button. Double click on the close button and enter the following code:
'Disable the outgoing buttons and tell the user
'that the connection has been closed
wsChat.Close
cmdClose.Enabled = False
cmdSend.Enabled = False
txtName.Enabled = True
cmdListen.Enabled = True
cmdConnect.Enabled = True
txtIn.text = "----- Connection Closed -----" & vbCrLfThe code behind the close button calls the close method of our Winsock object. When Winsock is told to close its connection, it also informs the remote computer that the connection has been closed.
Next, we disable the close and send buttons, enable the alias, listen and connect controls, and write a message to our chat box, letting the user know that the chat has been terminated:

Now that we've taken a look at the methods we need to implement to connect to a remote computer and send messages to it, let's take a look at the methods the remote computer needs to use to listen for a connection and accept messages.
Next: Code from the servers point of view >>
More Visual Basic Articles
More By Jason Brimblecombe