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

标签 java sockets network-programming localhost

这应该很简单,但我在这里遗漏了一些东西。我有两个玩具类:(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/

相关文章:

java - 使用开源 api 将 java 对象转换为 xml 的最佳方法是什么

java - 在 Grizzly 中配置 session cookie 名称

java - 如何防止任务栏中的JFrame警报效果

java - 如何使用java程序访问Linux机器中的文件和目录

PHP流套接字不解密数据

c# - 通过 TCP 套接字发送大量数据

c - 将 select 与阻塞和非阻塞套接字一起使用的影响

networking - Go 中的点对点网络

sockets - 为什么不在 Unix TCP/IP 服务器上使用 SO_REUSEADDR?

java - 使用 JAWS 读取禁用的 JButton