Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple applet socket connections  RSS feed

 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I took the code that was posted up here and modified it so that it will allow you to connect and disconnect from the server without it exiting. Something more like:
while(true)
{
waitForConnection
while(input!="exit")
{
keepEchoingLines
}
disconnect
}
Hopefully that makes sense. Basically when you connect it keeps echoing until you type "exit" then it goes back to waiting for a connection. Now what I'm wondering is how in the heck do I get this to work for 2 simultaneous connections? For instance if I wanted it to relay information between two users. Can anyone tell me?
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you think about this, you want to be able to handle multiple connections at one time. The way java has to do this is mutlithreading. Create a class that has the input stream and outputstream as objects and implements runnable. Once the server accepts the connection from the client, it creates an object of your class and starts it. At this point, one thread will handle the communication with the first client and the server will continue to listen for additional requests.

------------------
Hope This Helps
Carl Trusiak
 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Yeah I'd actually already figured that out and have implemented a server that allows an unlimited number of connections. The problem is, I don't have the first clue on how to communicate between the threads. Each of them is just an echo server (slightly enhanced to look for certain commands, like 'exit'). What I don't know how to do is pass chat messages from one thread when it is received to the out of all the other threads. If anyone can enlighten me I'd appreciate it.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The obvious next step There are several ways to go about this.
Sun has as part of the Custom Networking Tutorial a BroadCast Server
If you'd still like to try this on your own here are the steps I would take.
1) create an class that extends Observable and implement it with a Vector of Strings.
2) off the server thread you have, have it create a send send that registers as an Observer to the above.
3) anytime your server recieves a new message, place it in your obsrver an issue a change notice. Whenever your thread in 2 recieves notification of a change, it gets the change from the observable object and sends it to it's connection.
Sun has a good example of Obserable Objects

------------------
Hope This Helps
Carl Trusiak
 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little confused. I'm going to go ahead and try to put this to work in the mean time, but i figured I'd get my questions up here so you could answer them when you are on. =)
It seems to me that this is going to be a big problem if I try to take this up to 3 users. For instance, when thread1 sends a new message, thread2 and thread3 get notifications and go to check the state. How do they know how 'much' of the state to get? I mean, it's conceivable that when thread2 goes there, it only needs the last line, while thread3 might need the last 2 lines of text. Unless I make an observable for each thread to thread that will talk. Which would be fine for 2 or 3 threads, but what if I had 20? Wouldn't that be like 20*19*18... observables? Somehow the threads need to know which of the data they should fetch, and which they should leave there. This would also be an issue when it came to allowing users to message a particular user.
I hope that my questions/thoughts make some sense to you.
 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm ok after playing around with it for not very long, I think most of what I was worried about is a non factor. Gonna have to mess around a little more then maybe I'll be back with some questions. =)
Actually I was wondering one thing. Here's what I'm doing:
1. Server thread creates a message store (observable)
2. When a new connection comes in, a thread is created. The thread extends observer, and the server adds it to observable.
3. The server passes the message store to the thread.
My question is if there is any other way for the connection threads (observers) to reference the server thread or the message store (observable). In your explanation you said to have the server send the message. The messages are coming from the connection threads, to the connection thread. The only thing that has a reference to the message store is the server thread (unless I pass it to the connections). Is that the way to do it or is there something else?
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
As Granny says Inside of every large program is a small program struggling to get out.
You have so far identified several items that need addressed. If you think about it, there is a solution for each of your problems. However one step at a time. Get the Observer pattern working then addressing which messages a Thread needs to send to a client becomes a matter of a data element keeping track of the last message sent. To keep track of who the message is for, a data element is needed in the message store. It can be set to 'ALL' or a particular user. If a Thread check a message and it's not for 'ALL' or their user, don't send it.
The next that will crop up is memory requirements o the server but, That's the next small program that wants to come out.

------------------
Hope This Helps
Carl Trusiak
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!