简体   繁体   English

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

[英]Client side socket isn't creating (ConnectException)

I have two machines with hostname q2 and w18 and I have a two simple program to send some message between these machines using socket.我有两台主机名为q2w18的机器,我有两个简单的程序使用套接字在这些机器之间发送一些消息。 I have one file.cfg which contains the ip needs to be used by both Client.java and Server.java .我有一个包含file.cfgip需要被Client.javaServer.java使用。

Client客户

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();
        }
    }
}

Server服务器

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();
    }
}

file.cfg文件.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

Success Scenarios成功案例

  1. I ran both Client and Server in w18 , which works fine我在w18中同时运行了ClientServer ,效果很好
  2. I ran both Client and Server in q2 which works fine我在q2中同时运行了ClientServer ,效果很好
  3. I ran Client in q2 and Server in w18 which works fine我在q2中运行Client ,在w18中运行服务器,效果很好

Failure Scenario失败场景

  1. When I ran Server in q2 and Client in w18 .当我在q2中运行Server并在w18中运行Client时。 Client Socket isn't creating at all. Client套接字根本没有创建。 Client throwing below error after some time while creating a socket.创建套接字一段时间后,客户端抛出以下错误。
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)

Question问题

I've seen some ConnectException SO's QA's, But here I'm able to achieve the point 3 in Success Scenarios but not able to do the same thing vice versa which is point 1 of Failure Scenario .我看过一些ConnectException SO 的 QA,但在这里我能够在成功场景中实现第 3 点,但不能做同样的事情,反之亦然,即失败场景point 1

in server.java try replacing below code在 server.java 尝试替换下面的代码

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

with

serverSocket = new ServerSocket(3112, 50, "localhost");
serverSocket = new ServerSocket(3112, 50, "127.0.0.1");
serverSocket = new ServerSocket(3112, 50, "0.0.0.0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM