gpt4 book ai didi

java - 如何让这段代码(来自 Java 套接字教程)运行?

转载 作者:行者123 更新时间:2023-11-30 09:35:53 25 4
gpt4 key购买 nike

开始学习本教程的这一部分。我对什么是端口等只有一个基本的了解。

我尝试运行这段代码:

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

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

Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}

out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}

“不知道宿主:taranis。Java 结果:1"

是我得到的错误捕获。以我有限的理解;回声服务器是我机器上存在的东西吗?如果是这样,我需要做什么才能让它运行?还是我离题了?另外,他们为什么选择“taranis”作为参数?

我还将“taranis”替换为“localhost”以查看发生了什么。这次它最终捕获了一个 IOException。

编辑:所以我发现 echo 服务器在 win7 中默认被禁用并已激活它。但是我什至无法通过 telnet 连接到它。我想我可能只是在我的头上。我也尝试过您推荐的套接字,但没有成功。

最佳答案

来自同一教程:

... The Socket constructor used here requires the name of the machine and the port number to which you want to connect. The example program uses the host name taranis. This is the name of a hypothetical machine on our local network. When you type in and run this program on your machine, change the host name to the name of a machine on your network. Make sure that the name you use is the fully qualified IP name of the machine to which you want to connect. The second argument is the port number. Port number 7 is the port on which the Echo server listens.`

无论如何,您可能希望将 taranis 更改为 "localhost" 并确保 echo 服务正在您的机器上运行。如果不是,您可以使用类似于以下代码的内容来模拟回显服务器。

import java.net.Socket;
import java.util.Formatter;
import java.util.Scanner;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EchoServer {

public static void main(String[] args) {
try {
new EchoServer(INSERTPORT).execute();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

private ServerSocket serverSocket;
private int port;

private ArrayList<Client> clientList;
private ExecutorService clientRunner;

public EchoServer(int port) throws IOException {
this.port = port;
serverSocket = new ServerSocket(port);
clientRunner = Executors.newCachedThreadPool();
clientList = new ArrayList<>();
}

public void sendMessageToAll(String message) {
for (Client c : clientList) {
c.displayMessage(message);
}
}

public void execute() throws IOException {
while (true) {
clientList.add(new Client(serverSocket.accept(), this));
clientRunner.execute(clientList.get(clientList.size()-1));
}
}
private class Client implements Runnable {

private Socket clientSocket;
private Scanner input;
private Formatter output;

public Client(Socket s) throws IOException {
clientSocket = s;

input = new Scanner(clientSocket.getInputStream());
output = new Formatter(clientSocket.getOutputStream());
}

public void displayMessage(String s) {
output.format(s + "\n");
output.flush();
}
@Override
public void run() {
while(clientSocket.isConnected()) {
if(input.hasNextLine()) {
sendMessageToAll(input.nextLine());
}
}
}
}
}

编辑:为了完整起见,正如您提到的一些运行代码的问题,您运行服务器(这段代码)并让它在后台运行,然后运行客户端(您发布的代码)。我测试了它,工作正常。

关于java - 如何让这段代码(来自 Java 套接字教程)运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11076303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com