0

私はしばらくの間掘り下げていて、よく理解してきましたが、私がやろうとしていることに完全に到達するものは何もありません。

複数のクライアントが接続するサーバーが必要です。時々、クライアントはサーバーに新しい数値のセットを照会し(分散コンピューティング、基本的に)、それをサーバーは照会したクライアントに送り返します。

どのクライアントがサーバーにクエリを実行したかを確認して、サーバーに応答できるようにするにはどうすればよいですか?

私は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ライブラリしか使用できません。私は物事を簡単にする余分なものを手に入れることができません:)

よろしくお願いします!

4

2 に答える 2

1

どのクライアントを知る必要はありません。ソケットは、すべての目的と目的に対するクライアントです。リクエストを受け取ったのと同じソケットを介して応答を送信するだけです。

于 2013-03-17T00:07:02.660 に答える
1

行を置き換えてみてください:

                connection.send(currentlow)
                ...
                connection.send(currenthigh)

に:

                s.send(currentlow)
                ...
                s.send(currenthigh)

これにより、変数接続に格納されているクライアントではなく、データを受信した現在のクライアントにメッセージが送信されます。

于 2013-03-17T00:37:06.367 に答える