After discussions, It became apparent that the I2C might not be great for data transfer (as it freezes up the Arduino to perform the transfer. So I'm sticking with the socket code. Question now is how do I fix the code below.

My system was recently updated to python 3.6.5 to take advantage of some of the newer libraries but it broke the transfer code which was written for python 2.7.0. As I didn't write it and it isn't documented, I have to start at square one to get it to work.

Below is the code for the client (I added the comments)

import socket

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = ('',5000)

print "Connected to: ",server

running = True
while running:
    f = open(filename,"rb")
    #raw_cmd = "put "+re.search(r'\w+\.?\w+?$',filename).group(0)
    raw_cmd = "put "+ filename                                              # Put is a server side command to place file somewhere on computer
    socket.send(raw_cmd)                                                    # send command
    chunk = f.read(1024)                                                    # start pulling chunks from file to send
    while(chunk):                                                           # Loop while data is available in file
        socket.send(chunk)                                                  # Send chunk from file
        if socket.recv(1024) != "ACK":                                      # If package was not received, confirmation is receiving 'ACK', print below
            print("Failed transfer")
        chunk = f.read(1024)                                                # Grab next chunk from file for sending
    f.close()                                                               # Close file after sending all chunks
    socket.send('EOF')                                                      # 'EOF' is a kill command for server loop
    running = False
    socket.send("ACK")                                                      # Send 'ACK' to tell server if connection still exists

and here is the server code

import socket
import os

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#serversocket.bind(('localhost',4040)) # change localhost and 4040 to IP and 
PORT# of server

while True:
    c, addr = serversocket.accept()
    print('Connected to ', addr)
    running = True
    while running:
        c.send(os.getcwd()) # client needs to get the current directory, so we send it here
        raw_msg = c.recv(1024)
        if not raw_msg:
            print("Client disconnected")
        msg = raw_msg.split()
        cmd = msg[0]
        print('Received cmd ', cmd)
        if cmd == "ls":
            lsDir = os.listdir(os.getcwd())
            #Check for empty directory
            if not lsDir: string = "*empty*"
                string = '\n'.join(lsDir)
                print("string: ",string)
        elif cmd == "cd":
            target = msg[1]
        elif cmd == "mkdir":
            target = msg[1]
            newDir = os.getcwd() + "/" + target
        elif cmd == "get":
            target = msg[1]
            f = open(target,"rb");
            chunk = f.read(1024)
                #print("sending stuff ",sys.getsizeof(chunk))
                if c.recv(1024) != "ACK":
                    print "Failed transfer"
                chunk = f.read(1024)
        elif cmd == "put":
            target = msg[1]
            f = open(target,"wb");
            print("file ok")
            chunk = c.recv(1024)
            while(chunk != 'EOF'):
                print("got stuff")
                chunk = c.recv(1024)
        elif cmd == "terminate":
            running = False
            print "Received unkown cmd: "+cmd
        if c.recv(1024) != "ACK":
            print "Detected desynchronisation"

Code is full of system commands and calls and other details that I'm busy reading up on. any help explaining the code would be grateful.

  • I find it strange that the client tries to connect to an IP address that is different from the one the server is listening on. – Gerard H. Pille Jun 5 '18 at 13:00
  • "# path = socket.recv(1024) # get the current directory from the server" is commented out, but the server is still sending it upon connection. This puts you out of sync immediately. Are you sure you have the corresponding server and client code? – Gerard H. Pille Jun 5 '18 at 13:06
  • What specific error messages have you received with the code? That might guide you towards the problem; have you also tried using the 2to3 tool which might help to convert? – Aurora0001 Jun 5 '18 at 16:36
  • So going through the series of files written. I have located what appears to be a distilled version for the client side code. I shall update above with the code and test it. As for the error I'm receiving, It is actually a desyncing problem which Gerald H. Pille mentioned. I establish a connection and then absolutely nothing happens until I crash it. – Hojo.Timberwolf Jun 6 '18 at 4:40

So I figured out the majority of the problems. After deciphering the code in the file, it became apparent about the problems.

First was that python 2.7 --> 3.6.5 removed the auto-encoding and decoding which required code pieces like


to be changed into

str = "ACK"

and on the other end

raw_msg = c.recv(1024)
if raw_msg[0] == "ACK":


raw_msg = c.recv(1024)
if raw_msg.decode == "ACK":

Other errors were syntax changes between 2.7 to 3.6.5. One major issue was that the code was written in a confusing manner where the server was requesting for file locations from the client even though the server was in charge of data storage.

#raw_cmd = "put "+re.search(r'\w+\.?\w+?$',filename).group(0)
raw_cmd = "put "+ filename 

This code was supposed to send a file location and name of the file to the server to tell it to place the file in the exact same location as found on the client. This ultimately caused lots of trouble and by removing it fixed many of the problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.