2016-08-07 2 views
0

포트 9999에서 실행되는 소켓 서버 코드가 매우 간단합니다. 서버와 클라이언트를 시작하면 netstat를 통해 서버가 실행되고 있음을 알 수 있습니다.소켓 - 간단한 서버/클라이언트 - socket.error : [Errno 10060]

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 

내 서버 코드 :

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() 
클라이언트가 7180.

그러나
TCP 192.168.1.117:9999  0.0.0.0:0    LISTENING  7180 

의 임시 포트에, 클라이언트의 출력이 오류를 보여줍니다

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 

문제가 무엇인가

그리고 내 소켓 클라이언트 프로그램은 다음과 같다? 방화벽이나 연결이 끊어지는 원인이 될 수 있습니까?

+1

왜 당신이 사용하는 socket.gethostname()이 아니라 '로컬 호스트'또는 127.0.0.1? socket.gethostname()보다 더 안정적입니다. – Munchhausen

답변

1

socket.gethostname()이 FQDN을 반환한다는 보장은 없습니다. 서버를 ''에 바인딩하십시오 (빈 문자열은 사용 가능한 모든 인터페이스를 의미하는 상징적 인 이름입니다). 그런 다음 클라이언트를 localhost 또는 127.0.0.1에 연결하십시오.

Python 설명서에는 저수준 소켓 API [1]을 사용하여 간단한 TCP 서버 - 클라이언트 응용 프로그램을 만드는 매우 유용한 예제가 포함되어 있습니다.

[1] https://docs.python.org/2/library/socket.html#example

관련 문제