home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / swrench / tinyftp.bas < prev    next >
BASIC Source File  |  1995-07-24  |  7KB  |  269 lines

  1. Option Explicit
  2.  
  3. Function FTPCommand (CtlData As String) As Integer
  4.     On Error Resume Next
  5.     
  6.     CtlData = CtlData & Chr$(13) & Chr$(10)
  7.     Client.Socket1.SendLen = Len(CtlData)
  8.     Client.Socket1.SendData = CtlData
  9.  
  10.     If Err <> 0 Then
  11.         FTPCommand = False
  12.     Else
  13.         FTPCommand = True
  14.     End If
  15.  
  16. End Function
  17.  
  18. Function FTPConnect (HostName As String)
  19.     Dim CtlData As String, Reply As Integer
  20.  
  21.     FTPConnect = False
  22.     If HostName = "" Then Exit Function
  23.  
  24.     Client.Socket1.AddressFamily = AF_INET
  25.     Client.Socket1.Protocol = IPPROTO_IP
  26.     Client.Socket1.Type = SOCK_STREAM
  27.     Client.Socket1.RemotePort = IPPORT_FTP
  28.     Client.Socket1.HostName = HostName
  29.     Client.Socket1.Binary = False
  30.     Client.Socket1.BufferSize = 1024
  31.     Client.Socket1.Blocking = True
  32.  
  33.     On Error Resume Next
  34.     Client.Socket1.Action = SOCKET_CONNECT
  35.     If Err Then
  36.         MsgBox Error$
  37.         Exit Function
  38.     End If
  39.  
  40.     Reply = FTPResult(CtlData)
  41.     
  42.     If Reply = 220 Then
  43.         FTPConnect = True
  44.     Else
  45.         Client.Socket1.Action = SOCKET_CLOSE
  46.     End If
  47.  
  48. End Function
  49.  
  50. Sub FTPGetDirectory ()
  51.     Dim CtlData As String
  52.     
  53.     If Not FTPCommand("PWD") Then Exit Sub
  54.     If FTPResult(CtlData) <> 257 Then Exit Sub
  55.  
  56.     CtlData = Mid$(CtlData, 2, InStr(CtlData, " ") - 3)
  57.     Client.RemotePath.Caption = CtlData
  58. End Sub
  59.  
  60. Function FTPGetFile (RemoteFile As String, LocalFile As String)
  61.     Dim CtlData As String, Buffer As String
  62.     Dim Result As Integer
  63.  
  64.     FTPGetFile = False
  65.  
  66.     If RemoteFile = "" Or LocalFile = "" Then Exit Function
  67.     If Not FTPListen() Then Exit Function
  68.     If Not FTPCommand("RETR " & RemoteFile) Then Exit Function
  69.     
  70.     If FTPResult(CtlData) <> 150 Then
  71.         Client.Socket2.Action = SOCKET_CLOSE
  72.         Exit Function
  73.     End If
  74.  
  75.     Client.Socket2.Action = SOCKET_ACCEPT
  76.     On Error Resume Next
  77.     
  78.     Open LocalFile For Binary As #1
  79.     If Err Then
  80.         MsgBox Error$
  81.         Client.Socket2.Action = SOCKET_CLOSE
  82.         Exit Function
  83.     End If
  84.  
  85.     FTPGetFile = True
  86.  
  87.     Do
  88.         Client.Socket2.RecvLen = 4096
  89.         Buffer = Client.Socket2.RecvData
  90.         If Err Then
  91.             FTPGetFile = False
  92.             MsgBox Error$
  93.             Exit Do
  94.         End If
  95.         If Client.Socket2.RecvLen = 0 Then Exit Do
  96.         Put #1, , Buffer
  97.         DoEvents
  98.     Loop
  99.  
  100.     Close #1
  101.     Client.Socket2.Action = SOCKET_CLOSE
  102.     Result = FTPResult(CtlData)
  103. End Function
  104.  
  105. Function FTPListen ()
  106.     Dim Port As Integer, Address As String
  107.     Dim Reply As Integer, CtlData As String
  108.     Dim I As Integer, P As Integer
  109.  
  110.     FTPListen = False
  111.     
  112.     Client.Socket2.AddressFamily = AF_INET
  113.     Client.Socket2.Binary = True
  114.     Client.Socket2.Blocking = True
  115.     Client.Socket2.BufferSize = 0
  116.     Client.Socket2.HostAddress = INADDR_ANY
  117.     Client.Socket2.LocalPort = IPPORT_ANY
  118.     Client.Socket2.Protocol = IPPROTO_TCP
  119.     Client.Socket2.Timeout = 0
  120.     Client.Socket2.Type = SOCK_STREAM
  121.     Client.Socket2.Action = SOCKET_LISTEN
  122.  
  123.     Port = Client.Socket2.LocalPort
  124.     Address = Client.Socket2.LocalAddress
  125.  
  126.     For I = 1 To 3
  127.         P = InStr(Address, ".")
  128.         If P <> 0 Then Mid$(Address$, P, 1) = ","
  129.     Next I
  130.     
  131.     CtlData = "PORT " & Address & "," & (Port \ 256) & "," & (Port Mod 256)
  132.     
  133.     If Not FTPCommand(CtlData) Then GoTo OpenFailed
  134.     If FTPResult(CtlData) <> 200 Then GoTo OpenFailed
  135.     
  136.     If Client.BinaryTransfer.Value = 1 Then
  137.         CtlData = "TYPE I"
  138.     Else
  139.         CtlData = "TYPE A"
  140.     End If
  141.     
  142.     If Not FTPCommand(CtlData) Then GoTo OpenFailed
  143.     If FTPResult(CtlData) <> 200 Then GoTo OpenFailed
  144.     
  145.     FTPListen = True
  146.     Exit Function
  147.  
  148. OpenFailed:
  149.     If Client.Socket2.Listening Then Client.Socket2.Action = SOCKET_CLOSE
  150.     Exit Function
  151. End Function
  152.  
  153. Function FTPLogin (Username As String, Password As String) As Integer
  154.     Dim CtlData As String, Reply As Integer
  155.     Dim Counter As Integer
  156.     
  157.     FTPLogin = False
  158.  
  159.     If Client.Socket1.IsReadable Then
  160.         Reply = FTPResult(CtlData)
  161.     End If
  162.  
  163.     While Reply = 220 And Client.Socket1.IsReadable
  164.         Reply = FTPResult(CtlData)
  165.     Wend
  166.  
  167.     CtlData = "USER " & Username
  168.     If Not FTPCommand(CtlData) Then Exit Function
  169.     Reply = FTPResult(CtlData)
  170.  
  171.     If Reply = 331 Then
  172.         CtlData = "PASS " & Password
  173.         If Not FTPCommand(CtlData) Then Exit Function
  174.         Reply = FTPResult(CtlData)
  175.     End If
  176.     
  177.     While Reply = 230 And Client.Socket1.IsReadable
  178.         Reply = FTPResult(CtlData)
  179.     Wend
  180.  
  181.     If Reply = 230 Then
  182.         FTPLogin = True
  183.     Else
  184.         MsgBox "Invalid user name or password"
  185.     End If
  186.  
  187. End Function
  188.  
  189. Function FTPPutFile (LocalFile As String, RemoteFile As String)
  190.     Dim CtlData As String, Buffer As String * 4096
  191.     Dim Result As Integer, Size As Long
  192.  
  193.     FTPPutFile = False
  194.  
  195.     If RemoteFile = "" Or LocalFile = "" Then Exit Function
  196.     If Not FTPListen() Then Exit Function
  197.     If Not FTPCommand("STOR " & RemoteFile) Then Exit Function
  198.     
  199.     If FTPResult(CtlData) <> 150 Then
  200.         Client.Socket2.Action = SOCKET_ABORT
  201.         Exit Function
  202.     End If
  203.  
  204.     Client.Socket2.Action = SOCKET_ACCEPT
  205.     On Error Resume Next
  206.     
  207.     Size = FileLen(LocalFile)
  208.     If Err Then
  209.         Client.Socket2.Action = SOCKET_CLOSE
  210.         MsgBox Error$
  211.         Exit Function
  212.     End If
  213.     
  214.     Open LocalFile For Binary As #1
  215.  
  216.     If Err Then
  217.         Client.Socket2.Action = SOCKET_CLOSE
  218.         MsgBox Error$
  219.         Exit Function
  220.     End If
  221.  
  222.     FTPPutFile = True
  223.  
  224.     Do
  225.         Get #1, , Buffer
  226.         If Size < Len(Buffer) Then
  227.             Client.Socket2.SendLen = Size
  228.             Size = 0
  229.         Else
  230.             Client.Socket2.SendLen = Len(Buffer)
  231.             Size = Size - Len(Buffer)
  232.         End If
  233.         Client.Socket2.SendData = Buffer
  234.         If Err > 0 Then
  235.             FTPPutFile = False
  236.             MsgBox Error$
  237.             Exit Do
  238.         End If
  239.         If Size = 0 Then Exit Do
  240.         DoEvents
  241.     Loop
  242.  
  243.     Close #1
  244.     Client.Socket2.Action = SOCKET_CLOSE
  245.     Result = FTPResult(CtlData)
  246. End Function
  247.  
  248. Function FTPResult (CtlData As String) As Integer
  249.     Dim SockData As String, Reply As Integer
  250.  
  251.     Client.Socket1.RecvLen = 255
  252.     SockData = Client.Socket1.RecvData
  253.     Debug.Print SockData
  254.  
  255.     Reply = Val(Left$(SockData, 3))
  256.     If Mid$(SockData, 4, 1) = "-" Then
  257.         Do
  258.             Client.Socket1.RecvLen = 255
  259.             SockData = Client.Socket1.RecvData
  260.             If Val(Left$(SockData, 3)) = Reply Then Exit Do
  261.             Debug.Print SockData
  262.         Loop
  263.     End If
  264.     CtlData = Right$(SockData, Len(SockData) - InStr(SockData, " "))
  265.     
  266.     FTPResult = Reply
  267. End Function
  268.  
  269.