java 用java编写一个telnet客户端——如何监听新数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25503547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:08:44  来源:igfitidea点击:

Writing a telnet client in java - how to listen for new data?

javasocketsclienttelnet

提问by matt

I'm attempting to implement a Telnet client in Java, which will just get data from a telnet server.

我正在尝试用 Java 实现 Telnet 客户端,它只会从 telnet 服务器获取数据。

I've been looking for ages now and have found a lot of things, but not particularly what I need - for example the apache commons client example, which seems to send a lot of commands, which is just confusing me to be honest. So I therefore thought it would be easier to just to write my own client which connects to the server using a socket.

我已经找了很多年了,已经找到了很多东西,但并不是我特别需要的东西——例如 apache commons 客户端示例,它似乎发送了很多命令,老实说这让我感到困惑。因此,我认为编写自己的客户端使用套接字连接到服务器会更容易。

 public class TelnetClient {
    private String host;

    public TelnetClient(String host) {
        this.host = host;
    }

    public void getData(){
        Socket s = new Socket();
        PrintWriter s_out = null;
        BufferedReader s_in = null;

        try {
            s.connect(new InetSocketAddress("www.google.com" , 80));
            System.out.println("Connected");

            //writer for socket
            s_out = new PrintWriter( s.getOutputStream(), true);
            //reader for socket
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        }

        //Host not found
        catch (UnknownHostException e) {
            System.err.println("Don't know about host : " + host);
            System.exit(1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Send message to server
        String message = "GET / HTTP/1.1\r\n\r\n";
        s_out.println( message );

        System.out.println("Message send");

        //Get response from server
        String response;
        try {
            while ((response = s_in.readLine()) != null) {
                System.out.println( response );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

But I cant actually test this with the server currently so Im just using google.com, however I want to change it to listen continuously for new lines of data on the server.

但我目前无法实际使用服务器对此进行测试,因此我只是使用 google.com,但是我想将其更改为持续侦听服务器上的新数据行。

Basically my question is, am I going about this the wrong way - am I being naive by just using sockets to access the telnet server and am I underestimating what a telnet client/server should be, thanks for any help.

基本上我的问题是,我是否以错误的方式解决这个问题 - 我是否通过使用套接字访问 telnet 服务器而天真,我是否低估了 telnet 客户端/服务器应该是什么,感谢您的帮助。

Also if anyone has any good/simple examples of a telnet client, that would be very useful!!

此外,如果有人有任何好的/简单的 telnet 客户端示例,那将非常有用!!

回答by Patrick Collins

I've been holding off on answering because there are a couple different things going on here -- Google doesn't have a telnet server running on port 80, it's a web (HTTP) server. You're connecting to the webserver with your telnet client and trying to talk over HTTP with plain text. HTTPand telnetare two different protocols.

我一直在推迟回答,因为这里发生了一些不同的事情——Google 没有在端口 80 上运行的 telnet 服务器,它是一个 Web (HTTP) 服务器。您正在使用 telnet 客户端连接到网络服务器,并尝试使用纯文本通过 HTTP 进行通信。HTTPtelnet是两种不同的协议。

So there is a mismatch between what you want to do and what these Java telnet clients are designed to do -- namely connect to a remote shell.

因此,您要执行的操作与这些 Java telnet 客户端的设计目标(即连接到远程 shell)之间存在不匹配。

Based on what you've been saying, I think you can get it done much more easily by just making an HTTP request. There's a dead simple solution in this answer:

根据您所说的,我认为您可以通过发出 HTTP 请求更轻松地完成它。这个答案中有一个非常简单的解决方案

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

Sockets are complicated stuff. Take a look at this tutorial if you want to dig deeper in to them. But they're going to be a huge pain to work with and probably not help you get what you need done.

套接字是复杂的东西。如果您想更深入地了解它们,请查看本教程。但是使用它们将是一个巨大的痛苦,并且可能无法帮助您完成所需的工作。