回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我已经挖掘了一段时间,我带着很好的理解离开了这里,没有什么能完全理解我要做的:</p>
<p>我需要一个多个客户端连接的服务器。每隔一段时间,客户机就会向服务器查询一组要处理的新数字(基本上是分布式计算),然后服务器会将其发送回查询的客户机。你知道吗</p>
<p>我怎样才能知道哪个客户机查询了服务器,这样我就可以回复它了?你知道吗</p>
<p>我对Python还比较陌生,select()函数本身有点混乱。我能够接受新的连接并获取输入,但服务器似乎不知道哪个客户端正在与它通信(或者我的调试文本可能不正确,它确实知道??)。你知道吗</p>
<p>总之,这是服务器的代码:</p>
<pre><code>while running:
#select returns 3 subsets of the input containing sockets that have stuff to be read, empty buffer for writing, or an error
readers, writers, error = select.select(input, output, errors)
for s in readers:
if s == serversocket:
#readable server socket is ready for connection
connection, client_addr = s.accept()
print >> sys.stderr, 'new connection from', client_addr
connection.setblocking(0)
#clients.append([client_addr, performance])
input.append(connection)
output.append(connection)
else:
#readable socket, not ready for new connection
data = s.recv(1024)
print >> sys.stderr, "readable socket, not ready for new connection"
if data:
#the socket actually has data (put Compute process data into list)
print >> sys.stderr, "Data received"
substring = data[0:data.find("sss")];
print >> sys.stderr, 'received %s from %s' % (substring, client_addr)
if substring == "-get":
print >> sys.stderr, 'received -get command'
determinerange()
print >> sys.stderr, 'sending range of %d to %d', currentlow, currenthigh
temp = currentlow+"\0"
connection.send(currentlow)
temp = currenthigh+"\0"
connection.send(currenthigh)
elif substring == "-k":
print >> sys.stderr, 'received -k, stopping'
</code></pre>
<p>从本质上讲,它接收某种命令,然后转身发送所需的数据。如果你想知道的话,这个服务器必须和我正在编写的一个C程序对话,这个程序实际上是在进行计算。这就是我添加换行符“\0”的原因。如果它从任何客户端接收到-k,它需要停止每个客户端并退出。你知道吗</p>
<p>注意:我只能使用标准的python库。我不能得到任何额外的东西,使事情更容易:)</p>
<p>提前谢谢大家的帮助!你知道吗</p>