简体   繁体   English

UDP套接字未从python中的对等方接收数据

[英]UDP socket not receiving data from peer in python

I have a p2p simple chat app in python.我在 python 中有一个 p2p 简单的聊天应用程序。 A server code receives the IP and port of peers and sends each peer address to another:服务器代码接收对等点的 IP 和端口,并将每个对等点地址发送给另一个:

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 44444))

while True:
    clients = []

    while True:
        data, address = sock.recvfrom(128)
        clients.append(address)
        sock.sendto(b'ready', address)

        if len(clients) == 2:
            break

    c1 = clients.pop()
    c2 = clients.pop()

    try:
        sock.sendto('{} {} {}'.format(c1[0], c1[1], c2[1]).encode(), c2)
        sock.sendto('{} {} {}'.format(c2[0], c2[1], c1[1]).encode(), c1)
    except Exception as e:
        print(str(e))

In my client code, first I start sending client info to the server (This part works properly):在我的客户端代码中,首先我开始向服务器发送客户端信息(这部分工作正常):

import socket
import threading

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
    sock.bind(('', 40270))
    sock.sendto(b'0', ('X.X.X.X', 44444))

    while True:
        data = sock.recv(1024).decode()

        if data.strip() == 'ready':
            break

    ip, myport, dport = sock.recv(1024).decode().split(' ')
    myport = int(myport)
    dport = int(dport)

    print('\n ip: {} , myport: {} , dest: {}'.format(ip, myport, sport))

This part of the code starts listening to the server after sending the current client's info to the server and when the other client gets connected, it receives its IP and port.这部分代码在将当前客户端的信息发送到服务器后开始监听服务器,当另一个客户端连接时,它会接收其 IP 和端口。

After connecting two clients and exchanging their addresses, a p2p connection is established between them.在连接两个客户端并交换它们的地址后,它们之间就建立了 p2p 连接。

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
    sock.bind(('', myport))
    sock.sendto(b'0', (ip, dport))
    print('ready to exchange messages\n')

Then, I run a thread to start listening to the other client like this:然后,我运行一个线程开始监听另一个客户端,如下所示:

def listen():
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('', myport))

        while True:
            data = s.recv(1024)
            print('\rpeer: {}\n> '.format(data.decode()), end='')


listener = threading.Thread(target=listen, daemon=True)
listener.start()

Also, another socket is responsible to send messages:此外,另一个套接字负责发送消息:

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(('', myport))

    while True:
        msg = input('> ')
        s.sendto(msg.encode(), (ip, dport))

After all, as I said before, the server exchange the IP and port of clients properly.毕竟,正如我之前所说,服务器正确地交换了客户端的 IP 和端口。 But messages were not received by another client after sending.但是消息发送后没有被另一个客户端收到。 I think the problem is about the wrong port choice while I do the exchange.我认为问题出在我进行交换时错误的端口选择。

Regards.问候。

It is completely functional on Linux.它在 Linux 上完全可用。 The problem just occurred on a windows machine.问题刚刚发生在Windows机器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM