Link to home
Start Free TrialLog in
Avatar of neonlights
neonlights

asked on

CREATE MY CHAT PROGRAM ON MY VB PROGRAM::::::

Hi everyone:

I have a VB program. Multiple user system. At times, there will more than 10 agents will be using the program. What I would like to do is:

CREATE a chat program in my VB Program. I do not want to create extra window for it. This chat window will be part of my program, and will have combo box with list of agents on line. Then, if one agents want to chat with another agent, she or he will select the appropriate agent from the combo box, and start to type message. So....both agents can see their chat messages on VB program...

HOW DO I DO IT....
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Here is my server base.  Create a new Project and add a Winsock Control.  Set its Index to 0 to make it a control array.

Option Explicit

Private inputBuffer As String
Private connections As Integer

Private Sub Form_Load()
    Me.Caption = "Server - Listening..."
    connections = 0
   
    ' channel 0 is the only listening port
    Winsock1(0).LocalPort = 10001
    Winsock1(0).Listen
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim a As Integer
   
    ' shut everything down
    For a = Winsock1.UBound To 0 Step -1
        If Winsock1(a).State <> sckClosed Then
            Winsock1(a).Close
        End If
    Next a
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim acceptIndex As Integer
    Dim curindex As Integer
   
    ' See if there is an available Channel already loaded
    acceptIndex = -1
    If Index = 0 Then
        For curindex = 1 To Winsock1.UBound
            If Winsock1(curindex).State = sckClosed Then
                acceptIndex = curindex
                Exit For
            End If
        Next curindex
    End If
   
    ' Load a channel if none available
    If acceptIndex = -1 Then
        acceptIndex = Winsock1.Count
        Load Winsock1(acceptIndex)
    End If
   
    ' Accept connection on Channel decided upon
    Winsock1(acceptIndex).Accept requestID
   
    ' Increase number of connections
    connections = connections + 1
    Me.Caption = "Server : " & connections & " Connection(s)"
End Sub

Private Sub Winsock1_Close(Index As Integer)
    Winsock1(Index).Close
   
    ' Decrease Total number of connections
    connections = connections - 1
    If connections > 0 Then
        Me.Caption = "Server : " & connections & " Connection(s)"
    Else
        Me.Caption = "Server - Listening..."
    End If
   
    ' attempt to release unneeded Winsock Controls from end of array
    Do While Winsock1(Winsock1.UBound).State = sckClosed
        Unload Winsock1(Winsock1.UBound)
    Loop
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim inputString As String
   
    ' add current command to buffer and process the buffer
    Winsock1(Index).GetData inputString, vbString
    inputBuffer = inputBuffer & inputString
    processInputs
End Sub

Private Sub processInputs()
    Dim markerPos As Integer
    Dim curInput As String
    Dim values As Variant
   
    ' see if we have a msg in our buffer
    markerPos = InStr(inputBuffer, Chr(1))
    Do While markerPos > 0 ' if we have a msg then process it
        curInput = Left$(inputBuffer, markerPos - 1) ' get msg from buffer
        inputBuffer = Mid(inputBuffer, markerPos + 1) ' remove msg from buffer
       
        values = Split(curInput, Chr(0)) ' split msg into command and its values
       
        Select Case values(0) ' what command was it?
            Case "a"
           
            Case "b"
           
            Case "c"
           
            Case Else
               
        End Select
       
        markerPos = InStr(inputBuffer, Chr$(1)) ' any more msgs in buffer?
    Loop
End Sub
Here is my Client base.  Create a new project and add a Winsock control.

Option Explicit

Private IP As String
Private port As Long
Private inputBuffer As String

Private Sub Form_Load()
    Me.Caption = "Client - Connecting..."
    IP = "127.0.0.1"
    port = 10001
   
    ' attemp to connect to the server
    Winsock1.Connect IP, port
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim f As Form
   
    Winsock1.Close
    For Each f In Forms
        Unload f
    Next
End Sub

Private Sub Winsock1_Close()
    Me.Caption = "Client Disconnected"
End Sub

Private Sub Winsock1_Connect()
    Me.Caption = "Client Connected"
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim inputString As String
           
    ' add msg to buffer and process it
    Winsock1.GetData inputString, vbString
    inputBuffer = inputBuffer & inputString
    processInputs
