java - Modifying a server to allow multiple clients to connect concurrently -
so i've created server , client previous part of assignment, have extend/modify server can handle multiple clients concurrently. know have along lines of
server server1 = new server(); thread thread = new thread(server1); thread.start();
and have class server implement runnable.
but lecturers notes on multi-threading not clear , have been starring @ part of question long time , have got no where.
below code have written 1 client @ time connect server. appreciated.
server.java
public class server { arraylist<string> tokens = new arraylist<string>(); private socket s; private scanner in; private printwriter out; public static void main(string[] args) throws ioexception { serversocket server = new serversocket(1234); server serverinstance = new server(); 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 start() { system.out.println("starting " + threadname); if (t == null) { t = new thread(this, threadname); t.start(); } } public void run() { try { try { in = new scanner(s.getinputstream()); out = new printwriter(s.getoutputstream()); doservice(); // actual service } { 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: " + request); // (...) test type of request here (not implemented) request(request); } } public void request(string request) { string amountstr = in.next(); if (request.startswith("submit")) { if (tokens.size() < 10) { tokens.add(amountstr); system.out.println("token added"); out.println("ok"); } else { system.err.println("error"); out.println("error"); } } else if (request.startswith("remove")) { if (tokens.contains(amountstr)) { tokens.remove(amountstr); system.out.println("tokens removed"); out.println("ok"); } else { system.err.println("error"); out.println("error"); } } else if (request.equals("quit")) { system.err.println("program ended"); out.println("program ended"); } tokens.sort(null); system.out.println(tokens); out.flush(); } }
client.java
public class client { public static void main(string[] args) throws ioexception { socket s = new socket("localhost", 1234); inputstream instream = s.getinputstream(); outputstream outstream = s.getoutputstream(); scanner in = new scanner(instream); printwriter out = new printwriter(outstream); string request = "submit hello \n"; out.print(request); out.flush(); string response = in.nextline(); system.out.println("token: " + response); s.close(); } }
what trying duplicate rmi has been doing decades. java open source can @ how rmi works , learn there. can on internet myriad examples of multi threaded servers with/without rmi.
Comments
Post a Comment