SocketChannel.write(ByteBuffer src) problem

T

technodolt

I have a SocketChannel open (to a telnet port).

I am using a KeyListener on a JTextArea to call
SocketChannel.write(ByteBuffer.wrap(JTextArea.getText().getByes());
any time that someone presses enter while in the text area.

If I use the keyReleased event to do this, it works just fine.
However, if I use keyPressed, it won't write the text to the socket.

I surrounded the code that's doing the write with two message dialogs,
so I know the key event is being fired, and I know it's executing code
both before and after the line that calls write(). Also, write() is
returning a value of 6, which means that it's accepted 6 bytes to
write to the SocketChannel. However, the telnet server I'm connected
to never receives those 6 bytes.

Basically, I moved some code from keyReleased to keyPressed, and the
only piece of it that stopped working was writing the contents of the
TextArea to a SocketChannel. I simply don't understand how the type
of key event could have any bearing on whether or not the text is
being written to the channel.

One last note:

I tried swapping the code back and forth several times. The EXACT
same code works perfectly for keyReleased, and doesn't work for
keyPressed.

Anyone got any ideas?
 
K

Knute Johnson

technodolt said:
I have a SocketChannel open (to a telnet port).

I am using a KeyListener on a JTextArea to call
SocketChannel.write(ByteBuffer.wrap(JTextArea.getText().getByes());
any time that someone presses enter while in the text area.

If I use the keyReleased event to do this, it works just fine.
However, if I use keyPressed, it won't write the text to the socket.

I surrounded the code that's doing the write with two message dialogs,
so I know the key event is being fired, and I know it's executing code
both before and after the line that calls write(). Also, write() is
returning a value of 6, which means that it's accepted 6 bytes to
write to the SocketChannel. However, the telnet server I'm connected
to never receives those 6 bytes.

Basically, I moved some code from keyReleased to keyPressed, and the
only piece of it that stopped working was writing the contents of the
TextArea to a SocketChannel. I simply don't understand how the type
of key event could have any bearing on whether or not the text is
being written to the channel.

One last note:

I tried swapping the code back and forth several times. The EXACT
same code works perfectly for keyReleased, and doesn't work for
keyPressed.

Anyone got any ideas?

The problem is in your code?
 
T

technodolt

The problem is in your code?

Agreed. Anyone have any insight into where in my code the problem
might be?

Maybe I can re-ask the question this way: what would cause a call to
SocketChannel.write to return an integer (indicating it has written
what I sent it), but not actually write the bytes to the channel?

Thanks,
Luke
 
E

EJP

what would cause a call to
SocketChannel.write to return an integer (indicating it has written
what I sent it), but not actually write the bytes to the channel?
Nothing, but maybe the 6 bytes you wrote aren't the 6 bytes you thought
you wrote ...
 
D

Daniele Futtorovic

I have a SocketChannel open (to a telnet port).

I am using a KeyListener on a JTextArea to call
SocketChannel.write(ByteBuffer.wrap(JTextArea.getText().getByes());
any time that someone presses enter while in the text area.

If I use the keyReleased event to do this, it works just fine.
However, if I use keyPressed, it won't write the text to the socket.

I surrounded the code that's doing the write with two message
dialogs, so I know the key event is being fired, and I know it's
executing code both before and after the line that calls write().
Also, write() is returning a value of 6, which means that it's
accepted 6 bytes to write to the SocketChannel. However, the telnet
server I'm connected to never receives those 6 bytes.

Basically, I moved some code from keyReleased to keyPressed, and the
only piece of it that stopped working was writing the contents of
the TextArea to a SocketChannel. I simply don't understand how the
type of key event could have any bearing on whether or not the text
is being written to the channel.

One last note:

I tried swapping the code back and forth several times. The EXACT
same code works perfectly for keyReleased, and doesn't work for
keyPressed.

Anyone got any ideas?

I suspect you're using KeyEvent#getKeyChar. In that case: RTFM.


public char getKeyChar()

Returns the character associated with the key in this event. For
example, the KEY_TYPED event for shift + "a" returns the value for "A".

*KEY_PRESSED and KEY_RELEASED events are not intended for reporting*
*of character input. Therefore, the values returned by this method are*
*guaranteed to be meaningful only for KEY_TYPED events*.

Returns:
the Unicode character defined for this key event. If no valid
Unicode character exists for this key event, CHAR_UNDEFINED is returned.

Further reading: the whole java.awt.event.KeyEvent class (header)
documentation.
 
T

technodolt

I suspect you're using KeyEvent#getKeyChar. In that case: RTFM.

public char getKeyChar()

     Returns the character associated with the key in this event. For
example, the KEY_TYPED event for shift + "a" returns the value for "A".

     *KEY_PRESSED and KEY_RELEASED events are not intended for reporting*
*of character input. Therefore, the values returned by this method are*
*guaranteed to be meaningful only for KEY_TYPED events*.

     Returns:
         the Unicode character defined for this key event. If no valid
Unicode character exists for this key event, CHAR_UNDEFINED is returned.

Further reading: the whole java.awt.event.KeyEvent class (header)
documentation.

Nope. Using getKeyCode(). As I said, it works fine when using
keyReleased, it just doesn't work when using keyPressed. If I was
using getKeyChar(), it wouldn't work for either of them.

And it still recognizes the key press... other code inside the
listener still fires off when you hit enter. The only line that isn't
working is the SocketChannel.write call. It doesn't throw any
exceptions, it returns the correct value as though it were writing,
the value is just never actually sent through the socket.
 
T

technodolt

Nothing, but maybe the 6 bytes you wrote aren't the 6 bytes you thought
you wrote ...

Two reasons that can't be true:

a) I'm also pulling the exact same data into a byte array and then
using a message dialog to show me a string created from that byte
array.
b) With the telnet server I'm using, if it sent anything _at all_, I
would get a response back. I'm getting no response, and then getting
my connection dropped due to inactivity.

I definitely thought the same thing originally, but tested it into a
dead end.
 
D

Daniele Futtorovic

Nope. Using getKeyCode().

And what are you doing with that value?
As I said, it works fine when using keyReleased, it just doesn't work
when using keyPressed.

With the /exact/ same data?
If I was using getKeyChar(), it wouldn't work for either of them.

If your problem persists, wouldn't it be worth your while to consider
using getKeyChar() on KEY_TYPED events?

And it still recognizes the key press... other code inside the
listener still fires off when you hit enter. The only line that
isn't working is the SocketChannel.write call. It doesn't throw any
exceptions, it returns the correct value as though it were writing,
the value is just never actually sent through the socket.
a) I'm also pulling the exact same data into a byte array and then
using a message dialog to show me a string created from that byte
array. b) With the telnet server I'm using, if it sent anything _at
all_, I would get a response back. I'm getting no response, and then
getting my connection dropped due to inactivity.

