python网络通信程序
基本上我想要实现的是一个允许用户通过网络在聊天室中相互连接的程序。我目前正在努力编写代码,以便用户可以在不知道其他用户正在使用的计算机的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以给你两个建议。首先,发送到您网络的广播地址的 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.