This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I'm considering doing a project that involves collaborative client/server application involving chat, whiteboard, VoiP, etc.
My question is this...would each aspect of the project (chat, whiteboard,etc) require that it run on a seperate port to the server? Is that normal from a design perspective? I'm thinking a seperate thread on the server listening on different ports: one for the chat (port 2000), one for the whiteboard(port 2001), one for VoiP(port 2002), etc.
I'm only thinking of this approach as being favourable because the types of data being moved back and forth would be different on each port. Does this make sense? Are there some other approaches to this type of project?
I don't think there's any problem with using several ports that way. A single port might make life a lot easier if you have to get them opened on a firewall. It also simplifies what clients have to know about the server.
You can accept all request messages on one port and dispatch them to appropriate subsystems based on content. Read up on Strategy Pattern and see if this make sense:
The header might be CHAT or WHITEBOARD. The HandlerFactory might just be a HashMap where CHAT returns a ChatRequestReader. I like factories like that because you can add new headers and handlers through configuration without touching the dispatcher code.
Does that sound useful? Are you comfortable managing multiple threads for inbound requests?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Sep 24, 2003
I've used multi-threading before in standalone apps but not when trying to configure a server app.
Perhaps you can direct me to a tutorial or something that explains your idea in greater detail?
Joined: Jan 29, 2003
The classic socket server does something like:
This runs each new request on a new thread. In Java5 and later with only a couple more lines you can run requests on a thread pool to reduce thread setup and teardown. Does that look like something you can use?
Joined: Sep 24, 2003
Originally posted by Stan James:
I'm more interested in this snippet of code. This seems to suggest that I can somehow read each packet as it comes in and determine if is of a particular protocol. Is that possible? Certainly it would be good to read the packets as they arrive so that I can then determine to which handler to send them.