客户端套接字连接被拒绝

时间:2018-12-19 23:53:22

标签: java sockets

我试图了解套接字在Java中是如何工作的,因此我想看一看客户机-服务器实现。我发现了这个:Creating a Simple Java TCP/IP Server and Client Socket

如果我通过localhost127.0.0.1,服务器似乎可以正常工作,但是尽管我在客户端之前启动了服务器,但客户端将拒绝连接其中任何一个,并抛出连接被拒绝的异常。

服务器输出:

Running Server: Host=127.0.0.1 Port=5069

客户端输出:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at Client.<init>(Client.java:12)
    at Client.main(Client.java:29)

我的Java代码:

public class Server {
    private ServerSocket server;

    public Server(String ipAddress) throws Exception {
        if (ipAddress != null && !ipAddress.isEmpty()) {
//            System.out.println(InetAddress.getByName(ipAddress));
            this.server = new ServerSocket(0, 1, InetAddress.getByName(ipAddress));
        } else {
            this.server = new ServerSocket(0, 1, InetAddress.getLocalHost());
        }
    }

    private void listen() throws Exception {
        String data = null;
        Socket client = this.server.accept();
        String clientAddress = client.getInetAddress().getHostAddress();
        System.out.println("\r\nNew connection from " + clientAddress);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(client.getInputStream()));
        while ((data = in.readLine()) != null) {
            System.out.println("\r\nMessage from " + clientAddress + ": " + data);
        }
    }

    public InetAddress getSocketAddress() {
        return this.server.getInetAddress();
    }

    public int getPort() {
        return this.server.getLocalPort();
    }

    public static void main(String[] args) throws Exception {
        Server app = new Server("localhost");
        System.out.println("\r\nRunning Server: " +
                                   "Host=" + app.getSocketAddress().getHostAddress() +
                                   " Port=" + 50696);

        app.listen();
    }

}

public class Client {
    private Socket socket;
    private Scanner scanner;

    private Client(InetAddress serverAddress, int serverPort) throws Exception {
        this.socket = new Socket(serverAddress, serverPort);
        this.scanner = new Scanner(System.in);
    }

    private void start() throws IOException {
        String input;
        while (true) {
            input = scanner.nextLine();
            PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
            out.println(input);
            out.flush();
        }
    }

    public static void main(String[] args) throws Exception {
//        System.out.println( InetAddress.getByName("localhost"));
        Client client = new Client(
                InetAddress.getByName("127.0.0.1"), 50696);

        System.out.println("\r\nConnected to Server: " + client.socket.getInetAddress());
        client.start();
    }
}

1 个答案:

答案 0 :(得分:2)

如果您尝试连接到错误的端口,则连接将被拒绝(请参见this answer)。您的服务器未在端口5069上侦听。您显然将其设置为在此处的端口0上监听:

if (ipAddress != null && !ipAddress.isEmpty()) {
    this.server = new ServerSocket(0, 1, InetAddress.getByName(ipAddress));
} else {
    this.server = new ServerSocket(0, 1, InetAddress.getLocalHost());
}

为您使用的构造函数查看Javadoc

public ServerSocket(int port,
                    int backlog,
                    InetAddress bindAddr)
             throws IOException
     

参数:

     

port-端口号,或0以使用以下端口号:   自动分配。

     

backlog-请求的传入队列的最大长度   连接。

     

bindAddr-服务器将绑定到的本地InetAddress

第一个参数是端口号,您为两个构造函数传递了0