Reputation: 21
I quite new in docker and network and I have a udp servet with two ports for listen and send message and the same in the client side. The goal of this connection is they are synchronising the flags which the server is on a docker container and the client is on local host. Here is the server code:
import socket
import time
import random
class Physical:
def __init__(self, remote_ip, remote_port, port, t):
self.remote_ip = remote_ip
self.remote_port = remote_port
self.port = port
self.t = t
self.tick = 0
self.ctrl_flag = 0
self.ctrl_url = None
self.sw_state = 0
self.sw_package = None
self.receive_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.receive_socket.bind(("0.0.0.0", self.port))
def run(self):
while True:
# input here
if random.randint(0, 20) <= 3:
self.ctrl_flag = random.randint(0, 10)
print("in ctrl_flag {} {}".format(self.ctrl_flag, self.tick))
else:
print("same ctrl_flag {} {}".format(self.ctrl_flag, self.tick))
self.receive_sync()
# intermediary states
self.tick += 1
self.send_sync()
print("new ctrl_flag {}, {}\n".format(self.ctrl_flag, self.tick))
def send_sync(self):
self.send_socket.sendto("{}".format(self.ctrl_flag).encode(), (self.remote_ip, self.remote_port))
def receive_sync(self):
data, addr = self.receive_socket.recvfrom(256)
self.ctrl_flag = int(data)
def main():
phy = Physical("0.0.0.0", 5555, 5556, 0)
phy.run()
if __name__ == "__main__":
main()
The client code is:
import socket
import random
import asyncio
#SERVER = socket.gethostbyname(socket.gethostname())
class Twin:
def __init__(self, remote_ip, remote_port, port, t):
self.remote_ip = remote_ip
self.remote_port = remote_port
self.port = port
self.t = t
self.tick = 0
self.ctrl_flag = 0
self.ctrl_url = None
self.sw_state = 0
self.sw_package = None
self.receive_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.receive_socket.bind(("127.0.0.1", self.port))
async def run(self):
while True:
# time.sleep(self.t)
await asyncio.sleep(self.t)
# input here
if random.randint(0, 20) <= 3:
self.ctrl_flag = random.randint(0, 10)
print("in ctrl_flag {}, ticket {}".format(self.ctrl_flag, self.tick))
else:
print("same ctrl_flag {}, ticket {}".format(self.ctrl_flag, self.tick))
self.send_sync()
# intermediary state
self.tick += 1
self.receive_sync()
print("new ctrl_flag {}, ticket {}\n".format(self.ctrl_flag, self.tick))
def send_sync(self):
self.send_socket.sendto("{}".format(self.ctrl_flag).encode(), (self.remote_ip, self.remote_port))
def receive_sync(self):
data, addr = self.receive_socket.recvfrom(256)
self.ctrl_flag = int(data)
def main():
twin = Twin("127.0.0.1", 5556, 5555, 2)
loop = asyncio.get_event_loop()
# loop.call_later(time, twin.stop())
task = loop.create_task(twin.run())
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass
if __name__ == "__main__":
main()
The dockerfile to build the image is:
FROM python:3.9
WORKDIR /Physical
COPY . /Physical
EXPOSE 5555/udp
EXPOSE 5556/udp
ENV NAME digitaltwin
CMD ["python3", "-u", "Physical.py"]
For running the image once I used this command docker run -p 5555:5550/udp -p 5556:5559/udp digitaltwin:latest
the server and the client couldn't connect without any specific error but it was like they were running separatly and couldn't go forward to sync the flags.
The other command I used was forwarding the same port to the docker container docker run -p 5555:5555/udp -p 5556:5556/udp digitaltwin:latest
when I tried to run the client the error was the address is allocated already.
Does anyone know how I can fix it?
Upvotes: 0
Views: 84