Docker 个容器之间的 SCTP 连接挂起
SCTP connections between Docker containers hangs
我在设置 Docker 容器、通过 SCTP 通信时遇到了一些问题。所有相关文件如下。
运行 docker-组合后,输出如下:
$ docker-compose up
Starting server ... done
Starting client ... done
Attaching to server, client
server | Opening SctpServerChannel...
server | Binding to: server/172.18.0.2:3868
server | Waiting for new connection....
client | Opening SctpChannel...
client | Binding to: client/172.18.0.3:0
client | Connecting to: server/172.18.0.2:3868
客户端从未成功连接到服务器(同样,服务器也从未接收到客户端的连接)。
src/SctpClient.java
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.nio.sctp.SctpChannel;
public class SctpClient
{
public static void main(String[] args) throws IOException
{
String[] parts = args[0].split(":");
InetSocketAddress local = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
parts = args[1].split(":");
InetSocketAddress remote = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
System.out.println("Opening SctpChannel...");
SctpChannel client = SctpChannel.open();
System.out.println("Binding to: "+local);
client.bind(local);
System.out.println("Connecting to: "+remote);
if (client.connect(remote))
System.out.println("Connected!");
else
System.out.println("Connection failed!");
}
}
src/SctpServer.java
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;
public class SctpServer
{
public static void main(String[] args) throws IOException
{
String[] parts = args[0].split(":");
InetSocketAddress local = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
System.out.println("Opening SctpServerChannel...");
SctpServerChannel server = SctpServerChannel.open();
System.out.println("Binding to: "+local);
server.bind(local);
System.out.println("Waiting for new connection....");
while (true)
{
SctpChannel client = server.accept();
if (client == null)
System.out.println("Connection failed!");
else
System.out.println("Connected: "+client.getRemoteAddresses());
}
}
}
docker-compose.yml
version: '3.7'
services:
server:
build:
context: .
dockerfile: Dockerfile-Server
image: server
container_name: server
restart: unless-stopped
environment:
- LOCAL_IFACE=server:3868
ports:
- "3868:3868/sctp"
networks:
- sctp
client:
build:
context: .
dockerfile: Dockerfile-Client
image: client
container_name: client
restart: unless-stopped
environment:
- LOCAL_IFACE=client:0
- REMOTE_IFACE=server:3868
networks:
- sctp
depends_on:
- server
stdin_open: true
tty: true
networks:
sctp:
name: sctp
Docker文件客户端
FROM openjdk:11-jdk
RUN apt-get update -y && apt-get install lksctp-tools -y && apt-get clean
WORKDIR /opt
COPY src/SctpClient.java .
CMD java SctpClient.java $LOCAL_IFACE $REMOTE_IFACE
Docker文件服务器
FROM openjdk:11-jdk
RUN apt-get update -y && apt-get install lksctp-tools -y && apt-get clean
WORKDIR /opt
COPY src/SctpServer.java .
CMD java SctpServer.java $LOCAL_IFACE
有人有什么建议吗?
我想毕竟这么久我应该post我是如何让它工作的:
有趣的是,从 docker-compose.yml 中删除端口的发布允许容器之间的 SCTP 工作。这与 TCP 的工作方式不同。
此外,如果您希望主机网络上的应用程序通过 SCTP 与容器通信,则必须使用主机网络。与 TCP 的工作方式相比,这也有所不同。
在最初 post 的时候,似乎 SCTP 支持在 docker 中仍然很原始。
我在设置 Docker 容器、通过 SCTP 通信时遇到了一些问题。所有相关文件如下。
运行 docker-组合后,输出如下:
$ docker-compose up
Starting server ... done
Starting client ... done
Attaching to server, client
server | Opening SctpServerChannel...
server | Binding to: server/172.18.0.2:3868
server | Waiting for new connection....
client | Opening SctpChannel...
client | Binding to: client/172.18.0.3:0
client | Connecting to: server/172.18.0.2:3868
客户端从未成功连接到服务器(同样,服务器也从未接收到客户端的连接)。
src/SctpClient.java
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.nio.sctp.SctpChannel;
public class SctpClient
{
public static void main(String[] args) throws IOException
{
String[] parts = args[0].split(":");
InetSocketAddress local = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
parts = args[1].split(":");
InetSocketAddress remote = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
System.out.println("Opening SctpChannel...");
SctpChannel client = SctpChannel.open();
System.out.println("Binding to: "+local);
client.bind(local);
System.out.println("Connecting to: "+remote);
if (client.connect(remote))
System.out.println("Connected!");
else
System.out.println("Connection failed!");
}
}
src/SctpServer.java
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;
public class SctpServer
{
public static void main(String[] args) throws IOException
{
String[] parts = args[0].split(":");
InetSocketAddress local = new InetSocketAddress(parts[0], Integer.parseInt(parts[1]));
System.out.println("Opening SctpServerChannel...");
SctpServerChannel server = SctpServerChannel.open();
System.out.println("Binding to: "+local);
server.bind(local);
System.out.println("Waiting for new connection....");
while (true)
{
SctpChannel client = server.accept();
if (client == null)
System.out.println("Connection failed!");
else
System.out.println("Connected: "+client.getRemoteAddresses());
}
}
}
docker-compose.yml
version: '3.7'
services:
server:
build:
context: .
dockerfile: Dockerfile-Server
image: server
container_name: server
restart: unless-stopped
environment:
- LOCAL_IFACE=server:3868
ports:
- "3868:3868/sctp"
networks:
- sctp
client:
build:
context: .
dockerfile: Dockerfile-Client
image: client
container_name: client
restart: unless-stopped
environment:
- LOCAL_IFACE=client:0
- REMOTE_IFACE=server:3868
networks:
- sctp
depends_on:
- server
stdin_open: true
tty: true
networks:
sctp:
name: sctp
Docker文件客户端
FROM openjdk:11-jdk
RUN apt-get update -y && apt-get install lksctp-tools -y && apt-get clean
WORKDIR /opt
COPY src/SctpClient.java .
CMD java SctpClient.java $LOCAL_IFACE $REMOTE_IFACE
Docker文件服务器
FROM openjdk:11-jdk
RUN apt-get update -y && apt-get install lksctp-tools -y && apt-get clean
WORKDIR /opt
COPY src/SctpServer.java .
CMD java SctpServer.java $LOCAL_IFACE
有人有什么建议吗?
我想毕竟这么久我应该post我是如何让它工作的:
有趣的是,从 docker-compose.yml 中删除端口的发布允许容器之间的 SCTP 工作。这与 TCP 的工作方式不同。
此外,如果您希望主机网络上的应用程序通过 SCTP 与容器通信,则必须使用主机网络。与 TCP 的工作方式相比,这也有所不同。
在最初 post 的时候,似乎 SCTP 支持在 docker 中仍然很原始。