multithreading - Read/write in simple client-server app in Java -
i'm new java , i'm trying learn threads , socket. decide make simple client-server application following official java tutorial. idea simple - server wait connection, if appears, makes new thread new socket, input , output. client side -> make connection; new thread socket, input, output , stdin (to read line , after send server). wrong (don't have idea why) code. connection established, there's no exceptions. explain why doesn't work , how fix it? have suggestions code (probably it's not best practices , things that):
client side: public class client { private bufferedreader reader; private socket sock; private printwriter writer; public static void main(string[] args) { client client = new client(); client.go(); } public void go() { setupnetworking(); } private void setupnetworking() { try{ sock = new socket("127.0.0.1", 5000); system.out.println("network established"); serverthread serverthread= new serverthread(sock); serverthread.start(); system.out.println("type message: "); } catch (ioexception e) { system.out.println("problem establishing network: " + e); } } class serverthread extends thread { socket socket; printwriter out; bufferedreader in; bufferedreader stdin; serverthread(socket socket) { this.socket = socket; try{ out = new printwriter(socket.getoutputstream()); in = new bufferedreader(new inputstreamreader(socket.getinputstream())); stdin = new bufferedreader(new inputstreamreader(system.in)); }catch (ioexception e) { system.out.println("problem trying read/write server: " + e); } } @override public void run() { string fromserver; string fromclient; while(true){ try{ if((fromserver = in.readline()) != null) system.out.println(" " + fromserver); else if((fromclient = stdin.readline()) != null) out.println(fromclient); }catch(exception e) { system.out.println("msg exception: " + e); } } } } }
server side:
public class server { //run server until keepgoing = false private boolean keepgoing = true; public static void main(string[] args) { server server = new server(); server.go(); } public void go() { try { serversocket serversocket = new serversocket(5000); while(keepgoing) { socket clientsocket = serversocket.accept(); clientthread t = new clientthread(clientsocket); t.start(); } } catch (ioexception e) { system.out.println("problem socket/network: " + e); } } class clientthread extends thread { socket clientsocket; printwriter out; bufferedreader in; clientthread(socket clientsocket) { this.clientsocket = clientsocket; try{ out = new printwriter(clientsocket.getoutputstream()); in = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); } catch (ioexception e) { system.out.println("problem creating in/out: " + e); } } @override public void run() { string message; while(keepgoing) { try{ message = in.readline(); out.println(message); system.out.println(message); } catch (ioexception e){ system.out.println("exception while try read line: " + e); } } } } }
ps i've changed bit code - instead of made clientthread class, made new runnable class , pass variable thread class. inspired question: "implements runnable" vs. "extends thread".
i think problem both server , client waiting input. server:
message = in.readline();
client:
if((fromserver = in.readline()) != null) system.out.println(" " + fromserver); else if((fromclient = stdin.readline()) != null) out.println(fromclient);
but client code blocks on fromserver = in.readline()
part, never gets read standard in, , nothing sent out server.
you move attempt read standard in setupnetworking
method, right after system.out.println("type message: ");
. build loop there exit if user types "exit" or "quit" or that:
bufferedreader stdin = new bufferedreader(new inputstreamreader(system.in)); string read = ""; { read = stdin.readline(); system.out.println("read stdin: " + read); serverthread.send(read); } while (!read.equals("exit"));
the serverthread.send()
method simple:
void send(string string) { system.out.println("sending server: " + string); out.println(string); }
however, make work, either have flush stream manually after writing out, or use following constructor:
out = new printwriter(socket.getoutputstream(), true);
see printwriter's javadoc: true means auto-flush on newline.
i tested setup , worked me. able send client server.
however, first step. implement both reading , writing separate threads, both client , server. , there no graceful shutdown of sockets implemenented yet. more complete yet simple example can found on oracle.
Comments
Post a Comment