TCP Chat with Rooms

Hi guys, little problem with my Chat application.
For introduction, I got my chat working (execpt for 2-3 exception popping on immediate window, but thats for later)
Before I wrote a specific private sub, When I was opening for example: Room 1 and 3, then writing "Test" in Room 1, the message after pressing the button Send was going at Room 1 AND 3.

So it seems that the message would go everywhere. I fix that with a little compare with : ok if the Sender room is equals to the receiver room then Write the message, else do not.  But it is quite cheating: Its fine for 3 rooms, but If We have 500 rooms or even 4000 , it will just lag as hell.

So my problem goes as follow: The message needs to stay at the Right Room. That meens what? When I create my SocketClient, Do I have to change the port ? So each table would have uniquely chats ??  (I GUESS NOT)

Or Do I have to create multiple TCPListener ? (I guess not..)

My Room_Load goes as follow:

    Private mobjClient As TcpClient
    ...

    Private Sub frmRoom_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mobjClient = New TcpClient("localhost", 5000)
        mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
        Send(username + " as joined. " + CStr(tableID))
    End Sub


Ok so here I create a new TcpCLient each time a room is going open, always on port 5000.
What do I have to do to fic the problem of the message needs to stay at the right place ?


thank you
Phil

LVL 1
PhilippeRenaudAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Given that description, than this is what I was proposing:

   You need to send a message to room 2.  So get the form instance from the Hashtable, and send a message:

      Public Sub SendMessageToRoom(ByVal roomNumber As Integer, ByVal message As String)
         Dim frm As frmRoom = m_hashRooms(roomNumber)
         frm.SendMessage(message)
      End Sub

Bob
0
 
Bob LearnedCommented:
Preliminary questions:

1) .NET version?  2002, 2003, 2005?

2) What is the model for the application?  Multiple sender classes, multiple receiver classes, main form?

Bob
0
 
PhilippeRenaudAuthor Commented:
Hi Bob,

1) Im using Visual Studio 2005

2) Well I have 1 project for the Server (wich has a form and 1 class)  (The Listener)

And another prjoect for the Main app (client) , wich in the form  frmRoom  is the chat.
in this form i got all the code for client as you probably know.
in frmMain I got a list of room available. if the user Dbl-click on one, well it pops the clicked frmRoom.

I am actually using alot of this sample of Microsoft:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet08282001.asp



0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Bob LearnedCommented:
Does each room get a single instance of a TcpClient?  Or is it shared among rooms?

Bob
0
 
PhilippeRenaudAuthor Commented:
   Private mobjClient As TcpClient

    Private Sub frmRoom_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mobjClient = New TcpClient("localhost", 5000)
        mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
        Send(username + " as joined. " + CStr(tableID))
    End Sub

I am creating a new TcpClient on each room that is going to Open. The same user cannot open the same room twice.
He needs to close it first then re-open.

0
 
Bob LearnedCommented:
You could have a Hashtable that stores the room # and the room form instance, so that you could quickly look up the corresponding form when you need to send a message to a specific room.

Bob
0
 
PhilippeRenaudAuthor Commented:
a Hashtable in the chat form? (frmRoom) ?


In my Server side, I am doing something with the Hashtable.:


Private mcolClients As New Hashtable

    Private Sub DoListen()
        Try
            mobjListener = New TcpListener(5000)
            mobjListener.Start()
            Do
                Dim x As New Client(mobjListener.AcceptTcpClient)

                AddHandler x.Connected, AddressOf OnConnected
                AddHandler x.Disconnected, AddressOf OnDisconnected
                AddHandler x.LineReceived, AddressOf OnLineReceived
                mcolClients.Add(x.ID, x)
                Dim params() As Object = {"New connection"}
                Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
            Loop Until False
        Catch
        End Try
    End Sub
0
 
Bob LearnedCommented:
From the main form (frmMain), you create an instance of a frmRoom.  In frmMain, you could create an instance of the form, and store it like this:

Private m_hashRooms As New Hashtable

...


   Dim frm As New frmRoom(roomNumber)
   m_hashRooms.Add(roomNumber, frm)

Bob
0
 
PhilippeRenaudAuthor Commented:
I see. Okai, im just not sure how it could help when the boy clicks on the Send button to ship his message.
im sure you're right, but I cant see the light
0
 
Bob LearnedCommented:
>>but If We have 500 rooms or even 4000 , it will just lag as hell.
Describe what you mean by this?

Bob
0
 
PhilippeRenaudAuthor Commented:
Ok,

If the application wasnt comparing the RoomID just before writing the chat text, All rooms would receive the message of any room. Not good.

What I did, is that I did a compare of the RoomID from the btn_Send_CLick then I compare this to the Room of the user.
Then I fix the problem.  but NOT completely because I by-pass the problem. You see, If there is 4000 Rooms open, the app will compare 4000 times. Its absolutly not the beat approach. Im sure of that

SO I need to fix that in a way so the Application will not compare, but already know that the Message is for example Room 2 and only send the mesage to all the Rooms 2 opened. (and not compare all rooms to see if it is 2)


0
 
PhilippeRenaudAuthor Commented:
Ok know I think i undestand what you mean..
Yes you're right, i think its the only way.

Thank you Bob
0
All Courses

From novice to tech pro — start learning today.