End Sub

Private Sub processInputs()
    Dim markerPos As Integer
    Dim curInput As String
    Dim values As Variant
   
    ' see if we have a msg in our buffer
    markerPos = InStr(inputBuffer, Chr(1))
    Do While markerPos > 0 ' if we have a msg then process it
        curInput = Left(inputBuffer, markerPos - 1) ' get msg from buffer
        inputBuffer = Mid(inputBuffer, markerPos + 1) ' remove msg from buffer
       
        values = Split(curInput, Chr(0)) ' split msg into command and its values
       
        Select Case values(0) ' what command was it?
            Case "a"
                     
            Case "b"
               
            Case "c"
               
            Case Else
               
        End Select
       
        markerPos = InStr(inputBuffer, Chr$(1)) ' any more msgs in buffer?
    Loop
End Sub
Now you just need to come up with your own protocol of commands to send back and forth.  I have used Chr(0) as a parameter delimeter and Chr(1) as a command delimeter.

Here is one example:

' Client Side telling Server what our UserName is...
Dim cmd As String
cmd = "UserName" & Chr(0) & "John" & Chr(1)
Winsock1.SendData cmd

On the server side you would add "UserName" to the Select Case statement in the processInputs() sub and and place your supporting code in the Case section.

A RichTextBox would probably be best to show the ongoing Chat as you will be able to place messages the user typed in one color and messages received from another user in a different color.  A separate TextBox can be used to allow the user to compose the message to be sent.

Let us know which part you want to tackle first.

Idle_Mind
Avatar of neonlights
neonlights

ASKER

Hi Idle_Mind,

Thank you so much for your message.

I just want to clarify that with this:

I will have a combo box in my main form with the list of available agents. and button for chat. and txtbox in my form.
So, user does not have to switch many windows. only one window contains all...Is it possible with your code?

So, I want to chat with the other person in this room, I select the person from the list, and click chat....
How do I get Rich Text Box""""?
Hi Idle_Mind,

I have a question for you:::::

With the chat what I would like to achive is:

Any one can send a message to anyone at any time.....
So, If 4 people sending message at the same time, it will list their uesr name, and their message. I am wondering whether I can achieve this goal by your method. Almost like NetMeeting or MSN Messenger, but, built in my VB Program, one form, does it all.....

Let me know, I really appreciate your help.

Thanks
Absolutely.  It can all be achieved with the code I gave you.

You will have to do a lot of modifications as the code I posted is simply the basis for sending winsock messages back and forth between the client and the server.

To get the RichTextBox go to Project --> Components and check "Microsoft Rich TextBox Control".

I will whip it up for you and post it.

Idle_Mind
Thanks Idle_Mind.

Currently, what I am doing is:

In my main form, each user will type a message and hit send. It will store it in the db, one table, it contains, msgforwho, whosend, and msg.

And there is tmr function will check if they have any message for them, if they do, it will list it in the rich text box. But, I will wait for yours...because, your idea is more like chat, rather than mine.

Thanks again, I really appreciate it.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much for your work.

Do I have to specify what is that IP number of each PC???? How will the server knows...and how will the client knows? I am sorry if I am asking silly question.

Hi, it is working very very sweet.....Thank you very much.

Thanks for your all help!!!
So, Is that mean, I just have to run this client programs in each pc...that is it...?????
Exactly.

The client only needs to know the IP and Port number of the server program which hopefully is running on a dedicated machine.  I'm guessing you already have one since you already have multiple clients connecting to some kind of database.

The clients never know the IP addresses of the other clients as that information is handled by the server.

Idle_Mind
The Server Port is specified in the server Form_Load Event:
    Winsock1(0).LocalPort = 10001
    Winsock1(0).Listen

The client connects to the server in the Form_Load Event as well:
    Private Sub Form_Load()
        IP = "127.0.0.1" ' Local Machine
        port = 10001
        Winsock1.Connect IP, port
    End Sub

Each control in the server winsock array knows who it is connected to when you Accept the connection.

Idle_Mind
Thanks Idle_Mind for your enormous help!!!!
Not a problem...I love winsock control apps.

Idle_Mind