Java Server reply is not printed as expected -


i building simple client-server program , have in main :

ftpclient ftp = new ftpclient("www.kernel.org"); ftp.getreply(); ftp.sendcommand("user " + "anonymous"); ftp.getreply(); ftp.sendcommand("pass " + "anonymous"); ftp.getreply(); string com=""; while (!com.equalsignorecase("quit")){     system.out.println("enter commands . or enter quit");     bufferedreader keyboard = new bufferedreader(new inputstreamreader(system.in));     com = keyboard.readline();     ftp.sendcommand((com));     ftp.getreply();     system.out.println("==============="); } ftp.close(); 

the problem in getreply() function, function :

public void getreply() throws ioexception {     string line="";     while (br.ready())     {         line = br.readline();         system.out.println(line);         system.out.flush();     } } 

br bufferedreader.
now problem when program starts doesn't show welcome message server until press enter or command, when debug program step step every thing working perfectly.so problem in readline , should use else or what?

the problem end of server response not contain newline character. bufferedreader's readline method block until line of data received, "a line" consists of characters followed newline character (or end of stream). consequently, readline call not return if no newline received.

in situation then, bufferedreader isn't doing good. you'd better off using underlying reader yourself, reading array , emitting output comes in, such following:

final char[] buffer = new char[256]; // or whatever size want int nread; while ((nread = reader.read(buffer)) != -1) {     system.out.println(new string(buffer, 0, nread));     system.out.flush(); } 

the condition in while loop there might confusing if you're not used before, combines read operation (which reads buffer) check end of stream has not been reached. likewise, construction of string within while loop takes account fact buffer may not have been filled entirely, many characters supplied used.

note particular snippet keeps looping until stream empty; may wish add exit condition in particular case.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -