Java networking problem

Discussion in 'Java' started by, Mar 18, 2008.

  1. Guest

    Greetings. I'm having a major problem with Java NIO networking. I'm
    trying to get two clients to talk to each other over a network
    connection. I'm able to get the clients to write to the network, and
    the data clearly gets to the machine on the other side of the
    network. But for some reason, the other client never gets its channel
    marked as readable, and therefore the other client never reads in the
    data. Eventually, the input buffer on the receiving client fills, and
    the sending client stops writing. I've shown what I'm doing below.
    Does anybody know what I'm doing wrong, and how I can fix it?


    I'm initially registering the channel as:
    AcceptedChannel.register(ChannelSelector, SelectionKey.OP_READ |
    (I've also tried setting it up intially as:
    AcceptedChannel.register(ChannelSelector, SelectionKey.OP_READ);
    but that doesn't seem to work (as it shouldn't, since I need to be
    able to both read and write.)

    The next thing it hits (assuming that there is a key) is usually if
    (It hits the test for readability first, but I never get a readable

    It gets the appropriate channel based on that key:
    NetworkDataHandler Handler = DataHandlerContainer.Get((SocketChannel);

    and then the object associated with that specific channel tries to set
    up a message and write to it:

    That creates a test message (currently a string about wishing that my
    program would work!), encodes it in a byte array, and adds it to my
    outgoing message queue.

    A message sending method is then called, which checks for messages on
    the queue. If any exist, it gets the next one and places it in the
    SendBuffer. It then does a


    to write out the data. I've tried various combinations of registering
    teh channel for OP_READ, OP_READ | OP_WRITE, or OP_WRITE after this,
    and none of them worked (although registering for read only certainly
    prevented more data from being written. :) )

    After the data is written out to the channel, it definitely appears on
    the remote host. Using Ethereal, I've been able to confirm that both
    clients send the test message to each other. Using Telnet, I've also
    been able to confirm that the message is sent to remote clients.
    Ethereal also confirmed that both clients are writing to and from the
    same ports on each machine, so they are clearly using the same channel
    in each direction. However, despite the data making it at least as
    far as the remote host, the OP_READ is never triggered, and therefore
    the data is never read. Unless, of course, I send the data through
    telnet, in which case it is. Actually, I also tried writing a non-
    blocking client that does an OP_CONNECT instead of a read or write,
    that one seems to be able to get data through to the client (although
    it doesn't try to read any.) That client uses the same reading and
    writing methods as my regular client. Only the setup is different.

    Any ideas? Thanks!
    , Mar 18, 2008
    1. Advertisements

  2. EJP

    EJP Guest



    Please don't do that. You're getting plenty of responses in your
    original thread.
    EJP, Mar 18, 2008
    1. Advertisements

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. Rahul Sharma

    Is there Java.Networking group

    Rahul Sharma, Jul 23, 2003, in forum: Java
    Jul 23, 2003
  2. Robert Olofsson

    Re: Java I/O for Networking

    Robert Olofsson, Aug 22, 2003, in forum: Java
    Robert Olofsson
    Aug 24, 2003
  3. VisionSet

    java to java networking

    VisionSet, Nov 19, 2003, in forum: Java
    Nov 19, 2003
  4. Steve R. Burrus

    Need help w. Java Networking.

    Steve R. Burrus, Jun 5, 2004, in forum: Java
    Jun 7, 2004
  5. John Galt
    Jul 15, 2004