1.总述
在SDN网络搭建过程中,如果Docker容器的网络设置为bridge模式,容器间的通信就不需要经过SDN网络,可以直接ping通。因此,在SDN环境中,我们通常选择将Docker容器的网络模式设置为none,以确保所有通信都经过SDN处理。然而,当容器的网络模式设置为none并进行端口映射后,我发现仍然无法进行TCP通信。同时,我还发现,设置为none和bridge模式的两个容器之间并不能直接通信,仍然需要进行网络配置。综上所述,在SDN网络中,对于需要对外通信的Docker容器,我们可以根据情况将其网络模式改为bridge,但要确保这些容器之间不需要通过SDN进行通信;而对于SDN网络中的大多数普通Docker容器,建议还是应该使用none模式。
本实验里是bridge模式。
目录
2.容器配置
2.1 编写Dockerfile
FROM python:3.8-slim
WORKDIR /app
COPY server.py .
CMD ["python", "server.py"]
# server.py
import socket
HOST = '0.0.0.0' # 监听所有网络接口
PORT = 8888 # 选择一个端口
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f'Waiting for connections on {HOST}:{PORT}')
conn, addr = s.accept()
with conn:
print(f'Connected by {addr}')
with open('received_data.txt', 'w') as f:
while True:
data = conn.recv(1024)
if not data:
break
f.write(data.decode())
print(f'Received: {data.decode()}')
2.2运行容器
首先使用刚才的dockerfile构建镜像
docker build -t mytcp02 .
因为Docker本身不支持在容器运行后动态添加端口映射。如果你需要为已运行的容器添加端口映射,你通常需要停止并删除该容器,然后使用新的端口映射参数重新创建和启动它。
docker run -itd -p 80:8080 --privileged=true --name tcp01 mytcp02
如果是使用none模式的话,即使映射端口,使用
docker ps 命令查看也在port处看不到映射。
2.3 查看连接情况
编写简单的python代码
import socket
HOST = '192.168.1.8' # 替换为Docker容器的宿主机IP地址
PORT = 8888
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world!') # 发送你想要存储的数据
发送之后打开docker的存储信息的txt文件可以看到:
证明通信成功
3.Unity内通过TCP向docker容器发送信息
编写代码的目的如下:
- 在Unity中监测物体
- 将检测到的物体名称、地点和时间传输给docker
代码如下:
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
public class TCP : MonoBehaviour
{
private float lastDetectionTime = -Mathf.Infinity;
private const float DETECTION_COOLDOWN = 10f;
private const string server = "192.168.1.8"; // Docker容器的IP地址或主机名
private const int port = 8888; // Docker容器监听的TCP端口
private TcpClient tcpClient;
private NetworkStream stream;
void Start()
{
// 初始化TCP连接
ConnectToServer();
}
private void ConnectToServer()
{
try
{
tcpClient = new TcpClient(server, port);
stream = tcpClient.GetStream();
Debug.Log("Connected to TCP server.");
}
catch (Exception e)
{
Debug.LogError("Could not connect to TCP server: " + e.Message);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Detectable")
{
if (Time.time - lastDetectionTime > DETECTION_COOLDOWN)
{
Debug.Log("Drone 02 has detected an object: " + other.gameObject.name);
lastDetectionTime = Time.time;
// 构造要发送的消息,包含物体名称、位置和时间戳
string message = $"Detected {other.gameObject.name} at {other.transform.position} on {DateTime.Now}";
SendMessageToServer(message);
}
}
}
private void SendMessageToServer(string message)
{
if (stream != null && stream.CanWrite)
{
byte[] buffer = Encoding.UTF8.GetBytes(message);
try
{
stream.Write(buffer, 0, buffer.Length);
stream.Flush(); // 确保数据被发送出去
Debug.Log($"Message sent: {message}"); // 使用Unity的Debug.Log查看发送的消息
}
catch (Exception e)
{
Debug.LogError("Error sending message to server: " + e.Message);
// 尝试重新连接服务器,如果连接断开的话
ConnectToServer();
}
}
else
{
Debug.LogError("Stream is not writable or null.");
// 尝试重新连接服务器,如果连接断开的话
ConnectToServer();
}
}
void OnDestroy()
{
// 清理TCP资源
if (stream != null)
{
stream.Close();
stream.Dispose();
}
if (tcpClient != null)
{
tcpClient.Close();
}
}
}
然后我们运行Unity,可以在控制台看到物体已经成功被监测,TCP成功链接。
在docker中查看用于保存信息的txt,也能看到信息被正确接受。