-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_2.py
55 lines (30 loc) · 1.15 KB
/
channel_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import socket
import datetime
from threading import Thread
TIME_FORMAT = '%H:%M:%S.%f'
thread = Thread()
thread.start()
# UDP_IP = "10.1.30.36"
UDP_IP = "192.168.43.154"
channel_num = '2'
sensor_type_num = '07'
player_num = '0'
def get_server_client_ports(channel_num, sensor_type_num, player_num):
port_server = '6' + channel_num + sensor_type_num + player_num
port_client = '60'+ channel_num + sensor_type_num
return int(port_server), int(port_client)
port_server, port_client = get_server_client_ports(channel_num, sensor_type_num, player_num)
UDP_PORT_RECEIVE = port_client
UDP_PORT_SEND = port_server
socket_receiver = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
socket_receiver.bind((UDP_IP, UDP_PORT_RECEIVE))
while True:
msg, addr = socket_receiver.recvfrom(1024) # buffer size is 1024 bytes
msg = msg.decode()
print ("received message:", msg)
print ("sender:", addr)
sender_ip = addr[0]
response_address = (sender_ip, UDP_PORT_SEND)
response_msg = datetime.datetime.now().strftime(TIME_FORMAT)[:-3]
socket_receiver.sendto(response_msg, response_address)