Im new in Python. And I can't make the server listen to two ports at the same time. This is the code I have written until now:
sock_client1 = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_client1.bind((SEND_IP, SEND_CLIENT_PORT))
sock_client1.setblocking(0)
sock_client2 = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_client2.bind((SEND_IP, SEND_SERVER_PORT))
sock_client2.setblocking(0)
while True:
try:
ready_client1 = select.select([sock_client1], [], [], None)
ready_client2 = select.select([sock_client2], [], [], None)
if ready_client1[0]:
pkt_recv_raw, addr = sock_client1.recvfrom(4096)
port = SEND_CLIENT_PORT
if ready_client2[0]:
pkt_recv_raw, addr = sock_client2.recvfrom(4096)
port = SEND_SERVER_PORT
When I run this code together with a client, the server can't receive anything. It just works when I use only one of the ready_client's.
Thanks in advance!
ready_client1 = select.select([sock_client1], [], [], None) ready_client2 = select.select([sock_client2], [], [], None)
Try using a single select
:
ready_read, ready_write, exceptional = select.select(
[sock_client1, sock_client2], [], [], None)
for ready in ready_read:
pkt_recv_raw, addr = ready.recvfrom(4096)
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.