我正在编写一个程序,将数据从服务器连续发送到客户机。在这里,我使用sample的时间戳将其发送给连接的多个客户机。我使用多线程来支持多个客户机。我希望每10秒把时间发给客户。但是在我的代码中,客户端在接收到第一个数据后停止。如何使客户端连续接收数据。我试着在客户端添加while循环,但这不可能。有什么建议吗
下面是示例代码:
服务器端:import socket
import os
from threading import Thread
import thread
import threading
import time
import datetime
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()
clients = set()
clients_lock = threading.Lock()