Hamid K
Hamid K

Reputation: 1165

Socket - Simple Server/Client - socket.error: [Errno 10060]

I have a very simple Socket Server code running on port 9999. When I fire up my server and client, with netstat I can see that the server is running and the client is on the ephemeral port of 7180.

TCP    192.168.1.117:9999     0.0.0.0:0              LISTENING       7180

However, the output of client shows this error:

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

My 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()

And my Socket client program is as follow:

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

What is the issue? Could it be a Firewall or something which cause the connection to drop?

Upvotes: 0

Views: 1820

Answers (1)

Filip Ion Dorinel
Filip Ion Dorinel

Reputation: 359

There is no guarantee that socket.gethostname() will return a FQDN. Try to bind the server to '' (empty string is a symbolic name meaning all available interfaces), then connect your client to localhost or 127.0.0.1.

Python documentation includes a very useful example for creating a simple TCP server-client application using low-level socket API [1].

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

Upvotes: 1

Related Questions