我试图实现一个TCP连接,一切工作良好,从服务器端,但当我运行客户端程序(从客户端计算机),我得到以下错误:

java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at TCPClient.main(TCPClient.java:13)

我尝试改变套接字编号,以防它正在使用,但无济于事,有人知道是什么导致了这个错误&如何修复它吗?

服务器代码:

//TCPServer.java

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

class TCPServer {
    public static void main(String argv[]) throws Exception {
        String fromclient;
        String toclient;

        ServerSocket Server = new ServerSocket(5000);

        System.out.println("TCPServer Waiting for client on port 5000");

        while (true) {
            Socket connected = Server.accept();
            System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
                    + ":" + connected.getPort() + " IS CONNECTED ");

            BufferedReader inFromUser = new BufferedReader(
                    new InputStreamReader(System.in));

            BufferedReader inFromClient = new BufferedReader(
                    new InputStreamReader(connected.getInputStream()));

            PrintWriter outToClient = new PrintWriter(
                    connected.getOutputStream(), true);

            while (true) {

                System.out.println("SEND(Type Q or q to Quit):");
                toclient = inFromUser.readLine();

                if (toclient.equals("q") || toclient.equals("Q")) {
                    outToClient.println(toclient);
                    connected.close();
                    break;
                } else {
                    outToClient.println(toclient);
                }

                fromclient = inFromClient.readLine();

                if (fromclient.equals("q") || fromclient.equals("Q")) {
                    connected.close();
                    break;
                } else {
                    System.out.println("RECIEVED:" + fromclient);
                }

            }

        }
    }
}

客户端代码:

//TCPClient.java

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

class TCPClient {
    public static void main(String argv[]) throws Exception {
        String FromServer;
        String ToServer;

        Socket clientSocket = new Socket("localhost", 5000);

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
                System.in));

        PrintWriter outToServer = new PrintWriter(
                clientSocket.getOutputStream(), true);

        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        while (true) {

            FromServer = inFromServer.readLine();

            if (FromServer.equals("q") || FromServer.equals("Q")) {
                clientSocket.close();
                break;
            } else {
                System.out.println("RECIEVED:" + FromServer);
                System.out.println("SEND(Type Q or q to Quit):");

                ToServer = inFromUser.readLine();

                if (ToServer.equals("Q") || ToServer.equals("q")) {
                    outToServer.println(ToServer);
                    clientSocket.close();
                    break;
                } else {
                    outToServer.println(ToServer);
                }
            }
        }
    }
}

当前回答

我也有同样的问题,但是在运行客户端之前运行服务器可以解决这个问题。

其他回答

我在使用名为vernemq的Mqtt代理时遇到了同样的问题。但通过添加以下内容解决了这个问题。

$ sudo vmq-admin监听显示

显示vernemq允许的ip和端口的列表

$ sudo vmq-admin监听的起始端口=1885 -a 0.0.0.0——mountpoint /appname——nr_of_acceptors=10——max_connections=20000

添加任何IP和您的新端口。现在你应该可以毫无问题地连接了。

希望它能解决你的问题。

我有同样的问题,问题是我没有关闭套接字对象。使用socket.close();问题解决了。 这个代码适用于我。

ClientDemo.java

public class ClientDemo {
    public static void main(String[] args) throws UnknownHostException,
            IOException {
        Socket socket = new Socket("127.0.0.1", 55286);
        OutputStreamWriter os = new OutputStreamWriter(socket.getOutputStream());
        os.write("Santosh Karna");
        os.flush();
        socket.close();
    }
}

而且 ServerDemo.java

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        System.out.println("server is started");
        ServerSocket serverSocket= new ServerSocket(55286);
        System.out.println("server is waiting");
        Socket socket=serverSocket.accept();
        System.out.println("Client connected");
        BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str=reader.readLine();
        System.out.println("Client data: "+str);
        socket.close();
        serverSocket.close();

    }
}

您必须将客户端套接字连接到远程ServerSocket。而不是

套接字客户端套接字 = 新套接字(“本地主机”, 5000);

do

Socket clientSocket = new Socket(serverName, 5000);

客户端必须连接到serverName, serverName应该与实例化ServerSocket的机器的名称或IP相匹配(该名称必须可以从客户端机器上访问)。顺便说一句:名字并不重要,重要的是IP地址……

在我的情况下,我不得不在tcp://localhost:2375上的暴露守护进程附近放一个检查标记,在docker设置中没有TLS(在任务栏的右侧,右键单击docker,选择设置)

我得到了这个错误,因为我在一个for循环中关闭了ServerSocket,试图接受它内部的客户端数量(我没有完成接受所有clints)

所以要小心关闭你的Socket