I have 2 java files Server.java and Client.java. Both are in separate containers.
DOCKER FILES: The dockerfile(in the folder named 'Server') that i use for server is:
FROM java:8
COPY Server.java /
RUN javac Server.java
EXPOSE 25000
ENTRYPOINT ["java"]
CMD ["Server"]
The dockerfile(in the folder named 'Client') for client is:
FROM java:8
COPY Client.java /
RUN javac Client.java
EXPOSE 25000
ENTRYPOINT ["java"]
CMD ["Client"]
To build the cotainers i use
docker build . (for the client Dockerfile)
docker build . (for the server Dockerfile)
For the network setup i use
docker network create client_server_network
docker run --network-alias server --network client_server_network -it serverimage
docker run --network client_server_network -it clientimage
The error im getting when i run the client image is
java.net.ConnectException: Connection refused (Connection refused)
Client.java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.2.5", 25000);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Server.java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(25000);
Socket s = ss.accept();// establishes connection
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = (String) dis.readUTF();
System.out.println("message= " + str);
ss.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
What im doing wrong?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…