gpt4 book ai didi

java - 为什么我得到 “Address already in use (Bind failed)” ?

转载 作者:行者123 更新时间:2023-11-30 02:29:08 26 4
gpt4 key购买 nike

这应该很简单,但我在这里遗漏了一些东西。我有两个玩具类:(a) 一个需要连接并提供文件服务的服务器; (b) 请求文件并将其打印在标准输出上的客户端。

服务器代码:

package lp2.tarefa4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import static java.lang.String.format;
import java.net.ServerSocket;
import java.net.Socket;

public class ServidorWeb implements Runnable {

private final String dirName;

public ServidorWeb(String dirName) {
this.dirName = dirName;
}

public static void main(String[] args) {
ServidorWeb srv = new ServidorWeb(args[0]);
srv.run();
}

@Override
public void run() {
System.out.println("Server started. Waiting for connections...");

while (true) {
try {
ServerSocket server = new ServerSocket(48080);
Socket socket = server.accept();
proccessRequest(socket);
}
catch (IOException ex) {
System.err.println("!!! ERRO: " + ex.getMessage());
}
}
}

private void proccessRequest(Socket socket) throws IOException {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
String fileName = in.readUTF();
File inputFile = new File(dirName, fileName);

String msg = format("Request: %s:%d [%s]",
socket.getInetAddress(),
socket.getPort(),
inputFile.getPath());
System.out.println(msg);

DataInputStream fin = new DataInputStream(new FileInputStream(inputFile));
String s = fin.readUTF();
while (s != null) {
out.writeUTF(s);
s = fin.readUTF();
}
in.close();
out.close();
fin.close();
}

}

客户端代码:

package lp2.tarefa4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ClienteWeb implements Runnable {

private final String host;
private final int port;
private final String fileName;

public ClienteWeb(String host, int port, String fileName) {
this.host = host;
this.port = port;
this.fileName = fileName;
}

public static void main(String[] args) {
ClienteWeb srv = new ClienteWeb(args[0], Integer.parseInt(args[1]), args[2]);
srv.run();
}

@Override
public void run() {
try {
Socket socket = new Socket(host, port);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(fileName);
String s = in.readUTF();
while (s != null) {
System.out.println(s);
s = in.readUTF();
}
in.close();
out.close();
socket.close();
}
catch (IOException ex) {
System.err.println("!!! ERRO: " + ex.getMessage());
}
}

}

我尝试在同一台计算机上运行服务器和客户端,但每次我尝试运行客户端时总是从服务器收到此消息:

!!! ERRO: Address already in use (Bind failed)

我是否需要执行与上述不同的操作才能运行此代码而不会出现错误?

谢谢。

最佳答案

该错误通常意味着您尝试打开的端口已被另一个应用程序使用,请尝试使用 netstat 查看哪些端口已打开,然后使用可用端口。

还要检查您是否绑定(bind)到正确的 IP 地址(我假设它是 localhost)

netstat -tulpn 将使人们能够找到正在使用特定端口的进程 ID。

关于java - 为什么我得到 “Address already in use (Bind failed)” ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44727183/

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