2017-01-18 26 views
0

次のコードでサーバーをコーディングしようとしています。それはスレッド化されており、私はこのエラーです。OSError IDで奇妙なエラーが発生しました:WinError 10038

Unhandled exception in thread started by <function threaded_client at 0x0000000003302158> 
line 28, in threaded_client 
data = conn.recv(2048) 
OSError: [WinError 10038] An operation was attempted on something that is not a socket 

このエラーは解決できません。このエラーは解決できませんでした。私は本当にそれを修正する方法を知りたいです。

import socket 
import sys 

from _thread import * 
import time 

host = '' 
port = 5555 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

try: 
    s.bind((host,port)) 
except socket.error as e: 
    print(str(e))  


s.listen(5) 

print("Waiting for a Connection...") 
def threaded_client(conn): 

    conn.send(str.encode("Welcome, please write to the server.\r\n")) 
    userinput = "" 

    while True: 
     data = conn.recv(2048) 
     data = data.decode('utf-8') 
     if not data: 
      break 
     #data = data.decode('utf-8') 
     #Gather up the Input 
     if data == '\r\n': 
      userinput += data 
     #Do something with it 
     else: 
      reply = 'Server output: ' + userinput 
      conn.sendall(str.encode(reply)) 
      userinput = "" 

      conn.close() 
while True: 
    conn, addr = s.accept() 
    print("connected to: " +addr[0] + ': ' + str(addr[1])) 
    start_new_thread(threaded_client, (conn,))  

ここで、サーバーとクライアントとのやり取りに関する問題が発生しています。私のCMDウィンドウのイメージが開いている。修正するコードを提供してください。 Windows 8の場合は、 Image Link

+0

これを確認してください。 http://stackoverflow.com/questions/15210178/python-socket-programming-oserror-winerror-10038-an-operation-was-attempted-o?bcsi_scan_94a977aee9df674a=3DSJp4a+bDXk1nRlDXbwkv84AccIAAAA76R6ZQ12 – Prajwal

+0

朝は午前中です遅くなってしまったので、すぐに返事を期待してはいけません! – JerryPlayz101

答えて

0

この部分を確認してください。

else: 
    reply = 'Server output: ' + userinput 
    conn.sendall(str.encode(reply)) 
    userinput = "" 
    conn.close() #<--- here you are closing the connection 

これはループの外側にある必要があります。このようなもの。

while True: 
     data = conn.recv(2048) 
     data = data.decode('utf-8') 
     if not data: 
      break 
     #data = data.decode('utf-8') 
     #Gather up the Input 
     if data == '\r\n': 
      userinput += data 
     #Do something with it 
     else: 
      reply = 'Server output: ' + userinput 
      conn.sendall(str.encode(reply)) 
      userinput = "" 

conn.close() #<--- Notice the indent 
+0

ありがとうPrajwal - まもなくテストします – JerryPlayz101

+0

もう一度お悔やみします - しかし、各キーストロークの後に "Server Output"メッセージが表示され、ユーザーがEnterキーを押した後にそれを欲しいという問題があります。どのように考えていますか? – JerryPlayz101

+0

あなたを迷惑して申し訳ありませんが、質問の別の部分であなたの助けが必要です。 JerryPlayz101

関連する問題