2017-08-23 9 views
0

に送る私は、彼らがソケットPythonのソケットTCP誰か

に接続したときに、私は、ソケットのTCPサーバーの使用socket.sendto(文字列のすべてのクライアントを再コードソケットTCPサーバーと2つのソケットクライアント

def loopRedis(self): 
    socket = self.request 
    while True: 
     for key in r.scan_iter(match='push:*', count=100000): 
#   print key 
      ##find the msg that I need to push 
      data = json.loads(r.get(key)) 
      ##get the userid from push:* 
      flag = False 
      for userKey in r.scan_iter(match='redis:*', count=100000): 
       if flag == False: 
        if r.hget(userKey,'userid') == data['userid']: 
         print '=======' 
         print r.hget(userKey,'ip') 
         print int(r.hget(userKey,'id')) 
         print '|||||||||' 
         #find the map(ip and id and userid)from redis:* 
         socket.sendto(prpcrypt().encrypt(r.get(key)), (r.hget(userKey,'ip'),int(r.hget(userKey,'id')))) 
         flag = True 
    #     r.delete(key) ##delete when I send 
      time.sleep(2) ## looping 

を開始しました[、フラグ]、アドレス)メソッド。

しかし、なぜ両方のクライアントがデータを受信しましたか。私は誰かに送るだけです。

どうすればいいですか?ありがとうございました。

+0

サーバーとクライアントから完全なコードを入れてもいいですか、私は本物ではなく単純化しています –

+0

@ gsi-frank今はどうですか? –

+0

問題を理解するためにあまりにも多くの作業を手伝ってくれるようにしてくれる多くの定型コード。問題に関係するコードを思いついてください:クライアントソケット、サーバーソケット、および汎用データ転送コード(スクリプトの詳細は除きますが、コードを乱雑にするもの) –

答えて

0

あなたがコードのクライアント側を示していれば少し役に立ちましたが、私はあなたを助けるために最善を尽くします。

次は、私はあなたの質問から解釈するものである:

方法socketobj.sendto(data, (host, port))は、実際にはPythonでのネットワークudp(ユーザデータグラムプロトコル)で使用されている方法socketobj.recvfrom(BUFFER_SIZE)、とペアになっています。 tcpプロトコルを使用してデータを送信するには、以下を実行する必要があります。

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
host = xxx.xxx.xxx.x #whatever ip 
port = x #whatever port (make sure it is the same for the client in tcp) 
s.bind((host, port)) 
clients = [] #clients use the clients.append(whatever_to_add_to_list) method to add clients 
for client in clients: #for loop to iterate through the client list 
    client.connection.send(data)