我最近在linux上建立了套接字 - 服務器聊天。嘗試連接套接字時出現錯誤10051
當我試圖連接我的窗口8.1主計算機(兩者在相同的子網),我得到一個:
[Errno 10051]: A socket operation was attempted to an unreachable network
代碼:
import socket
import datetime
import getpass # Get username
import sys
import thread
import select
def client():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", 8856))
sock.connect(("192.168.1.17", 23657))
sock.send(getpass.getuser()+"[$ID$]4sthg")
socket_list = [sys.stdin, sock]
partner = raw_input("Chat partner: ")
while 1:
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock_index in read_sockets:
# incoming message from remote server
if sock_index == sock:
data = sock_index.recv(4096)
if not data :
print '\nDisconnected from chat server'
sys.exit()
else :
#print data
sys.stdout.write(data)
# user entered a message
else:
msg = sys.stdin.readline()
sock.send(partner + "|" + msg)
def get_time():
return datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S')
def main():
client()
if __name__ == "__main__":
main()
我試着 「ping」 來我的筆記本電腦地址,它運行良好。還在我的linux防火牆中添加了一條規則,聲明每個數據包都來自我的windows計算機是允許的。
任何想法爲什麼會發生?
謝謝!
顯示您的代碼。很可能地址不能正確地從字符串轉換爲網絡字節順序整數形式。 –