echoclient reading from server

Discussion in 'Java' started by jimgardener, Jul 1, 2008.

  1. jimgardener

    jimgardener Guest

    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
    jimgardener, Jul 1, 2008
    #1
    1. Advertising

  2. On Tue, 1 Jul 2008 03:25:41 -0700 (PDT), jimgardener wrote:
    > 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

    --
    Gordon Beaton, Jul 1, 2008
    #2
    1. Advertising

  3. jimgardener

    jimgardener Guest


    > 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
    jimgardener, Jul 1, 2008
    #3
  4. On Tue, 1 Jul 2008 06:41:31 -0700 (PDT), jimgardener wrote:
    > 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

    --
    Gordon Beaton, Jul 1, 2008
    #4
  5. "Gordon Beaton" <> wrote in message
    news:486a0a5b$0$2677$...
    > On Tue, 1 Jul 2008 03:25:41 -0700 (PDT), jimgardener wrote:
    >> 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
    Arved Sandstrom, Jul 1, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    657
    Kevin Spencer
    Nov 11, 2004
  2. Wael Soliman

    ASP.NET Reading problem (reading .xls)

    Wael Soliman, Jan 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,779
    =?Utf-8?B?dmluYXk=?=
    Jan 3, 2005
  3. -
    Replies:
    2
    Views:
    386
    Alan Krueger
    Jul 29, 2005
  4. Replies:
    0
    Views:
    771
  5. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    356
    Hrvoje Niksic
    May 25, 2007
Loading...

Share This Page