Solved

TCP Chat with Rooms

Posted on 2006-06-26
12
304 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now