为什么python插座不接收数据包?

发布于 2025-01-26 12:22:08 字数 1211 浏览 3 评论 0原文

我的程序没有收到任何数据包(在UDP,Windows 10上),但是当我在Wireshark上嗅探数据时,我可以看到数据确实已发送。我知道这与我的本地网络无关,因为当我切换到热点时,它仍然无法使用。

另一件事是,该程序会为我在同一项目中与我合作的朋友接收数据,但是对于我来说,即使我为客户端和服务器使用了同一台计算机,也无法使用。

绑定套接字后通过os模块在程序中启用混杂

if os.name == “nt”:
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

什至尝试通过添加这些行

Traceback (most recent call last):
  File "C:/Users/roeym/PycharmProjects/client game/tank_trouble_dynamic_map.py", line 5, in <module>
    import tank_client
  File "C:\Users\roeym\PycharmProjects\client game\tank_client.py", line 13, in <module>
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
OSError: [WinError 10022] An invalid argument was supplied

我 ?

这就是我设置套接字的方式:

ip = socket.gethostbyname(socket.gethostname())
port = 8888

s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
s.bind((ip, port))

print(f"[!][!]  Server is up, running on {ip}, port- {port}  [!][!]")

这就是我接收数据包的方式:

while run:
    data, ip = s.recvfrom(bufferSize)
    data = str(data)
    data = data[2:]
    data = data[:-1]
    if data == "":
        continue

    print(data)

My program doesn't receive any packets (on UDP, Windows 10), but when I sniff the data on Wireshark I can see that the data is indeed sent. I know that it doesn't have anything to do with my local network because when I switch to a hotspot it still doesn't work.

Another thing is that the program receives the data for my friends who work with me on the same project, but for me, even if I'm using the same computer for the client and server it doesn't work.

I even tried to enable Promiscuous in the program via the os module after binding the socket by adding these lines:

if os.name == “nt”:
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

but all I got was

Traceback (most recent call last):
  File "C:/Users/roeym/PycharmProjects/client game/tank_trouble_dynamic_map.py", line 5, in <module>
    import tank_client
  File "C:\Users\roeym\PycharmProjects\client game\tank_client.py", line 13, in <module>
    s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
OSError: [WinError 10022] An invalid argument was supplied

Can you please help me figure out why my program doesn't receive the data?

This is how I set the socket up:

ip = socket.gethostbyname(socket.gethostname())
port = 8888

s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
s.bind((ip, port))

print(f"[!][!]  Server is up, running on {ip}, port- {port}  [!][!]")

and this is how I receive packets:

while run:
    data, ip = s.recvfrom(bufferSize)
    data = str(data)
    data = data[2:]
    data = data[:-1]
    if data == "":
        continue

    print(data)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文