64

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

srnvs
  • 997
  • 2
  • 10
  • 22
  • How do you know that the server end is working? The error message suggests that the TCP connection to 127.0.0.1:36258 is being refused. What happens when you run `telnet 127.0.0.1 36258`? – tc. Jul 20 '12 at 18:58
  • 1
    I ran `telnet 127.0.0.1 36258` and was able to see the connection. `Trying 127.0.0.1...` `Connected to 127.0.0.1.` `Escape character is '^]'.` I checked the server end and found that there was an issue with the sockets not opening up in sync with the client side Thanks – srnvs Jul 20 '12 at 19:16
  • 1
    To add to the above comment, I added a 2 second delay after the `cs.close()` and after `ms.close()` so as to sync with connections on the server end and it worked like a charm. Thanks tc – srnvs Jul 20 '12 at 19:25
  • Please clarify: How can `cs.close()` (client-side) and `ms.close()` (server-side) be in the same code ? – Pe Dro Jan 14 '21 at 13:28

1 Answers1

49

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.)

tomasz
  • 12,574
  • 4
  • 43
  • 54
  • can i use the same port on client side to connect to multiple servers? each server is a just copy of one another. This is just to load balance the requests. – y_159 May 16 '20 at 04:53
  • @y_159, To my knowledge, each server binds to a single port. So having mutliple servers in same port isn't possible. Can anyone verify my claim? – Adarsh TS Oct 25 '20 at 16:21