Solved

TCP Chat with Rooms

Posted on 2006-06-26
12
306 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:PhilippeRenaud
  • 6
  • 6
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16985460
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 16986399
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16986408
Does each room get a single instance of a TcpClient?  Or is it shared among rooms?

Bob
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 16986445
   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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16986488
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 16986542
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16986606
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 16986659
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16987037
>>but If We have 500 rooms or even 4000 , it will just lag as hell.
Describe what you mean by this?

Bob
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 16987194
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16987218
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 16987243
Ok know I think i undestand what you mean..
Yes you're right, i think its the only way.

Thank you Bob
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question