1

我正在尝试编写一个非常简单的 IRC 客户端,希望以后可以扩展它。

在这一点上,我有两个用 java 编写的类,它们应该可以一起工作,并且是从 Oracle 教程中复制的。我想要做的是让 EchoClient 连接到某个端口上的主机,以便运行 EchoServer 的主机可以打印出客户端类型。我正在尝试完全按照教程所说的去做,但是在复制和粘贴代码后出现错误。

EchoClient.java:

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

public class EchoClient {
    public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.err.println(
            "Usage: java EchoClient <host name> <port number>");
        System.exit(1);
    }

    String hostName = args[0];
    int portNumber = Integer.parseInt(args[1]);

    try (
        Socket echoSocket = new Socket(hostName, portNumber);
        PrintWriter out =
            new PrintWriter(echoSocket.getOutputStream(), true);
        BufferedReader in =
            new BufferedReader(
                new InputStreamReader(echoSocket.getInputStream()));
        BufferedReader stdIn =
            new BufferedReader(
                new InputStreamReader(System.in))
    ) {
        String userInput;
        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
        }
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host " + hostName);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to " +
            hostName);
        System.exit(1);
    }
}

}

EchoServer.java:

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

public class EchoServer {
  public static void main(String[] args) throws IOException {

    if (args.length != 1) {
        System.err.println("Usage: java EchoServer <port number>");
        System.exit(1);
    }

    int portNumber = Integer.parseInt(args[0]);

    try (
        ServerSocket serverSocket =
            new ServerSocket(Integer.parseInt(args[0]));
        Socket clientSocket = serverSocket.accept();
        PrintWriter out =
            new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
    ) {
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            out.println(inputLine);
        }
    } catch (IOException e) {
        System.out.println("Exception caught when trying to listen on port "
            + portNumber + " or listening for a connection");
        System.out.println(e.getMessage());
    }
}

}

当我尝试从终端运行已编译的 EchoServer 时java EchoServer 2000出现错误,Error: Could not find or load main class EchoClient并且从java EchoServer 2000

4

2 回答 2

0

如果您从命令提示符运行您的应用程序 - 尝试首先cmd.exe管理员身份打开。

输入 windows start menu: cmd,然后右键单击cmd.exe并选择“以管理员身份运行”。

然后在打开的命令提示符下执行

java knockclient 127.0.0.1 80 test

像往常一样。

于 2018-10-20T14:39:28.683 回答
0

Echo 服务器示例向您展示了如何执行此操作。首先使用客户端创建的套接字设置某种输出流:

Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
    new PrintWriter(echoSocket.getOutputStream(), true);

然后,当您有消息要发送时,将结果写入out对象println()

String userInput;
while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    //... 
}

您可以在教程页面的代码清单中找到这些行: https ://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

这不是写入套接字的唯一方法,但示例就是这样做的,您现在应该坚持使用它。

于 2018-10-20T16:06:35.123 回答