echoclient reading from server

J

jimgardener

i have written an echoclient that connects to a server,takes input
from user and writes to server.I want the client to read from the
server and show it

this is what i wrote

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
public class EchoClient {
int port;
ByteBuffer buf=ByteBuffer.allocate(1024);
public EchoClient(int p)throws IOException{
port=p;
initClient();
}
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;
}

//how to read from server?


}

public static void main(String[] args) {
if (args.length !=1)debug("EchoClient port");
try{
new EchoClient(Integer.parseInt(args[0]));

}catch(IOException e){
e.printStackTrace();
}

}
public static void debug(String msg){
System.out.println(msg);
}
}


i am not very sure how to do the reading from the server part..can
someone help?

jim
 
G

Gordon Beaton

i have written an echoclient that connects to a server,takes input
from user and writes to server.I want the client to read from the
server and show it
[...]

i am not very sure how to do the reading from the server part..can
someone help?

You already seem to know how to write to the server. And if you've
written to the server, then presumably it can read what you wrote. So
why is reading a challenge at the client?

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.

The SocketChannel has methods for reading. Or you can get the Socket
from the SocketChannel, and an InputStream from that.

/gordon

--
 
J

jimgardener

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
 
G

Gordon Beaton

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?

It's possible to wait for the server to reply anywhere you like. An
idea is to do that after sending each line instead of after sending
the entire user input.
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

read() tells you how much it read, and there is a String constructor
that lets you specify how much of the buffer to use.

/gordon

--
 
A

Arved Sandstrom

Gordon Beaton said:
i have written an echoclient that connects to a server,takes input
from user and writes to server.I want the client to read from the
server and show it
[...]

i am not very sure how to do the reading from the server part..can
someone help?

You already seem to know how to write to the server. And if you've
written to the server, then presumably it can read what you wrote. So
why is reading a challenge at the client?

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.

The SocketChannel has methods for reading. Or you can get the Socket
from the SocketChannel, and an InputStream from that.

/gordon

A lot of the tutorials out now will use java.nio for both ends. For example,
http://rox-xmlrpc.sourceforge.net/niotut/

You're quite right that NIO was oriented towards servers: non-blocking I/O,
buffers and selectors. It's not that tough, however, to think of clients
that would wish to avail themselves of non-blocking I/O. If reading from the
connection is lietrally the only thing that the client can do at a certain
point, sure, NIO doesn't change the equation. But a web browser is one
example of a client that can benefit from the newer approach.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top