[英]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.我有两台主机名为
q2
和w18
的机器,我有两个简单的程序使用套接字在这些机器之间发送一些消息。 I have one file.cfg
which contains the ip
needs to be used by both Client.java
and Server.java
.我有一个包含
file.cfg
的ip
需要被Client.java
和Server.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成功案例
Client
and Server
in w18
, which works finew18
中同时运行了Client
和Server
,效果很好Client
and Server
in q2
which works fineq2
中同时运行了Client
和Server
,效果很好Client
in q2
and Server in w18
which works fineq2
中运行Client
,在w18
中运行服务器,效果很好Failure Scenario失败场景
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.