java.net.connectexception客户端套接字故障

时间:2018-01-13 20:29:25

标签: java server

我试图创建一个服务器,并在同一个wifi下的2台不同的计算机上运行服务器和客户端类。但是,在尝试创建客户端套接字时,我总是遇到此异常

Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
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.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:211)
at Client.main(Client.java:34)

我的服务器类:

public static void main (String[]args) throws IOException{

    Scanner in = new Scanner(System.in);

    String fromClient;
    String toClient;

    boolean made = false;

    ServerSocket serverSocket = null;

    while (!made){
        System.out.println("Please input the port number");
        int port = in.nextInt();
        try {
            serverSocket = new ServerSocket(port);
            made = true;
        } catch (IOException e){
            System.out.println("could not listen on port " + port);
        }
    }

    System.out.println("server socket established");

    //ServerSocket serverSocket = new ServerSocket(port, 8, InetAddress.getByName(ip));
    Socket client = serverSocket.accept();

    System.out.println("accepted");

    Scanner sc = new Scanner (client.getInputStream());
    PrintStream ps = new PrintStream(client.getOutputStream());

    ps.println("agiftfromserver");

    while (sc.hasNext()){
     fromClient = sc.next();

     System.out.println("from client " + fromClient);

     toClient = in.next();
     ps.println(toClient);

    }

    in.close();
    sc.close();
    serverSocket.close();
    serverSocket.close();
}

客户端:

public static void main (String[]args) throws UnknownHostException, IOException{


    Scanner in = new Scanner(System.in);

    String fromServer;
    String toServer;


    //System.out.println("Please input the ip address");
    //String ip = in.next();

    System.out.println("Please input the port number");
    int port = in.nextInt();

    Socket clientSocket = new Socket("localhost", port); //ERROR

    System.out.println("client established");

    //Socket cs = new Socket(ip, port);
    Scanner sc = new Scanner(clientSocket.getInputStream());
    PrintStream ps = new PrintStream(clientSocket.getOutputStream());

    while (sc.hasNext()){
        fromServer = sc.next();

        System.out.println("from server " + fromServer);

        toServer = in.next();
        ps.println(toServer);

    }

    in.close();
    clientSocket.close();
    sc.close();

} 

我看了其他线程,我: 尝试使用不同的端口号, 关掉防火墙, 并确保我在客户端之前运行我的服务器

当我在同一台计算机上运行这两个类但在2个不同设备上给出错误时,它可以正常工作。

交叉计算机连接是否像这样工作?

如果是的话,是否有人知道如何解决此错误。谢谢

1 个答案:

答案 0 :(得分:0)

您正在连接到localhost,根据您的问题应该是其他计算机的IP ...