<tfoot id='oQX9Y'></tfoot>
  • <i id='oQX9Y'><tr id='oQX9Y'><dt id='oQX9Y'><q id='oQX9Y'><span id='oQX9Y'><b id='oQX9Y'><form id='oQX9Y'><ins id='oQX9Y'></ins><ul id='oQX9Y'></ul><sub id='oQX9Y'></sub></form><legend id='oQX9Y'></legend><bdo id='oQX9Y'><pre id='oQX9Y'><center id='oQX9Y'></center></pre></bdo></b><th id='oQX9Y'></th></span></q></dt></tr></i><div id='oQX9Y'><tfoot id='oQX9Y'></tfoot><dl id='oQX9Y'><fieldset id='oQX9Y'></fieldset></dl></div>
  • <legend id='oQX9Y'><style id='oQX9Y'><dir id='oQX9Y'><q id='oQX9Y'></q></dir></style></legend>

        <small id='oQX9Y'></small><noframes id='oQX9Y'>

        • <bdo id='oQX9Y'></bdo><ul id='oQX9Y'></ul>

        如何使 LAN 客户端可以发现服务器

        时间:2023-10-10

        <i id='rvhZo'><tr id='rvhZo'><dt id='rvhZo'><q id='rvhZo'><span id='rvhZo'><b id='rvhZo'><form id='rvhZo'><ins id='rvhZo'></ins><ul id='rvhZo'></ul><sub id='rvhZo'></sub></form><legend id='rvhZo'></legend><bdo id='rvhZo'><pre id='rvhZo'><center id='rvhZo'></center></pre></bdo></b><th id='rvhZo'></th></span></q></dt></tr></i><div id='rvhZo'><tfoot id='rvhZo'></tfoot><dl id='rvhZo'><fieldset id='rvhZo'></fieldset></dl></div>

          <small id='rvhZo'></small><noframes id='rvhZo'>

            <tfoot id='rvhZo'></tfoot>
              <tbody id='rvhZo'></tbody>

                <bdo id='rvhZo'></bdo><ul id='rvhZo'></ul>
                • <legend id='rvhZo'><style id='rvhZo'><dir id='rvhZo'><q id='rvhZo'></q></dir></style></legend>
                • 本文介绍了如何使 LAN 客户端可以发现服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Python 开发一个多人游戏,该游戏使用套接字库进行网络连接.游戏将支持局域网播放.一名玩家将设置服务器,局域网上的其他玩家将能够加入游戏.

                  I am working on a multiplayer game in python that uses the socket library for its networking. The game will support play over LAN. One player will set up the server and other players on the LAN will be able to join the game.

                  为了实现这一点,我需要一种让玩家发现可用服务器列表的简单方法(不应该期望玩家必须输入 IP 地址!).我首选的解决方案将仅使用 python 套接字库(以及可选的标准库的其他部分).

                  To implement this, I need a simple way for the players to discover a list of available servers (players shouldn't be expected to have to enter IP addresses!). My preferred solution would use only the python socket library (and optionally other parts of the standard library).

                  我正在寻找的是客户端和服务器代码:

                  What I am looking for is client and server code:

                  • 客户端:将其对游戏的请求广播到侦听 LAN 上某个端口的所有机器

                  • client: broadcasts its request for games to all machines listening on a certain port on the LAN

                  服务器:回复客户端的可用性

                  server(s): replies to the client with its availability

                  尝试的答案 按照 Hans 在下面的答案中的建议,可以使用 UDP 套接字来响应来自客户端的广播请求.

                  ATTEMPTED ANSWER Following Hans' advice in his answer below, a UDP socket can be used to respond broadcast requests from the client.

                  服务器:

                  #UDP server responds to broadcast packets
                  #you can have more than one instance of these running
                  import socket
                  address = ('', 54545)
                  server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                  server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
                  server_socket.bind(address)
                  
                  while True:
                      print "Listening"
                      recv_data, addr = server_socket.recvfrom(2048)
                      print addr,':',recv_data
                      server_socket.sendto("*"+recv_data, addr)
                  

                  客户:

                  #UDP client broadcasts to server(s)
                  import socket
                  
                  address = ('<broadcast>', 54545)
                  client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                  client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
                  
                  data = "Request"
                  client_socket.sendto(data, address)
                  while True:
                      recv_data, addr = client_socket.recvfrom(2048)
                      print addr,recv_data
                  

                  还有其他令人信服的方法来处理这个可发现性问题吗?

                  Are there other compelling ways to handle this discoverability problem?

                  推荐答案

                  你可以试试 UDP 广播.你可以例如从客户端发送广播.然后,服务器应使用其地址广播响应,以便客户端可以使用常规连接.

                  You could try a UDP broadcast. You can e.g. send a broadcast from the client. The server should then broadcast a response with its address so the client can use a regular connection.

                  有关示例代码,请参见此处:http://wiki.python.org/moin/UdpCommunication

                  See here for some example code: http://wiki.python.org/moin/UdpCommunication

                  这篇关于如何使 LAN 客户端可以发现服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么右键单击会在圆圈中心创建一个橙色点? 下一篇:为什么我无法访问 Screen.ids?

                  相关文章

                  最新文章

                      • <bdo id='fLmuf'></bdo><ul id='fLmuf'></ul>
                    1. <legend id='fLmuf'><style id='fLmuf'><dir id='fLmuf'><q id='fLmuf'></q></dir></style></legend>
                    2. <tfoot id='fLmuf'></tfoot>
                    3. <small id='fLmuf'></small><noframes id='fLmuf'>

                      <i id='fLmuf'><tr id='fLmuf'><dt id='fLmuf'><q id='fLmuf'><span id='fLmuf'><b id='fLmuf'><form id='fLmuf'><ins id='fLmuf'></ins><ul id='fLmuf'></ul><sub id='fLmuf'></sub></form><legend id='fLmuf'></legend><bdo id='fLmuf'><pre id='fLmuf'><center id='fLmuf'></center></pre></bdo></b><th id='fLmuf'></th></span></q></dt></tr></i><div id='fLmuf'><tfoot id='fLmuf'></tfoot><dl id='fLmuf'><fieldset id='fLmuf'></fieldset></dl></div>