Home arrow Visual Basic arrow Page 4 - Two Person Chat With The Winsock Control And Visual Basic
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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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 = sckError


Because 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 = sckError

The "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 If


If 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:

Sending a message is easy

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.SetFocus


The 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:

Once the message has been sent, its displayed in our chat 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 -----" & vbCrLf


The 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:

Closing the connection by clicking the close button

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.
blog comments powered by Disqus
VISUAL BASIC ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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