2017-05-26 85 views
0

當我下面在同一無線網絡上運行的Python兩臺計算機之間的progrem,我得到了一個錯誤:爲什麼在python中會出現「Errno 10057 socket error」?

Traceback (most recent call last): 
    File "Server.EX.py", line 10, in <module> 
     des1 = s.recv(1024) 
    socket.error: [Errno 10057] A request to send or receive data was  
    disallowed because the socket is not connected and (when sending on a 
    datagram socket using a sendto call) no address was supplied 

Server.py一臺計算機上運行程序:

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((///Computer 1 IP///, 1729)) 
s.listen(1) 
conn, address = s.accept() 
print("Connection started.Enter 0 to finish connection.") 
des1 = 1 
while des1 != 0: 
    des1 = s.recv(1024) 
    print(des1) 
    conn.send(raw_input("SERVER: ")) 
conn.close() 
s.close() 

程序客戶端的.py運行在第二臺計算機上:

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((///Computer 1 IP///, 1729)) 
print("Connection started. Enter 0 to finish connection.") 
des1 = 1 
while des1 != 0: 
    s.send(raw_input("CLIENT: ")) 
    des1 = s.recv(1024) 
    print(des1) 
s.close() 

有人可以幫助我與討好( - :

*注意///計算機1 IP ///是實際的IPv4地址

回答

0

服務器套接字(s,位於您的server.py中)僅用於接受客戶端連接; .accept()的返回值包括連接的套接字(代碼中的conn),您將使用該套接字與該特定客戶端進行所有通信。您正在使用conn撥打.send(),但錯誤地使用s撥打.recv()

+0

改變了它感謝,但現在有這樣的問題: socket.error:[錯誤10048]每個套接字地址 (協議/網絡地址/端口)通常允許之一使用 –

+0

一個特定的地址/端口組合是在套接字關閉後考慮使用一段時間,以處理可能到達的任何滯留的網絡流量。在創建套接字後立即將's.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)'關閉以關閉此功能。 – jasonharper

相關問題