2016-05-20 31 views
0

私は、クライアントとサーバを作成しようとしています。クライアントはサーバに文字列を送り、サーバは応答を返します。Pythonソケット - WinError 10054

これは私のクライアントの方法

def send(self): 
    s = socket.socket() 
    s.connect(("127.0.0.1", 5012)) 

    message = bytes("Send!", "utf-8") 
    s.send(message) 

    data = s.recv(1024) 
    data = str(data, "utf-8") 

    print(data) 

    s.close() 

これはクライアントメッセージを待ち、サーバ内のメソッドです。これを実行する

def listener(self): 
     print("Startet") 
     s = socket.socket() 
     s.bind(("127.0.0.1", 5012)) 
     s.listen(1) 
     while True: 
      c, addr = s.accept() 

      while True: 
       data = c.recv(1024) 
       data = str(data, "utf-8") 

       print(data) 

       c.send(bytes("OK", "utf-8")) 
      c.close() 

私が取得:

Startet 
Send! 

Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "C:\Anaconda3\lib\threading.py", line 914, in _bootstrap_inner 
    self.run() 
    File "C:\Anaconda3\lib\threading.py", line 862, in run 
    self._target(*self._args, **self._kwargs) 
    File "C:\workspace\Server.py", line 41, in listener 
    data = c.recv(1024) 
ConnectionAbortedError: [WinError 10053] 
An established connection was disconnected by the software on the hostcomputer 

少なくともそれがメッセージをrecievesが、その後突然停止しますので、それは!センドを出力します。サーバーは常時稼働していて、クライアントのsend関数から任意の量のメッセージを 受け取る必要があります。

答えて

0

クライアントはデータが利用可能かどうかをチェックせずにsend()とrecv()を実行します(例えばaccept()を使用)。ソケットが非ブロック状態の場合、recv()はただちに戻ります(または、何か他の理由以外は例外です)。空文字列が表示され、ソケットが閉じられます。そのため、サーバーはConnectionAbortedErrorを返し、クライアントは既に接続を閉じています。これをチェックするには、クライアントrecv()の周りにtry/exceptを追加します。

関連する問題