私はしばらくの間掘り下げていて、よく理解してきましたが、私がやろうとしていることに完全に到達するものは何もありません。
複数のクライアントが接続するサーバーが必要です。時々、クライアントはサーバーに新しい数値のセットを照会し(分散コンピューティング、基本的に)、それをサーバーは照会したクライアントに送り返します。
どのクライアントがサーバーにクエリを実行したかを確認して、サーバーに応答できるようにするにはどうすればよいですか?
私はPythonにかなり慣れていないので、select()関数自体は少し混乱します。新しい接続を受け入れて入力を取得することはできますが、サーバーはどのクライアントが接続しているのかを認識していないようです(または、デバッグテキストが正しくなく、認識しているのでしょうか??)。
とにかく、サーバーのコードは次のとおりです。
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'
基本的に、ある種のコマンドを受信し、向きを変えて必要なデータを送信します。ご参考までに、このサーバーは、私も作成しているCプログラムと通信する必要があります。このプログラムは、実際に計算を実行します。そのため、改行の「\0」文字を追加しています。いずれかのクライアントから-kを受信した場合は、すべてのクライアントを停止して終了する必要があります。
注:私は標準のPythonライブラリしか使用できません。私は物事を簡単にする余分なものを手に入れることができません:)
よろしくお願いします!