为什么我收到“地址已被使用(绑定失败)”?

时间:2017-06-23 17:42:46

标签: 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:地址已在使用中(Bind   失败)

我是否必须执行与上述不同的操作才能正常运行此代码?

感谢。

1 个答案:

答案 0 :(得分:2)

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

还要检查你是否绑定了正确的ip地址(我假设它是localhost)

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