All this put together sounds very odd, and as such rather unlikely -- if
you'll forgive the scepticism.
 
T

technodolt

And what are you doing with that value?

if(e.getKeyCode() == KeyEvent.VK_ENTER)
With the /exact/ same data?
I'm using a class that extends KeyAdapter. I can literally change
ONLY the name of the method I'm overriding, leaving all other code
completely, 100% identical, and use the exact same data to send, and
it works with keyReleased, and not with keyPressed.
If your problem persists, wouldn't it be worth your while to consider
using getKeyChar() on KEY_TYPED events?
I can't use KeyTyped/getKeyChar because I'm processing on enter...
doesn't have an associated character, and therefore can only be caugh
with Released/Pressed.
All this put together sounds very odd, and as such rather unlikely -- if
you'll forgive the scepticism.
Not a problem. I spent an hour plus trying to debug this earlier and
going "This just doesn't make ANY sense", so I had a healthy dose of
skepticism myself. I finally thought it would be better to get
outside input than keep poking at it, in case I'm missing something
obvious.
 
D

Daniele Futtorovic

if(e.getKeyCode() == KeyEvent.VK_ENTER)

And then? You spoke of a text area earlier. Are you doing something like
textArea.getText() and then sending the result?

Could it be your telnet server expects a line/command terminator, which
it does not get because you're sending on KEY_PRESSED, before the enter
gets appended to the text area's content, and thus the server waits and
waits and eventually times out?
 
T

technodolt

And then? You spoke of a text area earlier. Are you doing something like
textArea.getText() and then sending the result?

Could it be your telnet server expects a line/command terminator, which
it does not get because you're sending on KEY_PRESSED, before the enter
gets appended to the text area's content, and thus the server waits and
waits and eventually times out?

See, now this is exactly why I posted in a forum instead of poking at
it myself.

That was indeed the problem, thank you very much :)
 
D

Daniele Futtorovic

See, now this is exactly why I posted in a forum instead of poking at
it myself.

That was indeed the problem, thank you very much :)

Thanks a lot for submitting such an easy one. It was just within my
means. ( <- teasing ;) )
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top