Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket.error: [Errno 111] Connection refused

I am trying to write a program for file transfer using sockets. The server end of the code is running fine. However, in the client side I get the following error

Traceback (most recent call last): File "client.py", line 54, in <module> uploadFiles(directory) File "client.py", line 36, in uploadFiles transferFile(fname) File "client.py", line 13, in transferFile      cs.connect((HOST, 36258)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused 

My code is as follows

import os import socket  def transferFile(fname):    HOST = '127.0.0.1'    CPORT = 36258    MPORT = 36250    FILE = fname    cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    cs.connect((HOST, 36258))    cs.send("SEND " + FILE)    cs.close()    ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    ms.connect((HOST, MPORT))    f = open(FILE, "rb")    data = f.read()    f.close()    ms.send(data)    ms.close()  def uploadFiles(directory):    home = os.getenv("HOME")    folder = str(home + "/" + directory)    os.chdir(folder)    dirList = os.listdir(folder)    print dirList    for fname in dirList:        if fname.endswith('.bin'):            transferFile(fname)  os.chdir(os.getenv("HOME")) directory = "testdownload" if not os.path.exists(directory):    os.makedirs(directory)  os.chdir(directory)  uploadFiles(directory) 

I tried looking for help on Google and other posts on Stack Overflow, none of them helped. Can someone please help me? The line numbers may be different since I pasted only some part of the code

like image 894
srnvs Avatar asked Jul 20 '12 18:07

srnvs


People also ask

What is Errno 111 Connection Refused?

For example, the classic Connection refused errno 111 means that the initial SYN packet to the host you connect() to was responded with an RST packet instead of the normal SYN+ACK — which usually happens when there's no program listening to the given port on the remote computer you connect() to.

How to solve Connection refused error in Python?

check from the server, if you're accepting the connections to the server: again based on your OS, but telnet LISTENING_IP LISTENING_PORT should do the job. check if you can access the port of the server from the client, but not using the code: just us the telnet (or appropriate command for your OS) from the client.

What is socket error connection refused?

Windows socket error 10061 is a Connection refused error sent to you by the target machine. You could not make a connection because the target machine actively refused it. The most common cause is a Network/Windows firewall or security software that blocking the connection on the port specified by the client.


1 Answers

The problem obviously was (as you figured it out) that port 36250 wasn't open on the server side at the time you tried to connect (hence connection refused). I can see the server was supposed to open this socket after receiving SEND command on another connection, but it apparently was "not opening [it] up in sync with the client side".

Well, the main reason would be there was no synchronisation whatsoever. Calling:

cs.send("SEND " + FILE) cs.close() 

would just place the data into a OS buffer; close would probably flush the data and push into the network, but it would almost certainly return before the data would reach the server. Adding sleep after close might mitigate the problem, but this is not synchronisation.

The correct solution would be to make sure the server has opened the connection. This would require server sending you some message back (for example OK, or better PORT 36250 to indicate where to connect). This would make sure the server is already listening.

The other thing is you must check the return values of send to make sure how many bytes was taken from your buffer. Or use sendall.

(Sorry for disturbing with this late answer, but I found this to be a high traffic question and I really didn't like the sleep idea in the comments section.)

like image 156
tomasz Avatar answered Sep 24 '22 23:09

tomasz