• <tfoot id='pgBeG'></tfoot>
      <bdo id='pgBeG'></bdo><ul id='pgBeG'></ul>
  • <small id='pgBeG'></small><noframes id='pgBeG'>

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

        使用 select() 监听 tcp 和 udp 消息

        use select() to listen on both tcp and udp message(使用 select() 监听 tcp 和 udp 消息)

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

              <legend id='kkFhK'><style id='kkFhK'><dir id='kkFhK'><q id='kkFhK'></q></dir></style></legend>

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

                  <bdo id='kkFhK'></bdo><ul id='kkFhK'></ul>
                  本文介绍了使用 select() 监听 tcp 和 udp 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我尝试此代码时,我只收到 TCP 消息:

                  I only get the TCP message when I try this code:

                  from socket import *
                  from select import select
                  
                  def read_tcp(s):
                      while True:
                          client,addr = s.accept()
                          data = client.recv(8000)
                          client.close()
                          print "Recv TCP:'%s'" % data
                  
                  def read_udp(s):
                      while True:
                          data,addr = s.recvfrom(8000)
                          print "Recv UDP:'%s'" % data
                  
                  def run():
                      host = ''
                      port = 8888
                      size = 8000
                      backlog = 5
                  
                      # create tcp socket
                      tcp = socket(AF_INET, SOCK_STREAM)
                      tcp.bind(('',port))
                      tcp.listen(backlog)
                  
                      # create udp socket
                      udp = socket(AF_INET, SOCK_DGRAM)
                      udp.bind(('',port))
                  
                      input = [tcp,udp]
                  
                      while True:
                          inputready,outputready,exceptready = select(input,[],[])
                  
                          for s in inputready:
                              if s == tcp:
                                  read_tcp(s)
                              elif s == udp:
                                  read_udp(s)
                              else:
                                  print "unknown socket:", s
                  
                  if __name__ == '__main__':
                      run()
                  

                  而客户端是这样的:

                  from socket import *
                  
                  def send_tcp():
                      s = socket(AF_INET,SOCK_STREAM)
                      s.connect(('localhost',8888))
                      data="TCP "*4
                      s.send(data)
                      s.close()
                  
                  def send_udp():
                      s = socket(AF_INET,SOCK_DGRAM)
                      data="UDP "*4
                      s.sendto(data, ('localhost',8888))
                      s.close()
                  
                  if __name__ == '__main__':
                      send_tcp()
                      send_udp()
                  

                  推荐答案

                  1. 摆脱 read_tcp() 和 read_udp() 中的while"循环.select() 循环是您唯一需要的循环:它将根据需要经常调用 read_XXX() 方法.read_XXX() 方法应该只处理一个事件.

                  1. Get rid of the 'while' loops in read_tcp() and read_udp(). The select() loop is the only loop you need: it will call the read_XXX() methods as often as required. The read_XXX() methods should handle exactly one event.

                  你的 read_tcp() 方法应该分成两部分:一个接受一个套接字并将其添加到选择集中,另一个读取一个接受的套接字.相应地调整选择循环.

                  Your read_tcp() method should be split into two parts: one to accept a socket and add it to the selection set, and another to read an accepted socket. Adjust the select loop accordingly.

                  这篇关于使用 select() 监听 tcp 和 udp 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                  • <bdo id='Xh6rt'></bdo><ul id='Xh6rt'></ul>
                    <legend id='Xh6rt'><style id='Xh6rt'><dir id='Xh6rt'><q id='Xh6rt'></q></dir></style></legend>
                    <i id='Xh6rt'><tr id='Xh6rt'><dt id='Xh6rt'><q id='Xh6rt'><span id='Xh6rt'><b id='Xh6rt'><form id='Xh6rt'><ins id='Xh6rt'></ins><ul id='Xh6rt'></ul><sub id='Xh6rt'></sub></form><legend id='Xh6rt'></legend><bdo id='Xh6rt'><pre id='Xh6rt'><center id='Xh6rt'></center></pre></bdo></b><th id='Xh6rt'></th></span></q></dt></tr></i><div id='Xh6rt'><tfoot id='Xh6rt'></tfoot><dl id='Xh6rt'><fieldset id='Xh6rt'></fieldset></dl></div>
                      • <small id='Xh6rt'></small><noframes id='Xh6rt'>

                          • <tfoot id='Xh6rt'></tfoot>
                              <tbody id='Xh6rt'></tbody>