java - Extend server code so server is able to interact wit clients using multithreading.So the no of clients sim connected the same server -
*cant figure out question above. have tried oracle tutorials , note still unable it. appreciated.
the weight conversion server given on blackboard (conversionservernoconcurrency.java) can interact multiple clients, in sequential manner (that is, new client instance can connect server after server has finished dealing previous client). extend/modify given server code server able interact multiple clients in parallel (concurrently), using multithreading. number of clients simultaneously connected same server should not restricted.
really need code other wise wont able implicate when doing assignment
import java.io.*; import java.net.*; import java.util.scanner; public class conversionservernoconcurrency { private socket s; private scanner in; private printwriter out; public static void main(string[] args) throws ioexception { serversocket server = new serversocket(8888); conversionservernoconcurrency serverinstance = new conversionservernoconcurrency(); system.out.println("server running. waiting client connect..."); while (true) { serverinstance.s = server.accept(); system.out.println("client connected"); serverinstance.run(); system.out.println("client disconnected. waiting new client connect..."); } } public void run() { try { try { in = new scanner(s.getinputstream()); out = new printwriter(s.getoutputstream()); doservice(); } { s.close(); } } catch (ioexception e) { system.err.println(e); } } public void doservice() throws ioexception { while (true) { if (!in.hasnext()) return; string request = in.next(); system.out.println("request received client: \'" + request + "\'"); if (!request.equals("quit")) // end connection client handleconversionrequest(request); } } public void handleconversionrequest(string request) { string amountstr = in.next(); double amount = double.valueof(amountstr); system.out.println("amount received client: " + amount); if (request.equals("convert_to_pounds")) { out.println(amount * 2.2d); // send conversion result client system.out.println("sent conversion result client"); } else if (request.equals("convert_to_kgs")) { out.println(amount / 2.2d); system.out.println("sent conversion result client"); } else system.err.println("unknown request!"); out.flush(); } }
import java.io.*; import java.net.socket; import java.util.scanner; public class conversionclient { public static void main(string[] args) throws ioexception, interruptedexception{ socket s = new socket("localhost", 8888); inputstream instream= s.getinputstream(); outputstream outstream= s.getoutputstream(); scanner in = new scanner(instream); printwriter out = new printwriter(outstream); out.print("convert_to_kgs 123.45\n"); // send 1st request server out.flush(); string response = in.nextline(); system.out.println("received server: " + response); thread.sleep(2000); // delay next request bit out.print("convert_to_pounds 56\n"); // 2nd request out.flush(); response = in.nextline(); system.out.println("received server: " + response); thread.sleep(2000); // delay next request bit out.print("convert_to_pounds 836.98\n"); // 3rd request out.flush(); response = in.nextline(); system.out.println("received server: " + response); out.print("quit\n"); out.flush(); s.close(); } }
writing concurrent connection oriented servers important subject in computer science/software engineering. if don't know how code have understand following.
- in iterative connection oriented server
conversionservernoconcurrency
have, have 1 thread (main thread) receive connections , service connections. - you need separate receiving connection , servicing connections 2 different threads if want develop concurrent connection oriented servers.
- if see
conversionserverconcurrency
below, connection receiving done main thread , delegates connection servicing threads inexecservice
pool. important use thread pool avoid ddos attacks. because thread pool there maximum limit on how many threads can spawned. have @
conversionservertask
has logic service connections.import java.io.*; import java.net.*; import java.util.scanner; import java.util.concurrent.*; public class conversionserverconcurrency { public static void main(string[] args) throws ioexception { serversocket server = new serversocket(8888); system.out.println("server running. waiting client connect..."); executorservice execservice = executors.newfixedthreadpool(10); while (true) { socket clientsocket = server.accept(); execservice.submit(new conversionservertask(clientsocket)); system.out.println("waiting new client connect..."); } } public static class conversionservertask implements runnable { private socket s; private scanner in; private printwriter out; public conversionservertask(socket clientsocket) { this.s = clientsocket; } public void run() { system.out.println("client connected"); try { try { in = new scanner(s.getinputstream()); out = new printwriter(s.getoutputstream()); doservice(); } { s.close(); } } catch (ioexception e) { system.err.println(e); } { system.out.println("client disconnected"); } } public void doservice() throws ioexception { while (true) { if (!in.hasnext()) return; string request = in.next(); system.out.println("request received client: \'" + request + "\'"); if (request.equals("quit")) {// end connection client break; } else { handleconversionrequest(request); } } } public void handleconversionrequest(string request) { string amountstr = in.next(); double amount = double.valueof(amountstr); system.out.println("amount received client: " + amount); if (request.equals("convert_to_pounds")) { out.println(amount * 2.2d); // send conversion result client system.out.println("sent conversion result client"); } else if (request.equals("convert_to_kgs")) { out.println(amount / 2.2d); system.out.println("sent conversion result client"); } else system.err.println("unknown request!"); out.flush(); } } }
Comments
Post a Comment