python网络通信程序

发布于 2024-09-04 06:10:05 字数 230 浏览 8 评论 0原文

基本上我想要实现的是一个允许用户通过网络在聊天室中相互连接的程序。我目前正在努力编写代码,以便用户可以在不知道其他用户正在使用的计算机的 IP 地址或服务器的 IP 地址的情况下相互连接。

有谁知道我可以简单地让所有用户扫描我的网络的 IP 范围以找到任何活动的“房间”,然后让用户有机会连接到它的方法吗?

此外,希望不需要中央服务器来运行它,而是每个用户都将简单地连接到所有其他用户,本质上同时是服务器和客户端。

Basically what I'm trying to achieve is a program which allow users to connect to a each other over a network in, essentially, a chat room. What I'm currently struggling with is writing the code so that the users can connect to each other without knowing the IP-address of the computer that the other users are using or knowing the IP-address of a server.

Does anyone know of a way in which I could simply have all of the users scan the IP range of my network in order to find any active 'room' and then give the user a chance to connect to it?

Also, the hope is that there will be no need for a central server to run this from, rather every user will simply be connected to all other user, essentially being the server and client at the same time.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-09-11 06:10:05

我可以给你两个建议。首先,发送到您网络的广播地址的 UDP 数据包将被每个人接收。其次,有一个协议可供提供某些服务的程序在本地网络上相互查找。该协议称为 mDNS、ZeroConf 或 Bonjour。

使用广播 UDP 可能是更快的路线。但如果我是你,我会学习如何使用 ZeroConf。它在 IPv6 下得到了很好的支持,并且已经被几个有趣的程序使用,例如 SubEthaEdit戈比

这是在 Python 中实现使用 ZeroConf 的好教程的链接。

另一个建议...如果您想手动编写自己的广播/多播 UDP 代码,并且可以确保您所在的所有系统都运行 2003 年左右更新的 Linux,并且所有 Windows 系统都是 XP或者更好的是,您可能可以不用使用 IPv6。 IPv6 链路本地(认为同一 LAN)所有主机多播地址为 ff02::1。这确实简单易行,并且可以到达同一 LAN 上的所有其他系统。这比必须通过 IPv4 找出网络的广播地址要好得多。

I can give you two suggestions. First of all, UDP packets to the broadcast address of your network will be received by everybody. Secondly, there is a protocol for programs offering certain services to find each other on a local network. That protocol is called mDNS, ZeroConf, or Bonjour.

Using broadcast UDP is likely going to be the faster route. But if I were you, I'd learn how to use ZeroConf instead. It's supported well under IPv6 and already used by several interesting programs such as SubEthaEdit and Gobby.

Here is a link to a nice tutorial for implementing something that speaks ZeroConf in Python.

Another recommendation... If you want to hand roll your own broadcast/multicast UDP code and you can be sure that all of the systems you're on are running a Linux that's newer than 2003 or so, and all the Windows systems are XP or better, you can probably get away with using IPv6. The IPv6 link-local (think same LAN) all hosts multicast address is ff02::1. That's really simple and easy, and it will reach all the other systems on the same LAN. It's much better than having to figure out what your network's broadcast address is with IPv4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文