Re: echoclient reading from server
I'm not sure why you chose to set up the connection with a
SocketChannel instead of a "plain" Socket. At the client end the
"traditional way" is probably easier than the stuff in java.nio.
i was learning nio using Greg Travis's ibmalphaworks tutorial on
nio.I tried out the MultiportEcho server program and thought i'd
write the client..
i modified the client code as below
private void initClient()throws IOException{
InetSocketAddress isa=new
InetSocketAddress(InetAddress.getLocalHost(),port);
SocketChannel sc=SocketChannel.open(isa) ;
if(sc!=null)debug("connected to server");
BufferedReader userin=new BufferedReader(new
InputStreamReader(System.in));
String userinput;
while((userinput=userin.readLine())!=null){
buf.clear();
buf.put(userinput.getBytes());
buf.flip();
sc.write(buf);
if(userinput.equals("bye"))break;
}
//trying to read from server
while(true){
buf.clear();
int r=sc.read(buf);
if(r <=0)break;
buf.flip();
String text=new String(buf.array());
debug("read from server message of length="+text.length()
+"message:"+text);
}
}
problem here is that the echo would come only after client says bye to
the server..I would like the server to return the echo as soon as i
type in.Is that possible?
Also ,since i am allocating the ByteBuffer as of 1024 bytes..the
returned string ('text' in the above code) is 1024 bytes length and
this is displayed by debug() as a string with a lot of blanks at the
end
i would like to know if these can be rectified
thanks
jim