Java networking problem

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

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?

Thanks!

I'm initially registering the channel as:
AcceptedChannel.register(ChannelSelector, SelectionKey.OP_READ |
SelectionKey.OP_WRITE);
(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
(NextKey.isWritable())
(It hits the test for readability first, but I never get a readable
channel)

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

and then the object associated with that specific channel tries to set
up a message and write to it:
MessageHandlerForThisChannel.SendTestMessage();

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

Channel.write(SendBuffer);

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!
 
E

EJP

[repost]

Please don't do that. You're getting plenty of responses in your
original thread.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top