• home
  • forum
  • my
  • kt
  • download
  • A Great Winsock tutorial ( Part 2) - Multi-user Chat Program ( like IRC )

    Author: 2007-09-06 10:02:12 From:

    Tutorial Description :This is the part 2 of the winsock tutorial for visual basic 6.
    In this part i will show you not just how to connect a client with
    a server, but how to create a multiple connection receiving server,
    and how to make a real chat network , something like IRC

     
    Who should read this :This tutorial targets the beginners in Visual Basic 6.
    This is part 2 so if you know nothing about winsock then
    you must first read the part 1
    Although i will try to explain everything asthoroughly as i can,
    basic knowledge of visual basic 6 or generallyprogramming is required.

     
    What is Winsock :The Winsock we are going to use is an ActiveX that we can add in our
    visual basic program so we can use it's features. When using the internet
    ( like from a web browser ) a lot of things happen behind the scenes.
    Packets are constructed by the soft wares that are then being send through
    routers and others are being received by your Operating System and analyzed
    by the application that send it. In order to do such complicated things a lot
    of in formation like headers, packet size ,hashes ,packet order and many more
    are required to create the packets. You WILL NOT have to deal with that stuff
    using the Winsock control from vb. Continue reading to see just how easy it is
    to effectively use Winsock.

     
    What do you need :For this tutorial you will only need a computer running Windows ,
    Microsoft Visual Basic 6, and the will to learn!

     
    What will this tutorial teach you :In this tutorial i am going to show you how to create a simple multi user chat program.
    The chat program will be just a server and a client, that you can connect
    from the internet ( or LAN ) and simply exchange text messages, but this server
    can receive more than one clients and create a simple chat channel

     
    What are servers and clients : To connect any 2 programs, you need at least one server and one client.
    The server will be the program that opens the ports on the hosting machine
    and receive the connections while the client is called the program
    thatconnects to the remote host. For example, when you connect to Google
    withyour firefox (or  internet explorer), your browser plays the role of the client
    that connects to the hosts that are running at Google. Most common is for the
    servers to be able to receive more than one connections from different clients
    These is called multithreaded socket servers , and this is what I am going to show
    you in this tutorial

     

     
     

     Download the Tutorials Example Source Code  


     
     
    Writting the Client
     
    The client is actually the exact same used in part 1 , since changes have been made (changed a bit one line )
    you can read how to write it at part 1,Here is the Link
     
    Writting the Server
     
    The server in we want to create is much more complex than part 1, since this one will be able to handle
    multiple client , and be responsible to distribute data to clients.

    If you don't know how to create the simple server i suggest you read Part 1  first.

     
    Ok, Here is the Server Form


     
    Now before i get into it , i must first tell you some basic things about control arrays.
    Control arrays is a very nice feature of VB that allows you to have many controls and handle them
    just like you would use an array. Control can be anything ( labels, command buttons, picture boxes , winsock , etc)
    and you can create new controls at runtime, unload them , or use them by saying ControlName(ControlIndex)
    where ControlName is then name of your control eg. label1 , pic1 and so on , and ContolIndex is a number
    which represent the current control in the control array

    I made a simple control demonstration program to see and test how to use them
    You can download it from here

    Note : To create a control array, select a control , for instance a label , and copy it ,
               then try to paste it back to the form. Visual Basic will then ask you
              "You already have a control named 'xxxxxx'. Do you want to create a control array?"
              and click yes


    A lot of hard work is behind everything you see in this site. So if you like it you can help us to keep
    the site as free and open to the public as possible by making a donation (of any amount)

    Ok now back to the winsock thing...

    The logic behind this is to create a server that will be listening on a port. When a connection is requested
    by a client , instead of assigning the connection to the listening socket , a new socket will be created , and
    the connection will be assigned to it , thus keeping the listening socket listening for requests to come...

     
    In the project we will have a global variable SocketCounter that will count how many sockets we made
    and what will be the index of the new one...
    That isn't really necessary because you can always get control count
    using the controls array .count function ( see the control array example above )
     
     
    First the listening button code....
     

    Private Sub bntListen_Click()

    On Error Resume Next
    'close and unload all previous sockets
    For n = 1 To SocketCounter
         sock1(n).Close
         Unload sock1(n)
    Next

    On Error GoTo t

    'sock1(0) is the name of our Winsock ActiveX Control

    sock1(0).Close 'we close it in case it listening before


    'txtPort is the textbox holding the Port number
    sock1(0).LocalPort = txtPort'set the port we want to listen to
    '( the client will connect on this port too)



    sock1(0).Listen'Start Listening


    txtLog = "Listening on Port " & txtPort

    Exit Sub
    t:
    MsgBox "Error : " & Err.Description, vbCritical
    End Sub
     
     



    With the server we need to accept the request from the client before the connection is completed
    To do that we use the sock1_ConnectionRequest that is triggered when a client tries to connect on our host.
    the connection will be completed only if we accept the request. ( command is bellow with bold fonts )
    But remember, we must create a NEW winsock control and assign the connection to it, otherwise it you
    assign the connection to the sock with index 0 (which is our listening sock ) then it will stop listening
    for connection so clients will not be able to connect to the server

    Bellow is the code that handles the connection request
     
     

    Private Sub sock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    'txtLog is the textbox used as our log.

    'this event is triggered when a client try to connect on our host
    'we must accept the request for the connection to be completed,
    'but we will create a new control and assign it to that, so
    'sock1(0) will still be listening for connection but
    'sock1(SocketCounter) , our new sock , will handle the current
    'request and the general connection with the client


    'increase counter
    SocketCounter = SocketCounter + 1

    'this will create a new control with index equal to SocketCounter
    Load sock1(SocketCounter)

    'with this we accept the connection and we are now connected to
    'the client and we can start sending/receiving data

    sock1(SocketCounter).Accept requestID

    'add to the log
    txtLog = "Client Connected. IP : " & sock1(0).RemoteHostIP & " , Client Nick : Client" & sockcounter & vbCrLf

    'tell our client his assigned nickname
    sock1(SocketCounter).SendData "Your Nick is ""Client" & SocketCounter & """"

    End Sub
     




     
    Next we need to write the DataArrival event.
    What is important to remember for this, is that when a client send a text string to the server ,
    it is not meant for the server, but all other clients that are currently connected.
    ( remember we are trying to create an IRC like chatroom )
    So when server get some data from the client ,  it need to redistribute it to all connected clients
    (also informing them who send the text )

    Ok here is the code for the dataarrival event
     

    Private Sub sock1_DataArrival(IndexAs Integer, ByVal bytesTotalAs Long)
    'This is being trigger every time new data arrive
    'we use the GetData function which returns the data that winsock is holding


    Dim dat As String       'where to put the data

    sock1(Index).GetData dat, vbString      'writes the new data in our string dat ( string format )

    'add the new message to our chat buffer
    txtLog = txtLog & "Client" & Index & " : " & dat & vbCrLf

    'now the client says something, wich arrived at the server...
    'the server must now redistibute this message to all other connected
    'clients...

    On Error Resume Next      'Error Handler
    For n = 1To SocketCounter
          IfNot n = Index Then      'we don't want to send the msg back to the sender :)
                If sock1(n).State = sckConnected Then      'if socket is connected
                     sock1(n).SendData "Client" & Index & " : " & dat
                End If
          End If
    Next

    End Sub

     
     
     
    Ok! those are the most crucial parts, there is also theError and Close events but there is nothing special about them,
    you can see them in the example source

     


     Download the Tutorials Example Source Code  

     


     
    This tutorial was written by VirusFree.

    Thank you for reading  it and please excuse my English

    For any problems or question please don't hesitate to post them in our forums
    and i ( or anyone else who can answer them ) will reply as soon as possible
     
     
    Keywords : Visual Basic, winsock, winsock connections, vb, multiuser chat, irc, server, client, winsock tutorial ,
                       visual basic winsock tutorial.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      .NET (8)
      Buttons (3)
      Database Related (7)
      Date and Time (1)
      Development (3)
      Error Handling (2)
      File Manipulation (5)
      Introduction to Visual Basic (9)
      Miscellaneous (2)
      Multimedia (9)
      Networking (9)
      Security (1)
      VB Script (6)

    New

    Hot