1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Pythonsocket實現(xiàn)多對多全雙工通信的方法-創(chuàng)新互聯(lián)

      服務(wù)器:#server.py

      成都創(chuàng)新互聯(lián)是一家專業(yè)提供伊金霍洛企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、網(wǎng)站建設(shè)H5場景定制、小程序制作等業(yè)務(wù)。10年已為伊金霍洛眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進行中。
      #!/usr/bin/env python
      #-*-coding:utf-8-*-
      import sys
      import struct#將字符串打包為二進制流進行網(wǎng)絡(luò)傳輸
      import select#
      import signal#用于捕獲中斷信號
      import cPickle#將python對象進行序列化:dumps將python對象序列化保存為字符串,loads與之相反
      from socket import *
      HOST = ''
      def send(channel,*args):#發(fā)送數(shù)據(jù)
        buffer = cPickle.dumps(args)
        value = htonl(len(buffer))
        size = struct.pack("L",value)
        channel.send(size)
        channel.send(buffer)
      def receive(channel):#接收數(shù)據(jù)
        size = struct.calcsize("L")
        size = channel.recv(size)
        try:
          size = ntohl(struct.unpack("L",size)[0])#socket.ntohl(參考:http://blog.csdn.net/tatun/article/details/7194973)
        except struct.error,e:
          return ''
        buf = ''
        while len(buf) < size:
          buf += channel.recv(size-len(buf))
        return cPickle.loads(buf)[0]#恢復(fù)python對象
       
      class ChatServer(object):
        def __init__(self,PORT,backlog = 5):
          self.clients = 0
          self.clientmap = {}
          self.outputs = [] #Client會話列表
          self.server = socket(AF_INET, SOCK_STREAM)
          self.server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)#重用套接字地址
          self.server.bind((HOST,PORT))
          self.server.listen(backlog)
          signal.signal(signal.SIGINT,self.signalhandler)#使用signal模塊捕獲中斷操作 SIGINT中斷進程(ctrl+c), SIGTERM 終止進程,SIGKILL殺死進程,SIGALRM 鬧鐘信號
       
        def signalhandler(self,signum,frame):#中斷處理方法
          print "Shutting down server ..."
          for output in self.outputs:
            output.close()
          self.server.close()
       
        def get_client_name(self,client):
          info = self.clientmap[client]
          host,port,name = info[0][0],info[0][1],info[1]
          return ':'.join((('@'.join((name,host))),str(port)))
       
        def run(self):
          inputs = [self.server]
          print 'Waiting for connect...'
          while True:
            try:
              readable,writeable,execption = select.select(inputs,self.outputs,[])
            except select.error,e:
              break
            for sock in readable:
              if sock == self.server:#服務(wù)器端接收
                client,address = self.server.accept()
                print "Chat server: connected from",address
                self.clients += 1
                cname = receive(client)
                send(client,str(address[0]))
                inputs.append(client)
                self.clientmap[client] = (address,cname)
                msg = "(Connected : New Client(%d) from %s)\n"%(self.clients,self.get_client_name(client))
                message = "At present, only one of you is in the chat room!"
                if self.clients == 1:
                  send(client,message)
                for output in self.outputs:
                  send(output,msg)
                self.outputs.append(client)#將開始回話的client加入Client回話列表
       
              #elif sock == sys.stdin:
                #break
              else:
                try:
                  data = receive(sock)
                  if data:
                    msg = '[' + self.get_client_name(sock)+ '] >> ' + data
                    for output in self.outputs:
                      if output!=sock:
                        send(output,msg)
                  else:
                    self.clients-=1
                    sock.close()
                    inputs.remove(sock)
                    self.outputs.remove(sock)
                    msg = '(Now hung up: Client from %s)'%self.get_client_name(sock)
                    message = "At present, only one of you is in the chat room!"
                    for output in self.outputs:
                      send(output,msg)
                    if self.clients == 1:
                      send(self.outputs[0],message)
                except error,e:
                  inputs.remove(sock)
                  self.outputs.remove(sock)
          self.server.close()
      if __name__ == "__main__":
          server = ChatServer(6004)
          server.run()
       
      

      網(wǎng)頁題目:Pythonsocket實現(xiàn)多對多全雙工通信的方法-創(chuàng)新互聯(lián)
      本文地址:http://ef60e0e.cn/article/dogijc.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        太康县| 海宁市| 三穗县| 河间市| 鄱阳县| 固镇县| 克什克腾旗| 龙陵县| 新兴县| 故城县| 比如县| 昭平县| 东海县| 民丰县| 巴林右旗| 巩义市| 桃园县| 额敏县| 纳雍县| 晋江市| 右玉县| 武威市| 益阳市| 井陉县| 石棉县| 阿拉善左旗| 舒兰市| 巴青县| 临夏市| 奉新县| 江口县| 益阳市| 雅江县| 莒南县| 辽阳县| 弥勒县| 长泰县| 海门市| 运城市| 会昌县| 宁晋县|