0

I'm programming a FTP client with Lua, and due to some limitations, I can only use sockets to connect to the FTP server (I can't use any specific library). When the file is fully sent, my program closes the data socket, but then I get an error on the FTP server: [ERROR] recv: connection reset by peer; [ERROR] shutdown: Socket is not connected . How am I supposed to finish the file transfer without raising an error on the server?

This is the code I've been using on Lua:

    function sendData(data)
        if data_skt == nil then
            error("An error occurred during data sending.")
        end
        return Socket.send(data_skt, data)
    end
    
    function openDataSocket()
        consoleWrite("Opening data channel on port " ..data_port.. "...\n")
        data_skt = Socket.connect(ip, data_port, is_ssl)
    end

    function closeDataSocket()
        Socket.close(data_skt)
        data_skt = nil
    end

    (...)

    function storeFile(filename)
        sendResponsiveCommand("TYPE", "I")
        enterPassiveMode()
        sendCommand("STOR", filename)
        openDataSocket()
        recvResponse()
        consoleWrite("Transfering " .. filename .. "...\n")
        input = io.open(client_dir..filename,FREAD)
        local filesize = io.size(input)
        local i = 0
        while i < filesize do
            packet_size = math.min(524288,filesize-i)
            i = i + sendData(io.read(input,i,packet_size))
        end
        closeDataSocket()
        io.close(input)
        recvResponse()
        recvResponse()
        enterPassiveMode()
        listServerDirectory()
        need_refresh = true
    end
FerdinandoPH
  • 113
  • 1
  • 8
  • Apparently, I need to shut down the socket instead of simply closing it. However, the library I'm using doesn't have that function, so I guess I'm stuck – FerdinandoPH Mar 12 '23 at 11:40

0 Answers0