multithreading - Java Socket client/server pair can only send one message at a time -
a successful conversation can carried out between client , server. if 1 message sent between client , server.
example (works):
client: hello server: helloo client: time server: let me
example (does not work):
client: hello server: helloo server: *how today* server: *test message* client: yes
the messages between asterisks not appear on client side untill client sends 2 messages. understand seems work net-message system number of messages sent server has equal n+-1 number of messages sent client, messages shown.
client side code:
try { socket client_socket= new socket(hostname,port_number); printwriter output = new printwriter(client_socket.getoutputstream(),true); bufferedreader input = new bufferedreader(new inputstreamreader(client_socket.getinputstream())); bufferedreader stdin=new bufferedreader(new inputstreamreader(system.in)); string fromuser,fromserver; while ((fromserver=input.readline())!=null) { system.out.println("server: "+fromserver); if (fromserver.equals("quit")) { break; } fromuser=stdin.readline(); if (fromuser!=null) { system.out.println("client: "+fromuser); output.println(fromuser); } } }
server side code:
try { serversocket server_socket=new serversocket(port_number); socket client_socket= server_socket.accept(); printwriter output = new printwriter(client_socket.getoutputstream(),true); bufferedreader input = new bufferedreader(new inputstreamreader(client_socket.getinputstream())); bufferedreader stdin=new bufferedreader(new inputstreamreader(system.in)); output.println("established"); string fromuser, fromserver; while ((fromuser=input.readline())!=null) { system.out.println("client: "+fromuser); if (fromuser.equals("quit")) { break; } fromserver=stdin.readline(); if (fromserver!=null) { system.out.println("server: "+fromserver); output.println(fromserver); } } }
looking @ code can see why happen, because sop after while
loop initialization stops code waiting 1 line of input, , waits send own sentence, before receiving another. tried putting these within if statements, results in no communication @ all.
i cant seem figure out how fix this.
if requires threads, there way? (i'm not familiar threads)
depending on nature of program, advise use more threads handle messaging.
lets start server: if server can have multiple clients, wise distinguish every client thread, example:
serversocket ss; try { ss = new serversocket(portin); while (true) { socket s = ss.accept(); // option 1: runnable class new thread(new customrunnableclass(s)).start(); } } catch (ioexception e) { e.printstacktrace(); }
or if dont want "session manager" custom runnable class, can replace line new thread(new customrunnableclass(s)).start();
following
new thread(new runnable() { @override public void run() { new someclass().runthisclass(); } }
on client side suggest whatever class listens new messages sockets distinguished new thread:
private void startlistening() { new thread(new runnable() { @override public void run() { for(;;) { string msg = dis.readutf(); system.out.println(" ----- recieved new message -----\n" + msg); (imessagelistener listener : listeners) { try { listener.onmessagerecieved(msg); } catch (exception e) { } } } } }).start(); }
this way client listens message received client , runs method handle them upon receiving, , not hung in meanwhile.
hope helpful.
Comments
Post a Comment