java 未创建客户端套接字(ConnectException)

qqrboqgw  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(114)

我有两台主机名分别为q2w18的机器,我有两个简单的程序使用套接字在这两台机器之间发送一些消息。我有一个file.cfg,其中包含ip需要由Client.javaServer.java使用。

    • 客户**
public class Client {
    public static void main(String... C) {
        try {

            String filePath = "file.cfg";
            String ip = "";

            byte[] bytes = Files.readAllBytes(Paths.get(filePath));
            ip = new String(bytes);
            ip = ip.trim();

            System.out.println("cfg file contains " + ip);

            Socket so = new Socket(ip, 3112);
            System.out.println("socket Connected. " + so.toString());

            so.setSoTimeout(30000);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(so.getInputStream(), StandardCharsets.UTF_8));
            System.out.println(reader.readLine());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
    • 服务器**
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;

        try {
            String filePath = "file.cfg";
            String ip = "";

            byte[] bytes = Files.readAllBytes(Paths.get(filePath));
            ip = new String(bytes);
            ip = ip.trim();

            System.out.println("cfg file contains" + ip);

            serverSocket = new ServerSocket(3112, 50, InetAddress.getByName(ip));
            System.out.println("Server Socket is created. " + serverSocket.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        Socket socket = null;

        try {
            System.out.println("Waiting for Accept...");
            socket = serverSocket.accept();
            System.out.println("Accepted...");

            OutputStreamWriter localOut = new OutputStreamWriter(socket.getOutputStream());

            localOut.write("Hi,");
            localOut.write("Hello");
            localOut.write("\n");
            localOut.flush();
            localOut.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        socket.close();
        serverSocket.close();
    }
}
    • 文件. cfg**
10.90.50.101
# this ip will be similar in both machines
# for ref, 
# 10.90.50.101 -> q2 and 
# 10.90.50.102 -> w18
    • 成功案例**

1.我在w18中运行了ClientServer,运行良好
1.我在q2中运行了ClientServer,运行良好
1.我在q2中运行Client,在w18中运行服务器,运行正常

    • 故障情况**

1.当我在q2中运行Server和在w18中运行Client时,根本没有创建Client套接字。创建套接字时,客户端在一段时间后抛出以下错误。

java.net.ConnectException: Connection timed out (Connection timed out)
        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:21)
    • 问题**

我看过一些ConnectException SO的QA,但在这里,我能够实现成功场景中的第3点,但不能做同样的事情,反之亦然,这是失败场景point 1

eufgjt7s

eufgjt7s1#

在www.example.com中server.java,尝试替换以下代码

serverSocket = new ServerSocket(3112, 50, InetAddress.getByName(ip));


x一个一个一个一个x一个一个二个一个x一个一个三个一个

相关问题