How to read a String from user in java (Socket Programming) -
i writing client/server program. when tried read string client using scanner.nextline() not wait write text on console , process next statement. writing program in binary i/o because more efficient text i/o.
datainputstream fromserver = null; dataoutputstream toserver = null; scanner scanner = new scanner(system.in); try{ socket = new socket("localhost",8000); toserver = new dataoutputstream (socket.getoutputstream()); fromserver = new datainputstream ( socket.getinputstream()); } catch(ioexception e){ system.out.println(" error in connection: " + e); e.printstacktrace(); } system.out.println("connection established..."); try{ //this not processing settext = scanner.nextline(); toserver.writeutf(settext); toserver.flush(); // area server double area = fromserver.readdouble(); // display text area system.out.println("text client " + settext + "\n"); system.out.println("response server " + area + '\n'); } catch(ioexception e){ system.out.println(" error in connection: " + e); e.printstacktrace(); }
the output is:
text client response server 45.22
as can see program not take input console
if used next() instead of nextline() runs fine, want read line of text enter key.
Comments
Post a Comment