non blocking echo client problem

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

  1. jimgardener

    jimgardener Guest

    i tried to implement a non blocking echoclient using java.nio
    it connects to an echoserver and takes userinput and writes to
    server .Then it reads from server and prints the server message

    however ,i found that the socketchannel.isConnected() is false in the
    while loop where key.readyOps is equal to OP_CONNECT

    i am not sure what i am doing wrong..Am a beginner with nio and
    networking..can someone help?


    here is the code>>>

    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.net.*;
    import java.util.*;
    public class EchoClient {
    int port;
    ByteBuffer buf=ByteBuffer.allocate(1024);
    Selector sel=null;
    SocketChannel sc=null;
    public EchoClient(int p)throws IOException{
    port=p;
    initClient();
    }
    private void initClient()throws IOException{
    sel=Selector.open();
    sc=SocketChannel.open();
    sc.configureBlocking(false);
    sc.connect(new
    InetSocketAddress(InetAddress.getLocalHost(),this.port));
    sc.register(sel, SelectionKey.OP_CONNECT);

    while(true){
    sel.select();
    Set<SelectionKey> selectedkeys=sel.selectedKeys();
    Iterator<SelectionKey> it=selectedkeys.iterator();
    while(it.hasNext()){
    SelectionKey key=it.next();
    int readyops=key.readyOps();
    if((readyops& SelectionKey.OP_CONNECT)==SelectionKey.OP_CONNECT){

    debug("connected to server:"+sc.isConnected());
    sc.register(sel, SelectionKey.OP_READ);
    debug("registered for read");
    debug("now write to server");
    getUserinputAndWrite();
    it.remove();
    }
    else if((readyops& SelectionKey.OP_READ)==SelectionKey.OP_READ){
    debug("read from server");
    debug("connected to server:"+sc.isConnected());
    buf.clear();
    int numread=0;
    while((numread=sc.read(buf) )!= -1){
    debug("server said:"+new String(buf.array(),0,numread));
    }
    it.remove();
    }

    }



    }

    }

    private void getUserinputAndWrite()throws IOException{

    BufferedReader userin=new BufferedReader(new
    InputStreamReader(System.in));
    String userinput=null;
    debug("enter user input:");
    while((userinput=userin.readLine()) !=null){
    debug("u entered:"+userinput);
    debug("still connected to server:"+sc.isConnected());
    byte[] userbits=userinput.getBytes();
    buf.clear();
    buf.put(userbits);
    buf.flip();
    sc.write(buf);

    if(userinput.equals("bye"))break;
    }
    }

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

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

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

    }

    this is the output i got>>>java EchoClient 9000
    connected to server:false
    registered for read
    now write to server
    enter user input:
    hello there
    u entered:hello there
    still connected to server:false
    Exception in thread "main" java.nio.channels.NotYetConnectedException
    at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(Unknown
    Source)
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
    at EchoClient.getUserinputAndWrite(EchoClient.java:69)
    at EchoClient.initClient(EchoClient.java:35)
    at EchoClient.<init>(EchoClient.java:13)
    at EchoClient.main(EchoClient.java:78)
     
    jimgardener, Jul 2, 2008
    #1
    1. Advertising

  2. jimgardener

    GArlington Guest

    On Jul 2, 10:03 am, jimgardener <> wrote:
    > i tried to implement a non blocking echoclient using java.nio
    > it connects to an echoserver and takes userinput and writes to
    > server .Then it reads from server and prints the server message
    >
    > however ,i found that the socketchannel.isConnected() is false in the
    > while loop where key.readyOps is equal to OP_CONNECT
    >
    > i am not sure what i am doing wrong..Am a beginner with nio and
    > networking..can someone help?
    >
    > here is the code>>>
    >
    > import java.io.*;
    > import java.nio.*;
    > import java.nio.channels.*;
    > import java.net.*;
    > import java.util.*;
    > public class EchoClient {
    > int port;
    > ByteBuffer buf=ByteBuffer.allocate(1024);
    > Selector sel=null;
    > SocketChannel sc=null;
    > public EchoClient(int p)throws IOException{
    > port=p;
    > initClient();
    > }
    > private void initClient()throws IOException{
    > sel=Selector.open();
    > sc=SocketChannel.open();
    > sc.configureBlocking(false);
    > sc.connect(new
    > InetSocketAddress(InetAddress.getLocalHost(),this.port));
    > sc.register(sel, SelectionKey.OP_CONNECT);

    <snap>
    You should read docs for NIO SocketChannel.connect() method... You
    might find that it returns boolean result and what it means and what
    you are meant to do if that result is false...
     
    GArlington, Jul 2, 2008
    #2
    1. Advertising

  3. jimgardener

    EJP Guest

    OP_CONNECT means that you must now call finishConnect(). If this returns
    true, so will sc.isConnected(). Not before.
     
    EJP, Jul 3, 2008
    #3
    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. Hendra Gunawan
    Replies:
    1
    Views:
    12,606
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    942
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    840
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    739
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    273
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page