Hi!

Can anyone show me what I need to do to upload a file via FTP?
I can't figure out, how to open a data port after the STOR command...


TIA,
bAZiK
Posted on 2002-06-08 16:45:03 by bazik
to receive information you will need to open a data port you must send a PASV command. the response you will get will be something like:

227 Entering Passive Mode (128,243,20,9,165,101).

using this information you will need to make some calculations to determine the port to connect to.

128,243,20,9 ;<-- this is the same ip address you connected with on port 21
165,101 ;<-- these 2 numbers after some multipliction and addition will give you the data port you will need to connect to.

here is the formula you will need to use to get the port number:
1st number * 256 + 2nd number

so it will look like this:
165 * 256 + 101 = the data port

so the port you will need to connect to is port 42341


you can also send the PORT command. the PORT command im not to fimilar with but i know instead of the server you are connected to deciding the port you decide your own port to connect to. i think alot of servers try to avoid using PORT for security reasons.


__________________
Posted on 2002-06-09 01:11:04 by smurf
I dont know if you want to use raw sockets or would be happy with wininet, in one of my curretn projects i needed to upload a file to an ftp server - this is the easiest way i found to do it:

(note some lines of code removed for simplicity - the original code was in the middle of a thread with some sync code for another thred)



invoke InternetOpen,addr AppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0
mov hInternet,eax


; Establish a connection to the FTP server specified

invoke InternetConnect,hInternet,addr FTPAddress,INTERNET_DEFAULT_FTP_PORT,addr UserName, addr Password,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0
mov hConnect,eax


;upload the file
invoke FtpPutFile,hConnect,addr TEST_TEMP,addr NewName,INTERNET_FLAG_RELOAD Or INTERNET_FLAG_RESYNCHRONIZE,0

;now close all the stuff

invoke InternetCloseHandle,hConnect
invoke InternetCloseHandle,hInternet




Hope this helps
Posted on 2002-06-09 03:04:07 by Terab
Hi Terab, Hi smurf!

Thanks so far for your advice. I code this program in PowerBASIC, so I just needed to know how to open the data port. Here is what I get from the FTP server (done via puTTY :) ):


220 FTP Server ready.
USER p544837
331 Password required for p544837.
PASS ********
230 User p544837 logged in.
CWD /tmp
250 CWD command successful.
TYPE I
200 Type set to I.
PASV
227 Entering Passive Mode (195,20,225,16,221,179).
STOR test.txt


Should I open the data-port before or after the STOR command?
In this case the data port should be
221 * 256 + 179 = 56755
right? :)

But perhaps I'll use the WinInet way... it's enough for my needs, too.
BTW, here is the little PowerBASIC snippet. Look how easy TCP stuff can be:


FUNCTION UploadFiles(sServer AS ASCIIZ, sUser AS ASCIIZ, sPass AS ASCIIZ, sDirectory AS ASCIIZ, sFileList AS ASCIIZ) EXPORT AS LONG

DIM hTCP AS LONG
DIM sBuffer AS STRING

hTCP = FREEFILE
TCP OPEN PORT 21 AT sServer AS hTCP

IF ERR THEN
sBuffer = "Error connecting to FTP server!
GOTO lblError
ELSE
TCP LINE hTCP, sBuffer
END IF

IF LEFT$(sBuffer, 3) <> "220" THEN
GOTO lblError
END IF

TCP PRINT hTCP, "USER " + sUser

TCP LINE hTCP, sBuffer

IF LEFT$(sBuffer, 3) <> "331" THEN
sBuffer = "USER error: " + sBuffer
GOTO lblError
END IF

TCP PRINT hTCP, "PASS " + sPass

TCP LINE hTCP, sBuffer

IF LEFT$(sBuffer, 3) <> "230" THEN
sBuffer = "PASS error: " + sBuffer
GOTO lblError
END IF

TCP PRINT hTCP, "CWD " + sDirectory

TCP LINE hTCP, sBuffer

IF LEFT$(sBuffer, 3) <> "250" THEN
sBuffer = "CWD error: " + sBuffer
GOTO lblError
END IF

TCP PRINT hTCP, "TYPE I"

TCP LINE hTCP, sBuffer

IF LEFT$(sBuffer, 3) <> "200" THEN
sBuffer = "TYPE I error: " + sBuffer
GOTO lblError
END IF

Posted on 2002-06-09 06:30:47 by bazik

Hi Terab, Hi smurf!

[
Should I open the data-port before or after the STOR command?
In this case the data port should be
221 * 256 + 179 = 56755
right? :)

BTW, here is the little PowerBASIC snippet. Look how easy TCP stuff can be:




I *think* you should open the port before using the STOR command.

even easier in asm with wininet :-)
Posted on 2002-06-09 06:37:27 by Terab
Hehe, works! Thanks again!



FUNCTION UploadFile(sServer AS ASCIIZ, sUser AS ASCIIZ, sPass AS ASCIIZ, sRemoteDir AS ASCIIZ, sInputDir AS ASCIIZ) EXPORT AS LONG

DIM hInternet AS LONG
DIM hConnect AS LONG
DIM sFile AS STRING

hInternet = InternetOpen("WebShot FTP Engine", %INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0)

hConnect = InternetConnect(hInternet, sServer, %INTERNET_DEFAULT_FTP_PORT, sUser, sPass, _
%INTERNET_SERVICE_FTP, %INTERNET_FLAG_PASSIVE, 0)

sFile = DIR$(sInputDir + "*.jpg")

CALL FtpSetCurrentDirectory(hConnect, sRemoteDir)

DO WHILE sFile <> ""

'msgbox sInputDir + sFile

IF FtpPutFile(hConnect, sInputDir + sFile, sFile + $NUL, %INTERNET_FLAG_TRANSFER_BINARY OR %INTERNET_FLAG_RELOAD, 0) <> 1 THEN
CALL InternetCloseHandle(hConnect)
CALL InternetCloseHandle(hInternet)
FUNCTION = 1
EXIT FUNCTION
END IF

sFile = DIR$

LOOP

CALL InternetCloseHandle(hConnect)
CALL InternetCloseHandle(hInternet)

FUNCTION = 0

END FUNCTION
Posted on 2002-06-09 09:39:11 by bazik
Hi!
Terab, your variant is very interesting but where can I see help (hlp or chm) for suc functions as InternetConnect, FtpPutFile and so on from the wininet?
Thanx,
Mike
Posted on 2002-06-10 06:07:59 by Mike
Posted on 2002-06-10 06:29:17 by bazik
Yup, like bazik says - the platform sdk, if you have a msdn library disk it *should* be on it
Posted on 2002-06-10 06:47:17 by Terab
The link bAZiK provided doesn't work. Has it expired? I'd like to see what was in this post

Thanks,

farrier
Posted on 2002-06-11 06:42:10 by farrier
Hiro deleted some old posts... this was one of them :(

If you are looking for the PlatformSDK, look here:
http://www.microsoft.com/msdownload/platformsdk/sdkupdate
Posted on 2002-06-11 07:10:51 by bazik
bAZik, if you are interested, ante and I are working on a FTP Client written in assembly, he has the project posted on www.sourceforge.net

I'm a newbie on assembly but I have written my own FTP Client using C on a Unix box

Just FYI

Later,
gorshing
Posted on 2002-06-12 10:01:37 by gorshing