java - How to stop BufferedReader readline()? -
im new java , im trying make client-server form. client , server can chat each other. client have 2 thread 2 mission: send , receive message. want when sendthread read keyboard string "bye", client stop 2 thread. problem thread receive still perform statement readline() of bufferedreader can't reach exit here code of mine:
try { br = new bufferedreader(new inputstreamreader(socket.getinputstream())); while (!stop) { string temp = br.readline(); if (temp.length() == 0) { thread.sleep(100); } else system.out.println(" receiving server: " + temp); } } catch (ioexception | interruptedexception e) { e.printstacktrace(); }
update: sorry because dont explain more clearly. client have 2 thread run independently. receivethread code in can alway wait message server. , sendthread alway read data keyboard. when type "bye", sendthread read string , stop client. problem receivethread performing readline()
can't stop itself.
according javadoc null
reference returned when there end of stream, this:
br = new bufferedreader(new inputstreamreader(socket.getinputstream())); string temp; while ((temp=br.readline())!=null) { //check null reference if (temp.length() == 0) thread.sleep(100); else system.out.println(" receiving server: " + temp); }
it not helpfull see how interrupt bufferedreader's readline
Comments
Post a Comment