Ich habe einen sehr einfachen Socket Server Code, der auf Port 9999 läuft. Wenn ich meinen Server und meinen Client starte, kann ich mit netstat sehen, dass der Server läuft und der Client auf dem kurzlebig Port von 7180.Socket - Einfacher Server/Client - socket.error: [Errno 10060]
TCP 192.168.1.117:9999 0.0.0.0:0 LISTENING 7180
jedoch der Ausgang des Client zeigt diesen Fehler:
Traceback (most recent call last):
File "client.py", line 6, in <module>
clisock.connect((host, 9999))
File "C:\Python27\lib\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Mein Server-Code:
import socket
import sys
import time
srvsock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
print 'Server Socket is Created'
host = socket.gethostname()
try:
srvsock.bind((host, 9999))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
srvsock.listen(5)
print 'Socket is now listening'
while True:
clisock, (remhost, remport) = srvsock.accept()
print 'Connected with ' + remhost + ':' + str(remport)
currentTime = time.ctime(time.time()) + "\r\n"
print currentTime
clisock.send(currentTime)
clisock.close()
srvsock.close()
Und Programm meines Socket-Client ist wie folgt:
import socket
clisock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
print host
clisock.connect((host, 9999))
tm = clisock.recv(1024)
clisock.close()
print tm
Was ist das Problem? Könnte es eine Firewall oder etwas sein, das die Verbindung abbricht?
Warum Sie verwenden socket.gethostname() und nicht 'localhost' oder 127.0.0.1? Es ist zuverlässiger als socket.gethostname(). – Munchhausen