tchous
asked on
How do i create a simple ftp connection with winsock control?
I am trying to create a simple ftp connection that will go out to a server and get a specified file. But I cant get to the point to enter a username and password. I want to use the winsock control to do this. When I connect this is what I get:
connect to "somewhere"
220 "somewhere" FTP server ( version blah blah blah ) ready.
and it stops there.
Plus I would also like to do error trapping, so that if there is an error at all it closes out, and returns an error message as to what happened.
This is simply for getting one ascii file and bringing it back to use.
Thanks
connect to "somewhere"
220 "somewhere" FTP server ( version blah blah blah ) ready.
and it stops there.
Plus I would also like to do error trapping, so that if there is an error at all it closes out, and returns an error message as to what happened.
This is simply for getting one ascii file and bringing it back to use.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Const ftpDIR As Integer = 0
Privat e Const ftpPUT As Integer = 1
Privat e Const ftpGET As Integer = 2
Privat e Const ftpDEL As Integer = 3
Privat e iLastFTP As Integer
Privat e Sub cmdConnect_Click()
On Error GoTo ConnectError
In et1.URL = txtURL
In et1.UserNa me = txtUserName
In et1.Passwo rd = txtPassword
In et1.Protoc ol = icFTP
iL astFTP = ftpDIR
In et1.Execut e Inet1.URL, "DIR"
End Sub
Privat e Sub Inet1_StateChanged(ByVal _
St ate As Integer)
Se lect Case State
Case icNone
sbFTP.Panels("stat us").Text = ""
Case icResolvingHost
sbFTP.Panels("stat us").Text
= "Resolving Host"
Case icHostResolved
sbFTP.Panels("stat us").Text _
= "Host Resolved"
Case icConnecting
sbFTP.Panels("stat us").Text _
= "Connecting..."
Case icConnected
sbFTP.Panels("stat us").Text _
= "Connected!"
Case icRequesting
sbFTP.Panels("stat us").Text _
= "Requesting..."
Case icRequestSent
sbFTP.Panels("stat us").Text _
= "Request Sent"
Case icReceivingResponse
sbFTP.Panels("stat us").Text _
= "Receiving Response..."
Case icResponseReceived
sbFTP.Panels("stat us").Text _
= "Response Received!"
Case icDisconnecting
sbFTP.Panels("stat us").Text _
= "Disconnecting..."
Case icDisconnected
sbFTP.Panels("stat us").Text _
= "Disconnected"
Case icError
sbFTP.Panels("stat us").Text _
= "Error! " & Trim(CStr( _
Inet1 .ResponseC ode)) & _
": " & Inet1.ResponseInfo
Case icResponseCompleted
sbFTP.Panels("stat us").Text _
= "Response Completed!"
Reac tToRespons e iLastFTP
En d Select
End Sub
Public Function _
Re actToRespo nse(ByVal _
iL astCommand As Integer) As Long
Se lect Case iLastCommand
Case ftpDIR
Show RemoteFile List
Case ftpPUT
MsgB ox "File Sent from " & CurDir()
Case ftpGET
MsgB ox "File Received "& "in " & CurDir()
Case ftpDEL
En d Select
End Function
Public Function ShowRemoteFileList() As Long
Di m sFileList As String
Di m sTemp As String
Di m p As Integer
sT emp = Inet1.GetChunk(1024)
Do While Len(sTemp) > 0
DoEvents
sFileLis t = sFileList & sTemp
sTemp = Inet1.GetChunk(1024)
Lo op
ls tRemoteFil es.Clear
Do While sFileList > ""
DoEvents
p = InStr(sFileList, vbCrLf)
If p > 0 Then
lstR emoteFiles .AddItem
Left(sFile List, p - 1)
If Len(sFileList) > (p + 2) Then
sFileList = Mid(sFileList, p + 2)
Else
sFileList = ""
End If
Else
lstR emoteFiles .AddItem sFileList
sFil eList = ""
End If
Lo op
End Function
Public Function GetFiles(sFileList As String) As Long
Di m sFile As String
Di m sTemp As String
Di m p As Integer
iL astFTP = ftpGET
sT emp = sFileList
Do While sTemp > ""
DoEvents
p = InStr(sTemp, "|")
If p Then
sFil e = Left(sTemp, p - 1)
sTem p = Mid(sTemp, p + 1)
Else
sFil e = sTemp
sTem p = ""
End If
Inet1.Ex ecute Inet1.URL, "GET " & sFile & _
" " & sFile
'w ait until this execution is done
`b efore going to next file
Do
DoEv ents
Loop Until Not _
Inet 1.StillExe cuting
Lo op
iL astFTP = ftpDIR
In et1.Execut e Inet1.URL, "DIR"
End Function
Public Function PutFiles(sFileList As String) As Long
Di m sFile As String
Di m sTemp As String
Di m p As Integer
iL astFTP = ftpPUT
sT emp = sFileList
Do While sTemp > ""
DoEvents
p = InStr(sTemp, "|")
If p Then
sFil e = Left(sTemp, p - 1)
sTem p = Mid(sTemp, p + 1)
Else
sFil e = sTemp
sTem p = ""
End If
Inet1.Ex ecute Inet1.URL, "PUT" & sFile & _
" " & sFile
'w ait until this execution is done
`b efore going to next file
Do
DoEv ents
Loop Until Not Inet1.StillExecuting
Lo op
iL astFTP = ftpDIR
In et1.Execut e Inet1.URL, "DIR"
End Function
Privat e Sub dirLocal_Change()
fi lLocal.Pat h = dirLocal.Path
End Sub
Privat e Sub drvLocal_Change()
di rLocal.Pat h = drvLocal.Drive
End Sub
Privat e Sub filLocal_DragDrop(Source _
As Control, X As Single, Y As Single)
'r eceiving files from FTP site.
Di m I As Integer
Di m sFileList As String
If TypeOf Source Is ListBox Then
For i = 0 _
To Source.ListCount - 1
If Source.Selected(i) Then
sFileList = _
sFileL ist & _
Source .List(i) & "|"
End If
Next
En d If
If Len(sFileList) > 0 Then
'strip off the last pipe
sFileLis t = Left(sFileList, _
Len( sFileList) - 1)
GetFiles sFileList
En d If
End Sub
Privat e Sub _
fi lLocal_Mou seDown(But ton As _
In teger, Shift As Integer, X As _
Si ngle, Y As Single)
fi lLocal.Dra g vbBeginDrag
End Sub
Privat e Sub filLocal_MouseUp(Button _
As Integer, Shift As Integer, _
X As Single, Y As Single)
fi lLocal.Dra g vbEndDrag
End Sub
Privat e Sub _
ls tRemoteFil es_DragDro p(Source _
As Control, X As Single, Y As Single)
Di m I As Integer
Di m sFileList As String
If TypeOf Source Is FileListBox Then
For i = 0 To Source.ListCount - 1
If Source.Selected(i) Then
sFileList = sFileList & _
Source.L ist(i) & "|"
End If
Next
En d If
If Len(sFileList) > 0 Then
'strip off the last pipe
sFileLis t = Left(sFileList, _
Len( sFileList) - 1)
PutFiles sFileList
En d If
End Sub
Privat e Sub _
ls tRemoteFil es_KeyDown (KeyCode _
As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
Inet1.Ex ecute Inet1.URL, "DEL " & _
lstR emoteFiles .List( _
lstR emoteFiles .ListIndex )
Do
DoEv ents
Loop While Inet1.StillExecuting
En d If
iL astFTP = ftpDIR
In et1.Execut e Inet1.URL, "DIR"
End Sub
Privat e Sub _
ls tRemoteFil es_MouseDo wn(Button _
As Integer, Shift As Integer, )
X As Single, Y As Single)
ls tRemoteFil es.Drag vbBeginDrag
End Sub
Privat e Sub lstRemoteFiles_MouseUp(But ton As _
In teger, Shift As Integer, _
X As Single, Y As Single)
ls tRemoteFil es.Drag vbEndDrag
End Sub
Privat
Privat
Privat
Privat
Privat
On
In
In
In
In
iL
In
End Sub
Privat
St
Se
En
End Sub
Public
Re
iL
Se
En
End Function
Public
Di
Di
Di
sT
Do
Lo
ls
Do
Lo
End Function
Public
Di
Di
Di
iL
sT
Do
'w
`b
Lo
iL
In
End Function
Public
Di
Di
Di
iL
sT
Do
'w
`b
Lo
iL
In
End Function
Privat
fi
End Sub
Privat
di
End Sub
Privat
'r
Di
Di
If
En
If
En
End Sub
Privat
fi
In
Si
fi
End Sub
Privat
As
X As Single, Y As Single)
fi
End Sub
Privat
ls
As
Di
Di
If
En
If
En
End Sub
Privat
ls
As
If
En
iL
In
End Sub
Privat
ls
As
X As Single, Y As Single)
ls
End Sub
Privat
In
X As Single, Y As Single)
ls
End Sub
Need more info
ASKER
Although it isn't using the winsock control, it still is an in depth detailed answer that gets the job done.
Thanks
Thanks
ASKER
One other question, is this tranferring the file as ascii or binary, because this is a very important part of the program, it needs to be transferred by ascii, or VB doesn't understand the carriage return line feed bytes in the file. THanks
ASKER