-1

Right now, I made a simple server in java as so:

import Java.net.*;
import Java.io.*;
import Java.util.*;

class Server{
public static void main(String[] args){
         int PORT = 13;
             try(ServerSocket server = new ServerSocket(PORT)){
                while(true){
                    try(Socket connection = server.accept()){
                         Writer out = new OutputStreamWriter(connection.getOutputStream());
                         Date now = new Date();
                         out.write(now.toString());
                         out.flush();
                         connection.close();
                     } catch(IOException ex){}
                 }
             }
             catch(IOException ex){
                System.err.println(ex);
             }
         }
    }

I compile and run this from the command line. Being on Port 13, I try to run this on telnet as so: telnet localhost 13 but all it gives me is "Connection to host lost". Mind you, I did this after enabling telnet on Windows 10 and installing it. Is there a simple step I'm missing?

2 Answers2

0

Telnet won't connect to localhost ...

Yes it did. That's why it said 'connection to host lost' instead of 'connection refused'.

And here's what happened. You coded:

connection.close();

You got:

connection to host lost

You closed the connection; Telnet told you so.

That's what's supposed to happen.

There is no problem here to solve.

Or else you got an I/O exception in the server accept loop.

But as you are ignoring them it is impossible to say which.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks, I just copied the example from my textbook.Maybe they used a previous version of Telnet...? – Brock Lesnar May 17 '18 at 05:01
  • No, they just didn't tell you what should happen correctly. Everything here is working as designed. Possibly you should have sent a line terminator with the date. Windows Telnet has a nasty habit of clearing the window when disconnection happens. – user207421 May 17 '18 at 05:46
-1

In your program, following events occurs:

  1. Server starts at 13
  2. telnet localhost 13 : connects to that server
  3. As soon as connection is established, server sends date
  4. You will see date on your client
  5. out.flush() says the client I am done writing
  6. telnet client don't understand server command (Date)
  7. telnet closes the connection

Telnet - Connection to host lost - on port 1099 in local machine

  • Telnet doesn't have to understand anything except IAC commands. Anything else it just displays. And 'connection to host lost' does not mean that Telnet has closed the connection: it means the *peer* has closed the connection. And `out.flush()` doesn't 'say' anything to the client. It just flushes buffered data at the server. A Telnet or even TCP peer *never* knows when its remote peer is done writing. – user207421 May 17 '18 at 00:29
  • And the link in your answer is irrelevant. That is a case where the *server* didn't understand the Telnet *client*, and where the server closed the connection (as in fact it did here). You are alleging, incorrectly, that the Telnet client didn't understand the server, and that the client closed the connection. – user207421 May 17 '18 at 02:51