Servers are supposed to run for a long time without stopping

Servers are supposed to run for a long time without stopping; therefore, they must be designed to provide good service no matter what their clients do. Examine the server examples TCPEchoServer.java and list anything you can think of that a client might do to cause it to give poor service to other clients. Revise TCPEchoClient.java to implement them. Suggest improvements to fix the problems that you find. Revise TCPEchoServer.java to implement them.

Solution

If you want to disrupt the server, just do while(true) {out.write(bytebuffer);} in one client, other clients will never receive the message. It means you are keep sending the message and server will keep accepting it. There is no solution on server side to accept the other client connections. What is the solution?

We need to allow multiple connection on server side such that no client can interfere with other client. How to do that? Thread is the solution. Thread will allow you to get the incoming client connection in different threads. For small example but not much well visible you can take human as process and their hands as threads. With right hand you can write and left you can eat, do some things on mobile etc. Both different work. So we just accept client connection on different thread.

I have created a new class which will do basic reading and writing of content. you will pass the accepted clientconnection to its constructor and it will run in separate thread.

I have used ExecutorService which is same as thread with lot of features and have assigned maximum threadpool to use as 12. So when you have 12 client continue writing to server then it will not accept 13 one. You can try by giving it one thread. I hope you will understand the code and concept, if have any doubt ask me.

Servers are supposed to run for a long time without stopping; therefore, they must be designed to provide good service no matter what their clients do. Examine

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site