简体   繁体   English

Docker 和 java sockets:在容器之间共享数据

[英]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.因此,当我在java NetBeans IDE运行我的程序时,它可以按我的意愿工作。 I get the files我得到文件

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

For the docker connection i created a network with对于 docker 连接,我创建了一个网络

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.虽然客户端-服务器连接通过 docker 容器成功,但当我运行这两个服务时,我没有得到任何 output。

<---Available files--->

I'm stuck in this for days.我被困在这几天了。 What might be wrong?可能有什么问题? If I should provide any other information please tell me.如果我应该提供任何其他信息,请告诉我。

PS at the server-side of docker I set the server image as the host newclientSocket = new Socket("server", 4000) PS在docker服务器端我将服务器镜像设置为主机newclientSocket = new Socket("server", 4000)

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.您的路径C:\ServerMusicStorage无法在您的容器中工作,因为此文件不在您的容器中。

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此外,您的路径是 windows 路径,您应该将其更改为 unix 路径(/yourdirectory),因为大多数 docker 图像是 ZE09606A5872CEDD776 系统

If you want to use COPY in your DockerFile just add如果您想在 DockerFile 中使用COPY ,只需添加

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但如果我没记错的话,src 文件应该是一个相对路径... 相关所以你必须把 /ServerMusicStorage 放在你的构建目录附近

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM