Reputation: 1017
I just got an assignment.It is to design a java socket server which accepts incoming client connections.The server's job is to accept it and keep track of all the connected clients and it has to advertise the connected client list to all the clients that connect.I am a newbie to Java.I just know that this task has to be completed with the help of threads.I have some knowledge in Java I/O,Networking and Threads.But I am unable to collaborate all these concepts in one single application.It would be helpful if someone would give a model program,or perhaps how to proceed,then it would be helpful.
Thanks, Madhu
Upvotes: 0
Views: 8343
Reputation: 547
Use Apache Mina it will release you from implementing and manage threads and sockets at low level and let you focus on what you want to do.
It may take a day to learn but it really payoff. Mina offers great performance too.
Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract ·event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
Apache MINA is often called:
NIO framework · library, client · server framework · library, or a networking · socket library. However, it's much more than that. Please take a look around the list of the features that enable rapid network application development, and what people says about MINA. Please grab yourself a download, try our Quick Start Guide, surf our FAQ or start join us on our community
Upvotes: 0
Reputation: 52185
This tutorial should show you the basics on concurrent servers. This is another tutorial you might want to look at as well. The second deals more with threading and concurrency.
Upvotes: 4
Reputation: 543
If you want to use plain sockets:
here you can find about java sockets (with the principles for managing it in a multithreaded environment) http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html
and here you can find about multithreading in Java http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html
This is one of the possible solutions, you can design yours in many ways: Every thread manages one connection with the client. You have a main class which accepts connections and keeps the list of active connections At every new connection you fork one thread which manages it. You define a class which manages the thread and which exposes a synchronized method to be notified of new clients. Every time the main class gets a new connection, it informs the other through the method defined at the previous step.
PAY ATTENTION: when you call methods one class which represents a thread (thus it is accessed from a different thread than the one you are calling the method from) you must be careful not to incur in concurrency problems (you can see some examples in the second link I sent).
Upvotes: 0