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

    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 incoming message is displayed in our txtIn text box

    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:

    If Number <> 0 Then

    AddText "----- Error [" & Description & "] -----" & vbCrLf, txtIn

    Call cmdClose_Click

    End If


    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.

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