Java network with server-client in single model -
i want create network 3 nodes communicate each other. in more detail i'm trying to:
- create server-client model single project copy project in 2 more , there have 3 nodes
- by changing ports ofcourse can make them communicate if split projects in different computers
- after want 1 project send time (with made-up time lag) other 2 nodes , vice-verca other 2 nodes, , find average time.
- i dont want create single servers , single clients, want use threads make unified server-client runs simple "run".
the problems i'm having node starts first finishes first , rest nodes left hanging , in endless loop. , other that i'm stuck no idea do.
so far have made this:
server:
public class server implements runnable{ private int port; private string name; public server(int port, string name){ this.port=port; this.name=name; } public synchronized void run(){ while(true){ try{ thread.sleep(5000); method(); } catch(exception e){ return; } } } public synchronized void method() throws exception{ double sum=0; double average=0; double[] values = new double[10]; serversocket server = new serversocket(port); socket s=server.accept(); inputstream in= s.getinputstream(); outputstream out = s.getoutputstream(); printwriter w = new printwriter(out); scanner r = new scanner(in); for(int i=0; i<10; i++){ string msg = r.next(); sum = double.parsedouble(msg); values[i] = sum; } for(int j=0; j<values.length; j++){ average = average + values[j]; } system.out.println(name+": "+average/10); } }
client:
public class client implements runnable{ private int port; private int id; public client(int port,int id){ this.port=port; this.id = id; } public synchronized void run(){ try{ thread.sleep(5000); method(); } catch(exception e){ return; } } public synchronized void method() throws exception{ int num=0; random ran = new random(10); int d = 0; socket s = new socket("localhost", port); inputstream in= s.getinputstream(); outputstream out = s.getoutputstream(); printwriter w = new printwriter(out); scanner r = new scanner(in); for(int i=0; i<10; i++){ d = ran.nextint(10); system.out.println("client "+id+" sent "+d); w.println(d); w.flush(); } } }
and run them this:
public class networks { public static void main(string[] args) { server s = new server(5000, "server 0"); server s2 = new server(5001, "server 1"); client c = new client(5002, 11); client c2 = new client(5004, 22); thread t1 = new thread(s); thread t2 = new thread(c); thread t3 = new thread(c2); thread t4 = new thread(s2); t1.start(); t2.start(); t3.start(); t4.start(); try{ t1.join(); t2.join(); t3.join(); t4.join(); } catch(exception e){ system.out.println("something"); } } }
you may ignore ports , stuff since have created same project 2 more times testing. method() method testing , results when run 3 projects output first project's clients send nothing , results both servers, second's clients works 1 , 1 server result , @ third project clients send servers return nothing. when output done first project completes , ends , other 2 run ever.
maybe:
- you're swallowing exceptions in client.
- you're starting 4 threads @ same time.
- there race condition client can start , try send on socket before server listening/accepting
- when server come up, client has errored out , server waits forever.
this theory, looks it's case.
Comments
Post a Comment