Visual Basic
  Home arrow Visual Basic arrow Page 3 - 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


    (Page 3 of 7 )

    Our chat application uses the client/server model, yet functions as both a client and server itself! This page will look at the methods used to connect to the remote computer (AKA connecting to the server).

    The chat form that I've just shown you is useless unless it actually functions. The Winsock control that we have added to our chat form exposes several methods that we can override to both capture and send data from and to another chat user. The details of these methods are shown below:
    • Close: This method is called whenever a connection between your computer and another computer is terminated
    • Connect: This method handles connecting to a remote computer using either UDP or TCP and a specified port
    • ConnectionRequest: This method is called whenever the Winsock library detects that another computer is requesting a connection to yours
    • DataArrival: This method is called when data arrives from a remote computer
    • Error: This method is called whenever the Winsock library encounters an error during its operations
    • SendComplete: Called when a block of data is successfully sent to another computer using the SendData method
    • SendProgress: This method allows you to monitor the progress of a send operation
    Let's start adding our code. Double click on the frmMain form and go to the general declarations section of the code window by changing the options in the two drop-down lists at the top of the window, like this:

    Moving to the general declarations section

    Add the following line to the general declarations section:

    Option Explicit

    The "Option Explicit" command simply tells Visual Basic not to compile our application if any variables are used, but not dimensioned.

    Now, let's add the code behind our cmdConnect_Click method, which will actually attempt to establish a connection to the remote computer:

    'Before we can connect, we should check to see

    'if there is an IP and name for the user.

    If txtIP.text = "" Or txtName.text = "" Then

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

    txtName.SetFocus

    Exit Sub

    End If

    On Error Resume Next



    'Connecting the IP that is placed in the txtIP.text value.

    wsChat.Close

    wsChat.Connect txtIP.text, 1234



    cmdClose.Enabled = True

    cmdListen.Enabled = False

    cmdConnect.Enabled = False

    txtName.Enabled = False


    The cmdConnect_Click() method starts by making sure that the user has entered both an IP address and alias to use during the chat session. If they haven't, then a message box is displayed and control returns to the application.

    Next, we close any open sockets using the Close method of our Winsock object. The Connect method of our Winsock object allows us to connect to a remote server. Its prototype is shown below:

    Connect([RemoteHost], [RemotePort])

    Both arguments are optional, and can be set manually like this:

    wsChat.RemoteHost = "127.0.0.1"

    wsChat.RemotePort = 1234


    In our application, we are using the value that has been entered into the "txtIP" text box as the remote hosts IP address. We use port 1234, which tells the Winsock library to bind our connection and any data sent to/from us to this port. We don't have to use port 1234, and we can use any port between 1024 and 49151. Ports are divided into three types and numerical ranges. The details of each of these types are shown below:
    • Well-known ports: The well-known ports are those used internally by Windows to allow us to connect to the Internet, use an FTP service, check our email, etc. They should never be used for alternate purposes and range from 0 through to 1023.
    • Registered Ports: Registered ports can be used from within our applications and are used by most secondary software services, such as ICQ. They range from 1024 to 49151.
    • Dynamic/Private Ports: Dynamic/private ports are those that range from 49152 through to 65535. They are not used by windows and should be avoided from within applications and secondary software services.
    IANA has more details about these ports here.

    After the Connect method of our Winsock object has been called, we enable the close button and disable the listen, connect and alias controls:

    Disabling controls after the connect button is pressed

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek