服务器:#server.py
#!/usr/bin/env python #-*-coding:utf-8-*- import sys import struct#将字符串打包为二进制流进行网络传输 import select# import signal#用于捕获中断信号 import cPickle#将python对象进行序列化:dumps将python对象序列化保存为字符串,loads与之相反 from socket import * HOST = '' def send(channel,*args):#发送数据 buffer = cPickle.dumps(args) value = htonl(len(buffer)) size = struct.pack("L",value) channel.send(size) channel.send(buffer) def receive(channel):#接收数据 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]#恢复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:#服务器端接收 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()
客户端:#client.py
#!/usr/bin/env python #-*-coding:utf-8-*- from server import send,receive from socket import * import sys import select import cPickle import struct import signal class ChatClient(object): def __init__(self,name): self.name = name self.connected = False self.host = 'localhost' self.port = 6004 try: self.sock = socket(AF_INET,SOCK_STREAM) self.sock.connect((self.host,self.port)) self.connected = True send(self.sock,self.name) data= receive(self.sock) addr = data except error,e:#socket.serro print 'Failed to connect to chat server' sys.exit(1) def run(self): while True: try: readable,writeable,exception = select.select([0,self.sock],[],[]) for sock in readable: if sock == 0: data = sys.stdin.readline().strip() if data: send(self.sock,data) else: data=receive(self.sock) if not data: print 'Client shutting down.' self.connected = False break else: sys.stdout.write(data+'\n') sys.stdout.flush() except KeyboardInterrupt: print 'Client interrupted' self.sock.close() break if __name__ == "__main__": name = raw_input("Please input login name > ") client=ChatClient(name) client.run()
以上这篇Python socket实现多对多全双工通信的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]