Reputation: 1012
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. I have one file.cfg
which contains the ip
needs to be used by both Client.java
and 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
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 fineClient
and Server
in q2
which works fineClient
in q2
and Server in w18
which works fineFailure Scenario
Server
in q2
and Client
in w18
. Client
Socket isn't creating at all. 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.
Upvotes: 0
Views: 59
Reputation: 101
in server.java try replacing below code
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");
Upvotes: 1