Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Docker and java sockets: Share data between containers

I'm coding a client-server service where my server is sending files to the client.

In the following example, I tried to send a list of file names to my client.

Server

serverSocket = new ServerSocket(4000);
connsock = serverSocket.accept();
objectOutput = new ObjectOutputStream(connsock.getOutputStream());

List<String> file_names = new ArrayList<String>();
File[] files = new File("C:\ServerMusicStorage").listFiles();

for (File file : files) {
    if (file.isFile()) {
        file_names.add(file.getName());
    }
}
objectOutput.writeObject(file_names);
objectOutput.flush();

Client

newclientSocket = new Socket("localhost", 4000);
objectInput1 = new ObjectInputStream(newclientSocket.getInputStream());

System.out.println("<---Available files--->");

// get list of files from server
Object file_names = objectInput1.readObject();
file_list = (ArrayList<String>) file_names;
int count = 1;

for (int i = 0; i < file_list.size(); i++) {
    System.out.println(count + ")" + file_list.get(i));
    count++;
}

So when I run my program at java NetBeans IDE it works as I want. I get the files

<---Available files--->
1)blank.wav
2)fuark.wav

For the docker connection i created a network with

docker network create client_server_network

I run the server with

docker run --env SERVER_HOST_ENV=server --network-alias server --network client_server_network -it server

and the client with

docker run --network client_server_network -it clientimage

Although the client-server connection is successful through docker containers, when I run both services I don't get any output.

<---Available files--->

I'm stuck in this for days. What might be wrong? If I should provide any other information please tell me.

P.S. at the server-side of docker I set the server image as the host newclientSocket = new Socket("server", 4000)


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

1 Reply

0 votes
by (71.8m points)

Container have their own fileSystem different from the host filesystem. Your path C:ServerMusicStorage can't work in your container because this file is not in your container.

You should take a look to bind or volume

Or copying the file when creating your image.

Also your path is a windows path you should change it for an unix path (/yourdirectory) because most of docker images are linux system

If you want to use COPY in your DockerFile just add

COPY ServerMusicStorage/ /ServerMusicStorage 

But if i'm not mistaken src file should be a relative path... related to So you have to put /ServerMusicStorage near your build dir


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...