Visual Basic
  Home arrow Visual Basic arrow Page 4 - Two Person Chat With The Winsock Control A...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Two Person Chat With The Winsock Control And Visual Basic
By: Jason Brimblecombe
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 278
    2002-01-12

    Table of Contents:
  • Two Person Chat With The Winsock Control And Visual Basic
  • Creating the chat form
  • Code from the clients point of view
  • Code from the clients point of view (contd.)
  • Code from the servers point of view
  • Using our chat application
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Visual Basic Articles
    More By Jason Brimblecombe


       · You say you can download it @ the last page ...No download or what so ever
       · dude the point of coding isn't to just download people's full program then change...
       · LIAR...On the first page of the tut, you SAID that the download is at the last page....
       · You are right loozahhh..patience is the point..
       · I apologize for sounding like an anonymous loozah, but I am having some issues with...
       · the code:additemdosent work. is there any declarations for it or sumthin plz...
       · nvm i figured it out but i cant get the other ppl to see that i disconnected, have a...
       · How Did you fix? @person above me
       · like A chat with B in one window , and chat with C in other indow??
       · you say that "To use our chat application, simply download the support material from...
     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT