Telnet and EOF with SocketChannel.read()

Discussion in 'Java' started by Fritz Bayer, Dec 8, 2004.

  1. Fritz Bayer

    Fritz Bayer Guest

    Hello,

    I'm playing around with the NIO api trying to figure out how certain
    functions respond and work.

    I know that SocketChannel.read() can return -1, which actually signals
    a EOF. I have seen this and I'm trying to reproduce it

    To do so I use telnet to connect to my server. Then I type "^]" so
    enter the special mode of telnet and use "send eof" to sent an EOF to
    the program.

    I use a debugger to check out whats comming in. My
    SocketChannel.read() returns two bytes -1 and -20 respectively. Those
    correspond to the ISO 8859 latin1 of 255 and 236.

    I was expecting that SocketChannel.read() would not fill the buffer
    and return a -1 instead.

    Now I was wondering if anybody has some suggestions on what I'm doing
    wrong here?

    I just thought that maybe the telnet program does not really send eof
    but something else instead?

    Typing in ^D does not work. This usually corresponds to EOF when using
    bash under linux.

    Fritz
     
    Fritz Bayer, Dec 8, 2004
    #1
    1. Advertisements

  2. "send eof" sends the "telnet end of file" character, which is a telnet
    control character.

    If you want the server to reach end of file, don't "send" anything.
    You need to close the client connection. SocketChannel.read() will
    indicate EOF at that point.
    ^D is not an EOF character either. It is a special character
    interpreted by the console driver, telling it to close the input
    stream to whatever process is reading from the current tty (and
    thereby causing that process to detect EOF on its stdin).

    /gordon
     
    Gordon Beaton, Dec 8, 2004
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.