Solved

TCP Chat with Rooms

Posted on 2006-06-26
12
303 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
Comment Utility
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
Comment Utility
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
Comment Utility
Does each room get a single instance of a TcpClient?  Or is it shared among rooms?

Bob
0
 
LVL 1

Author Comment

by:PhilippeRenaud
Comment Utility
   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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

11 Experts available now in Live!

Get 1:1 Help Now