Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

Docker on java sockets : java.net.ConnectException: Connection refused (Connection refused)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default, docker uses automatic DNS resolution between containers. Containers can resolve each other by name or alias. (Docker bridge networks)

On the client side, you explicitly defined IP address of the server: Socket serverSocker = new Socket("192.168.2.5", 25000);.

Docker doesn't know about it, instead of this use name or alias of the container: Socket serverSocker = new Socket("server", 25000);

Or you can dynamically pass a host of the server via environment variable: Socket serverSocket = new Socket(System.getenv("SERVER_HOST_ENV"), 25000);

Don't forget to pass an environment variable: docker run --env SERVER_HOST_ENV=server --network-alias server --network client_server_network -it server


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...