python socket服务器 所有客户端_我需要服务器将消息发送到所有客户端(Python,套接字)...

This is my server program, how can it send the data received from each client to every other client?

import socket

import os

from threading import Thread

import thread

def listener(client, address):

print "Accepted connection from: ", address

while True:

data = client.recv(1024)

if not data:

break

else:

print repr(data)

client.send(data)

client.close()

host = socket.gethostname()

port = 10016

s = socket.socket()

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind((host,port))

s.listen(3)

th = []

while True:

print "Server is listening for connections..."

client, address = s.accept()

th.append(Thread(target=listener, args = (client,address)).start())

s.close()

解决方案

If you need to send a message to all clients, you need to keep a collection of all clients in some way. For example:

clients = set()

clients_lock = threading.Lock()

def listener(client, address):

print "Accepted connection from: ", address

with clients_lock:

clients.add(client)

try:

while True:

data = client.recv(1024)

if not data:

break

else:

print repr(data)

with clients_lock:

for c in clients:

c.sendall(data)

finally:

with clients_lock:

clients.remove(client)

client.close()

It would probably be clearer to factor parts of this out into separate functions, like a broadcast function that did all the sends.

Anyway, this is the simplest way to do it, but it has problems:

If one client has a slow connection, everyone else could bog down writing to it. And while they're blocking on their turn to write, they're not reading anything, so you could overflow the buffers and start disconnecting everyone.

If one client has an error, the client whose thread is writing to that client could get the exception, meaning you'll end up disconnecting the wrong user.

So, a better solution is to give each client a queue, and a writer thread servicing that queue, alongside the reader thread. (You can then extend this in all kinds of ways—put limits on the queue so that people stop trying to talk to someone who's too far behind, etc.)

As Anzel points out, there's a different way to design servers besides using a thread (or two) per client: using a reactor that multiplexes all of the clients' events.

Python 3.x has some great libraries for this built in, but 2.7 only has the clunky and out-of-date asyncore/asynchat and the low-level select.

As Anzel says, Python SocketServer: sending to multiple clients has an answer using asyncore, which is worth reading. But I wouldn't actually use that. If you want to write a reactor-based server in Python 2.x, I'd either use a better third-party framework like Twisted, or find or write a very simple one that sits directly on select.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值