?
Solved

TCP Chat with Rooms

Posted on 2006-06-26
12
Medium Priority
?
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 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