java - StringBuffer is not been printed correctly -


i trying read data inputstream, , put them stringbuffer print it.

i put code in main method.

my problem stringbuffer printed when debugging code, when run isnt been printed.

my code:

socket s = new socket(); string host = ""; printwriter s_out = null; bufferedreader s_in = null; inputstream in = null;  s.connect(new inetsocketaddress(host, 23));  //writer socket s_out = new printwriter(s.getoutputstream(), true); //reader socket s_in = new bufferedreader(new inputstreamreader(s.getinputstream())); in = s.getinputstream();  if(s.isconnected() == true && s.isinputshutdown() == false && s.isoutputshutdown() == false){     stringbuffer sb = new stringbuffer();     boolean found = false;     char ch;     int numberofbytesthatcanberead = in.available();      for(int j = 0; j < numberofbytesthatcanberead; j++){         ch = (char) s_in.read();         //system.out.print(ch);         sb.append(ch);     }      system.out.print(sb.tostring());      s_in.close();     in.close();     s_out.close();     s.close(); 

the correct value of variable 564, returned when debug code, when run returns 21.

int numberofbytesthatcanberead = in.available(); 

so, question is: why when debug code, result of stringbuffer correct, , when run not ?

thanks !

you have no loop listening on port, means run through , end before other side has finished sending data.

when debug means there enough time arrive before try , read it.

do reading in loop waiting until data need has arrived.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -