-
I've been doing a lot of research on the Winsock Control, and began making my FTP program.. But i'm stuck on sending a LIST command to the FTP.. I'll put
Winsock1.SendData "LIST" & vbCrLf
and it'll send it, but i'll get a response that it could not open a data connection, yet I can connect to the ftp using CuteFTP. Here're my questions: How exactly do I go about using the LIST command and how can I retrieve the data so that the files it lists appear in a listbox control. Thank you so much for whoever can help me.
-
As the Error told you, you need to create a Data Connection to the FTP as well as your inital connection,
this Data Connection is used for transfering files and retreiving the results of
LIST and other commands.
Here's some example code I put together a long time ago,
I've ran through it and it's still good, it demonstrates (in this case) how to retrieve a list of files from the FTP server:
Code:
'----------------------------------------------------
' frmFTP (frmFTP.frm)
'
' Winsock Control Example of Basic FTP Operation
'
'----------------------------------------------------
' Written By: Aaron Young
' Last Modified: 10/01/1999
'
' Add 2 Winsock Controls (wskFTP and wskDC)
' Add 2 Multiline Textboxes (txtData and txtDCData)
' Add 2 Command Buttons (cmdConnect and cmdDisconnect)
'
Option Explicit
Private Sub cmdConnect_Click()
'Connect to the FTP site
With wskFTP
If .State <> sckClosed Then .Close
.LocalPort = 0
.RemoteHost = "ftp.server.com"
.RemotePort = 21
.Connect
End With
End Sub
Private Sub cmdDisconnect_Click()
'Close the FTP & Data Connections
If wskFTP.State <> sckClosed Then wskFTP.Close
If wskDC.State <> sckClosed Then wskDC.Close
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Make sure the Winsocks are Disconnected
cmdDisconnect_Click
End Sub
Private Sub wskFTP_Connect()
'Once Connected Login, Open a Data Connection and send a Command
wskFTP.SendData "USER usernamehere" & vbCrLf
wskFTP.SendData "PASS passwordhere" & vbCrLf
wskFTP.SendData "SYST" & vbCrLf
If OpenDataConnection(True) Then
'Successfully Opened a Data Connection to -
'the Server Execute Data Command..
wskFTP.SendData "LIST" & vbCrLf
End If
End Sub
Private Sub wskFTP_DataArrival(ByVal bytesTotal As Long)
'Recieve FTP Messages
Dim sNewData As String
wskFTP.GetData sNewData, vbString, bytesTotal
txtData.SelText = sNewData
End Sub
Private Sub wskDC_ConnectionRequest(ByVal requestID As Long)
'Data Connection Winsock Waits For FTP Server to try and Connect
Dim sData As String
wskDC.Close
wskDC.Accept requestID
End Sub
Private Sub wskDC_DataArrival(ByVal bytesTotal As Long)
'Recieve Data from the Data Connection, i.e. Lists, etc..
Dim sNewData As String
Dim iFile As Integer
wskDC.GetData sNewData, vbByte, bytesTotal
txtDCData.SelText = sNewData
End Sub
Private Function OpenDataConnection(Optional ByVal bBinary As Boolean = False) As Boolean
'Open a Data Connection to the FTP Server -
'Used for Send/Recieving Files and List Commands, etc..
On Error GoTo Error_Exit
While wskDC.State <> sckClosed
wskDC.Close
Wend
If wskDC.State = sckClosed Then
'Setting the Port to Zero Before Listening -
'Selects an Available Port Automatically.
wskDC.LocalPort = 0
wskDC.Listen
'Set the Transfer Type to Either -
'ASCII(A) or Binary(I)
wskFTP.SendData "TYPE " & IIf(bBinary, "I", "A") & vbCrLf
'Calculate the PORT Hi and Lo Numbers and Tell the FTP Connection where to send data from Server commands
wskFTP.SendData "PORT " & Replace(wskDC.LocalIP, ".", ",") & "," & Val("&H" & Left(Right("0000" & Hex(wskDC.LocalPort), 4), 2)) & "," & (wskDC.LocalPort And &HFF) & vbCrLf
End If
OpenDataConnection = True
Error_Exit:
End Function
-
no success, still same error using your program : "425 Can't open data connection for LIST." I tried using the wsc_ftp_client.zip that if ound in some vb archives, but that *sometimes* gives me an error. I DO connect to this site on CuteFTP and I don't get the error, so I don't see why this should be happening. I have all the commands right, so that's not it. Anyone else?
-
I've actually been working on a similar project and the only way I could get the directory contents was to re-open the listening socket every time I tried to request the directory, send the PORT command, use a DoEvents and then request the list.... Sock is connected on port 21 and lSock is listening on port 0. Good luck.
Private Sub Command2_Click()
Dim strData As String
If lSock.State = sckClosed Then lSock.listen
strData = "PORT " & Replace(lSock.LocalIP, ".", ",") & "," & Val("&H" & Left(Right("0000" & Hex(lSock.LocalPort), 4), 2)) & "," & (lSock.LocalPort And &HFF) & vbCrLf
Debug.Print strData
'MsgBox lSock.LocalPort
frmMain.Sock.SendData strData
DoEvents
SendData txtSend.Text & vbCrLf, 1
End Sub
Private Sub lSock_ConnectionRequest(ByVal requestID As Long)
lSock.Close
lSock.accept requestID
End Sub
Private Sub lSock_DataArrival(ByVal bytesTotal As Long)
Dim lData As String
lSock.GetData lData, vbString
Debug.Print lData
lSock.Close
End Sub