home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / fssock20 / ftpdemo.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-20  |  28.6 KB  |  827 lines

  1. VERSION 2.00
  2. Begin Form FtpDemo 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "FtpDemo"
  5.    ClientHeight    =   7815
  6.    ClientLeft      =   1200
  7.    ClientTop       =   1605
  8.    ClientWidth     =   8145
  9.    Height          =   8220
  10.    Left            =   1140
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   7815
  15.    ScaleWidth      =   8145
  16.    Top             =   1260
  17.    Width           =   8265
  18.    Begin Frame Frame6 
  19.       Caption         =   "Transfer Type"
  20.       Height          =   615
  21.       Left            =   2400
  22.       TabIndex        =   21
  23.       Top             =   4680
  24.       Width           =   3375
  25.       Begin OptionButton Binary_Option 
  26.          Caption         =   "BINARY"
  27.          Height          =   255
  28.          Left            =   2025
  29.          TabIndex        =   23
  30.          Top             =   240
  31.          Width           =   1095
  32.       End
  33.       Begin OptionButton ASCII_Option 
  34.          Caption         =   "ASCII"
  35.          Height          =   255
  36.          Left            =   480
  37.          TabIndex        =   22
  38.          Top             =   240
  39.          Value           =   -1  'True
  40.          Width           =   855
  41.       End
  42.    End
  43.    Begin Frame Frame5 
  44.       Caption         =   "Login Information"
  45.       Height          =   2535
  46.       Left            =   2400
  47.       TabIndex        =   17
  48.       Top             =   2160
  49.       Width           =   3375
  50.       Begin FSSocket FtpXfrData 
  51.          Connect         =   0   'False
  52.          EOL             =   ""
  53.          HostAddress     =   ""
  54.          HostName        =   ""
  55.          InputBufferSize =   8192
  56.          Left            =   2880
  57.          Listen          =   0   'False
  58.          ListenPort      =   0
  59.          OutputBufferSize=   8192
  60.          PortNumber      =   0
  61.          Protocol        =   0  'TCP
  62.          ServiceName     =   ""
  63.          Top             =   1200
  64.       End
  65.       Begin Timer Logout_Timer 
  66.          Left            =   2280
  67.          Top             =   240
  68.       End
  69.       Begin FSSocket FtpCommand 
  70.          Connect         =   0   'False
  71.          EOL             =   ""
  72.          HostAddress     =   ""
  73.          HostName        =   ""
  74.          InputBufferSize =   8192
  75.          Left            =   2880
  76.          Listen          =   0   'False
  77.          ListenPort      =   0
  78.          OutputBufferSize=   8192
  79.          PortNumber      =   0
  80.          Protocol        =   0  'TCP
  81.          ServiceName     =   ""
  82.          Top             =   240
  83.       End
  84.       Begin FSSocket FtpDirData 
  85.          Connect         =   0   'False
  86.          EOL             =   ""
  87.          HostAddress     =   ""
  88.          HostName        =   ""
  89.          InputBufferSize =   8192
  90.          Left            =   2880
  91.          Listen          =   0   'False
  92.          ListenPort      =   0
  93.          OutputBufferSize=   8192
  94.          PortNumber      =   0
  95.          Protocol        =   0  'TCP
  96.          ServiceName     =   ""
  97.          Top             =   720
  98.       End
  99.       Begin TextBox Hostname 
  100.          Height          =   285
  101.          Left            =   120
  102.          TabIndex        =   1
  103.          Top             =   480
  104.          Width           =   3135
  105.       End
  106.       Begin TextBox Userid 
  107.          Height          =   285
  108.          Left            =   120
  109.          TabIndex        =   2
  110.          Top             =   1080
  111.          Width           =   3135
  112.       End
  113.       Begin TextBox Password 
  114.          Height          =   285
  115.          Left            =   120
  116.          PasswordChar    =   "*"
  117.          TabIndex        =   3
  118.          Top             =   1680
  119.          Width           =   3135
  120.       End
  121.       Begin CommandButton ConnectButton 
  122.          Caption         =   "Connect"
  123.          Height          =   375
  124.          Left            =   120
  125.          TabIndex        =   5
  126.          Top             =   2040
  127.          Width           =   1455
  128.       End
  129.       Begin CommandButton ListRefreshButton 
  130.          Caption         =   "List Directory"
  131.          Enabled         =   0   'False
  132.          Height          =   375
  133.          Left            =   1800
  134.          TabIndex        =   4
  135.          Top             =   2040
  136.          Width           =   1455
  137.       End
  138.       Begin Label Label1 
  139.          AutoSize        =   -1  'True
  140.          Caption         =   "Host Name"
  141.          Height          =   195
  142.          Left            =   120
  143.          TabIndex        =   20
  144.          Top             =   240
  145.          Width           =   945
  146.       End
  147.       Begin Label Label2 
  148.          AutoSize        =   -1  'True
  149.          Caption         =   "Userid"
  150.          Height          =   195
  151.          Left            =   120
  152.          TabIndex        =   19
  153.          Top             =   840
  154.          Width           =   555
  155.       End
  156.       Begin Label Label3 
  157.          AutoSize        =   -1  'True
  158.          Caption         =   "Password"
  159.          Height          =   195
  160.          Left            =   120
  161.          TabIndex        =   18
  162.          Top             =   1440
  163.          Width           =   825
  164.       End
  165.    End
  166.    Begin Frame Frame4 
  167.       Caption         =   "Commands"
  168.       Height          =   2415
  169.       Left            =   120
  170.       TabIndex        =   15
  171.       Top             =   5280
  172.       Width           =   7935
  173.       Begin TextBox CommandWindow 
  174.          Height          =   2055
  175.          Left            =   120
  176.          MultiLine       =   -1  'True
  177.          ScrollBars      =   3  'Both
  178.          TabIndex        =   16
  179.          TabStop         =   0   'False
  180.          Top             =   240
  181.          Width           =   7695
  182.       End
  183.    End
  184.    Begin Frame Frame3 
  185.       Caption         =   "Remote System"
  186.       Height          =   5175
  187.       Left            =   5880
  188.       TabIndex        =   13
  189.       Top             =   120
  190.       Width           =   2175
  191.       Begin ComboBox Remote_Current_Directory 
  192.          Height          =   300
  193.          Left            =   120
  194.          TabIndex        =   11
  195.          Top             =   240
  196.          Width           =   1935
  197.       End
  198.       Begin ListBox Remote_Directory 
  199.          Height          =   1590
  200.          Left            =   120
  201.          Sorted          =   -1  'True
  202.          TabIndex        =   7
  203.          Top             =   600
  204.          Width           =   1935
  205.       End
  206.       Begin ListBox Remote_File_List 
  207.          Height          =   2760
  208.          Left            =   120
  209.          Sorted          =   -1  'True
  210.          TabIndex        =   6
  211.          Top             =   2280
  212.          Width           =   1935
  213.       End
  214.    End
  215.    Begin Frame Frame2 
  216.       Caption         =   "Local System"
  217.       Height          =   5175
  218.       Left            =   120
  219.       TabIndex        =   12
  220.       Top             =   120
  221.       Width           =   2175
  222.       Begin DirListBox Local_Directory 
  223.          Height          =   1380
  224.          Left            =   120
  225.          TabIndex        =   9
  226.          Top             =   600
  227.          Width           =   1935
  228.       End
  229.       Begin FileListBox Local_File_List 
  230.          Height          =   2955
  231.          Left            =   120
  232.          TabIndex        =   8
  233.          Top             =   2040
  234.          Width           =   1935
  235.       End
  236.       Begin DriveListBox Local_Drive 
  237.          Height          =   315
  238.          Left            =   120
  239.          TabIndex        =   10
  240.          Top             =   240
  241.          Width           =   1935
  242.       End
  243.    End
  244.    Begin Frame Frame1 
  245.       Caption         =   "FTP Demo"
  246.       Height          =   1935
  247.       Left            =   2400
  248.       TabIndex        =   0
  249.       Top             =   120
  250.       Width           =   3375
  251.       Begin TextBox Text1 
  252.          BackColor       =   &H00C0C0C0&
  253.          BorderStyle     =   0  'None
  254.          Height          =   1575
  255.          Left            =   120
  256.          MultiLine       =   -1  'True
  257.          TabIndex        =   14
  258.          TabStop         =   0   'False
  259.          Top             =   240
  260.          Width           =   3135
  261.       End
  262.    End
  263. Option Explicit
  264. Dim listport As String          'address and port for directory listing
  265. Dim xfrport As String           'address and port for data transfers
  266. Dim reply As String             'field to host command replies from the server
  267. Dim download_file As Integer    'file number for downloading files
  268. Dim upload_file As Integer      'file number for uploading files
  269. Dim ready_to_send As Integer    'is the socket ready to send?
  270. Dim connected As Integer        'are we connected?
  271. Dim client_mode As String       'Current client transfer mode 'a' or 'i'
  272. Dim server_mode As String       'Current server transfer mode
  273. Private Sub ASCII_Option_Click ()
  274. Rem Set file transfer type to "ascii"
  275.     client_mode = "a"
  276. End Sub
  277. Private Sub Binary_Option_Click ()
  278. Rem Set file transfer type to "image"
  279.     client_mode = "i"
  280. End Sub
  281. Private Sub ChangeCurrentDirectory (directory As String)
  282. Dim firstquote As Integer
  283. Dim secondquote As Integer
  284. Dim index As Integer
  285. Dim newdir As String
  286. Rem If there is a new directory to goto, send a "cwd" command to the server
  287.     If directory <> "" Then
  288.         Send_Command ("cwd " & directory)
  289.     End If
  290. Rem Send a "pwd" command to get the current directory on the server.
  291.     Send_Command ("pwd")
  292. Rem Reply looks like '2xx "dir/dir/dir" text message'
  293. Rem Get the data between the quotes.
  294.     firstquote = InStr(1, reply, Chr$(34)) + 1
  295.     secondquote = InStr(firstquote, reply, Chr$(34))
  296.     newdir = Mid$(reply, firstquote, secondquote - firstquote)
  297. Rem Search the Remote_Current_Directory combo box for a match.
  298.     For index = 0 To Remote_Current_Directory.ListCount - 1
  299.         If newdir = Remote_Current_Directory.List(index) Then
  300.             Remote_Current_Directory.Text = Remote_Current_Directory.List(index)
  301.         End If
  302.     Next index
  303. Rem If no match add it to the Remote_Current_Directory combo box and make
  304. Rem it the current selection.
  305.     If Remote_Current_Directory.Text <> newdir Then
  306.         Remote_Current_Directory.AddItem newdir
  307.         Remote_Current_Directory.Text = Remote_Current_Directory.List(Remote_Current_Directory.NewIndex)
  308.     End If
  309. End Sub
  310. Private Sub ConnectButton_Click ()
  311.     If ConnectButton.Caption = "Connect" Then
  312.         On Error GoTo nohost:   ' catch bad host names
  313. Rem Set the default mode to ASCII
  314.         ASCII_Option.Value = True
  315.         client_mode = "a"
  316.         server_mode = "a"
  317.         
  318. Rem Set the hostname.
  319.         ftpcommand.HostName = HostName.Text
  320. Rem set the protocol.
  321.         ftpcommand.ServiceName = "ftp"
  322. Rem Clear the reply string.
  323.         reply = ""
  324.         
  325. Rem Show that we are connecting
  326.         ConnectButton.Caption = "Working"
  327. Rem Connect to the server.
  328.         ftpcommand.Connect = True
  329.         
  330. Rem Wait for the server to connect and send us a message
  331.         While reply = ""
  332.             DoEvents
  333.         Wend
  334.         
  335. Rem Some replies have a "-" in col 4, This means there is more to come
  336. Rem so skip over them.
  337.         While Mid$(reply, 4, 1) = "-"
  338.             DoEvents
  339.         Wend
  340. Rem Make the list button active.
  341.         ListRefreshButton.Enabled = True
  342.            
  343. Rem Send the userid.
  344.         Send_Command ("user " & userid.Text)
  345.         
  346. Rem Replys that begin with "5xx" are errors.
  347.         If Left$(reply, 1) = "5" Then
  348.             MsgBox reply
  349. Rem Login failed, so tell the server we are done.
  350.             Send_Command ("quit")
  351.             Exit Sub
  352.         End If
  353. Rem Send the password.
  354.         Send_Command ("pass " & password.Text)
  355.         If Left$(reply, 1) = "5" Then
  356.             MsgBox reply
  357.             Send_Command ("quit")
  358.             Exit Sub
  359.         End If
  360. Rem Call ChangeCurrentDirectory to update the Remote_Current_Directory combo box.
  361.         ChangeCurrentDirectory ("")
  362.         
  363. Rem List the remote directory.
  364.         ListCurrentDirectory
  365.         
  366. Rem Set the default xfer type to "ascii"
  367.         
  368.         Send_Command ("type a")
  369.     Else
  370. Rem Send the "quit" command to the server.
  371.         Send_Command ("quit")
  372. Rem Sometimes we may be out of sync with the server and it will
  373. Rem not respond to the "quit". So we set a timer for 5 seconds.
  374. Rem The timer will close all connections from the client side.
  375.         Logout_Timer.Interval = 5000
  376.     End If
  377.     Exit Sub
  378. nohost:
  379. Rem Bad host name, so tell the user.
  380.     If Err = 20102 Then
  381.         MsgBox ftpcommand.WSALastErrorMsg
  382.     Else
  383.         MsgBox Error
  384.     End If
  385.     Exit Sub
  386. End Sub
  387. Private Sub Form_Load ()
  388.     FtpDemo.Caption = FtpDemo.Caption & " - " & ftpcommand.WSADescription
  389. Rem We want to break up the responses from the server into lines.
  390.     FtpDirData.EOL = Chr$(13) & Chr$(10)
  391.     ftpcommand.EOL = Chr$(13) & Chr$(10)
  392.     text1.Text = "This is not meant to be a complete FTP program."
  393.     text1.Text = text1.Text & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10) & "It's purpose is to show some of the basic logic required to create one."
  394.     text1.Text = text1.Text & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10) & "For more information on FTP you should read RFC959.TXT"
  395. End Sub
  396. Private Sub FtpCommand_Connected (StatusCode As Integer, Description As String)
  397. Rem We have connected to a server, make the connect button into a
  398. Rem disconnect button.
  399.     ConnectButton.Caption = "Disconnect"
  400.     connected = True
  401. End Sub
  402. Private Sub FtpCommand_DataReceived (Text As String, EOL As Integer)
  403. Rem When this event is fired, the data received in the Text string
  404. Rem may not be complete. If EOL is not True, only a partial line
  405. Rem of data has been received. We store this incomplete data in a
  406. Rem static string called "line".  Only when EOL is True can we
  407. Rem proccess the data.
  408.     Static data_line As String
  409.     data_line = data_line & Text
  410.     If EOL = True Then
  411. Rem Show the data received in the command window
  412.         CommandWindow.SelStart = Len(CommandWindow.Text)
  413.         CommandWindow.SelText = data_line & Chr$(13) & Chr$(10)
  414.         reply = data_line
  415.         data_line = ""
  416.     End If
  417. End Sub
  418. Private Sub FtpCommand_Disconnected (StatusCode As Integer, Description As String)
  419. Rem If we are attempting a "new" connection, display the reason for the failure
  420. Rem to connect.
  421.     If ConnectButton.Caption = "Working" Then
  422.         MsgBox Description
  423.     End If
  424. Rem Mark us disconnected
  425.     connected = False
  426. Rem Stop listening for data
  427.     FtpDirData.Listen = False
  428.     FtpXfrData.Listen = False
  429. Rem Disable the list button
  430.     ListRefreshButton.Enabled = False
  431. Rem clear the server directory info
  432.     Remote_Directory.Clear
  433.     Remote_Directory.AddItem ".."
  434.     Remote_File_List.Clear
  435.     Remote_Current_Directory.Clear
  436. Rem Stop the timer, it one was runing.
  437.     Logout_Timer.Interval = 0
  438. Rem Make this a connect button
  439.     ConnectButton.Caption = "Connect"
  440. End Sub
  441. Private Sub ftpdirdata_DataReceived (Text As String, EOL As Integer)
  442. Rem When this event is fired, the data received in the Text string
  443. Rem may not be complete. If EOL is not True, only a partial line
  444. Rem of data has been received. We store this incomplete data in a
  445. Rem static string called "data_line".  Only when EOL is True can we
  446. Rem proccess the data.
  447.     Dim a As Integer            ' field count
  448.     Dim pos As Integer
  449.     Dim entry_name As String          ' the entry name
  450.     Dim firstchar As String     ' the first character of the directory entry
  451.     Static data_line As String       ' where we build the directory entry
  452. Rem Add the new data to the line we are working on
  453.     data_line = data_line & Text
  454.     If EOL = True Then
  455. Rem We now have a complete line of directory information sent from the
  456. Rem server.
  457. Rem On some systems, the server will return extra lines of data
  458. Rem such as the count of files.
  459. Rem If it not an entry, just ignore it.
  460.         firstchar = Mid$(data_line, 1, 1)
  461.         If firstchar <> "d" And firstchar <> "l" And firstchar <> "-" Then
  462.             data_line = ""
  463.             Exit Sub
  464.         End If
  465.         
  466. Rem Find the entry name, in the 9th field
  467.         pos = 1
  468.         For a = 1 To 8
  469.             pos = InStr(pos, data_line, " ")
  470.             'skip multiple blanks
  471.             While Mid$(data_line, pos, 1) = " "
  472.                 pos = pos + 1
  473.             Wend
  474.             entry_name = Mid$(data_line, pos)
  475.         Next a
  476.         
  477.             
  478. Rem Look at the type for this entry and add it to the correct list.
  479.         Select Case firstchar
  480.         Case "d"
  481. Rem It is a directory.
  482. Rem Only add real directory names, not all servers send the ".."
  483. Rem entry, so we took care of that elsewhere.
  484.             If entry_name <> ".." Or entry_name <> "." Then
  485.                 Remote_Directory.AddItem entry_name
  486.             End If
  487.         Case "l"
  488. Rem It is a link, remove the link info before saving.
  489.             Remote_Directory.AddItem Left$(entry_name, InStr(1, entry_name, " ->") - 1)
  490.         Case "-"
  491. Rem It is a file.
  492.             Remote_File_List.AddItem entry_name
  493.         End Select
  494.         
  495. Rem Done with this line, clear it.
  496.         data_line = ""
  497.     End If
  498. End Sub
  499. Private Sub FtpDirData_Disconnected (StatusCode As Integer, Description As String)
  500. Rem Close the listen connection.
  501. Rem We need to do this because the server keeps the old port
  502. Rem open for a while.
  503.     FtpDirData.Listen = False
  504. End Sub
  505. Private Sub FtpXfrData_Connected (StatusCode As Integer, Description As String)
  506.     Dim buffer As String
  507.     Dim file_length As Long
  508.     Dim curpos As Long
  509.     Dim buflen As Integer
  510. Rem If we are not uploading a file, do nothing
  511.     If upload_file = 0 Then
  512.         Exit Sub
  513.     End If
  514. Rem Set up to catch failed sends (10035 WSAWOULDBLOCK)
  515.     On Error GoTo delay:
  516. Rem Get the length of the file and our current possition in the file
  517.     file_length = LOF(upload_file)
  518.     curpos = Seek(upload_file)
  519. Rem set our send size to 1/2 the Output buffer size.
  520.     buflen = FtpXfrData.OutputBufferSize / 2
  521. Rem while there is more data to send
  522.     Do While curpos <= file_length
  523. Rem     Don't go past the end of the file
  524.         If curpos + buflen > file_length Then
  525.             buflen = file_length - curpos + 1
  526.         End If
  527. Rem     Read in buflen bytes from the file
  528.         buffer = String$(buflen, " ")
  529.         Get #upload_file, , buffer
  530. Rem     Send the buffer
  531.         FtpXfrData.Send = buffer
  532. Rem     We can't go into a tight loop sending data because FSSocket
  533. Rem     may need some cpu cycles to actually send the data.
  534.         DoEvents
  535. Rem     Update our current possition in the file
  536.         curpos = Seek(upload_file)
  537.     Loop
  538. Rem Close the connection
  539.     FtpXfrData.Connect = False
  540.     Close #upload_file
  541.     upload_file = 0
  542.     Exit Sub
  543. delay:
  544. Rem Was the error generated by FSSocket
  545.     If Err = 20102 Then
  546. Rem     Yes, was it "Operation would block"
  547.         If FtpXfrData.WSALastError = 10035 Then
  548. Rem         Yes, wait for the ready_to send event
  549.             ready_to_send = False
  550.             While ready_to_send = False
  551.                 DoEvents
  552.             Wend
  553.             Resume
  554.         Else
  555. Rem Some other error
  556.             MsgBox FtpXfrData.WSALastErrorMsg
  557.             Exit Sub
  558.         End If
  559.     Else
  560.         MsgBox Error
  561.         Exit Sub
  562.     End If
  563.     Resume
  564. End Sub
  565. Private Sub FtpXfrData_DataReceived (Text As String, EOL As Integer)
  566. Rem A data block has arrived from the server, add it to the local file
  567.     Put #download_file, , Text
  568. End Sub
  569. Private Sub FtpXfrData_Disconnected (StatusCode As Integer, Description As String)
  570. Rem The data transfer has completed, close the port and the file.
  571.     FtpXfrData.Listen = False
  572. Rem If we are downloading a file, close it
  573.     If download_file <> 0 Then
  574.         Close #download_file
  575.         download_file = 0
  576.     End If
  577. Rem Make sure the local file window is up to date.
  578.     Local_File_List.Refresh
  579. End Sub
  580. Private Sub FtpXfrData_ReadyToSend ()
  581.     ready_to_send = True
  582. End Sub
  583. Private Sub ListCurrentDirectory ()
  584. Rem Clear the current Directory and File lists.
  585.     Remote_Directory.Clear
  586.     Remote_File_List.Clear
  587. Rem Add a fake "parrent" directory entry.
  588.     Remote_Directory.AddItem ".."
  589.     If server_mode <> "a" Then
  590.         Send_Command ("type a")
  591.         server_mode = "a"
  592.     End If
  593. Rem Start listening for a connection from the server.
  594.     OpenListPort
  595.         
  596. Rem Tell the server what port we are listening on.
  597.     Send_Command ("port " & listport)
  598. Rem Tell the server to list the current directory.
  599.     Send_Command ("list")
  600. End Sub
  601. Private Sub ListRefreshButton_Click ()
  602. Rem Rescan the current directory on the server
  603.     ListCurrentDirectory
  604. End Sub
  605. Private Sub Local_Directory_Change ()
  606.     Local_File_List.Path = Local_Directory.Path
  607.     ChDir Local_Directory.Path
  608. End Sub
  609. Private Sub Local_Drive_Change ()
  610.     Local_Directory.Path = Local_Drive.Drive    ' Set directory path.
  611.     ChDrive Local_Drive.Drive
  612. End Sub
  613. Private Sub Local_File_List_DblClick ()
  614. Rem Request for an upload
  615. Rem Open a file to save the data into
  616.     open_upload_file (Local_File_List.List(Local_File_List.ListIndex))
  617. Rem Start listening for a connection from the server
  618.     OpenXfrPort
  619.     If client_mode <> server_mode Then
  620.         Send_Command ("type " & client_mode)
  621.         server_mode = client_mode
  622.     End If
  623. Rem Tell the server what port we are listening on.
  624.     Send_Command ("port " & xfrport)
  625. Rem Tell the sever we are sending a file.
  626.     Send_Command ("stor " & Local_File_List.List(Local_File_List.ListIndex))
  627.     If Left$(reply, 1) = "5" Then
  628.             MsgBox reply
  629.             FtpXfrData.Listen = False
  630.             Exit Sub
  631.         End If
  632. Rem Refresh the remote directory list
  633.     ListCurrentDirectory
  634. End Sub
  635. Private Sub Logout_Timer_Timer ()
  636. Rem This timer will only fire if the server does not respond
  637. Rem to a "quit" command in 5 seconds.
  638. Rem Close all open connections
  639.     ftpcommand.Connect = False
  640.     FtpXfrData.Connect = False
  641.     FtpDirData.Connect = False
  642. Rem There may be a "send_command" call waiting for a response
  643. Rem so lets give it one.
  644.     reply = "221 Goodbye."
  645. Rem Turn off the timer, we have disconnected.
  646.     Logout_Timer.Interval = 0
  647. End Sub
  648. Private Sub open_download_file (filename As String)
  649. Rem open a file to receive the data
  650.     Dim Path As String
  651. Rem If we are at the root, don't add an unneeded "\"
  652.     If Right$(Local_Directory.Path, 1) = "\" Then
  653.         Path = Local_Directory.Path & filename
  654.     Else
  655.         Path = Local_Directory.Path & "\" & filename
  656.     End If
  657. Rem Get a new file number
  658.     download_file = FreeFile
  659. Rem delete the receiving file if it exists
  660.     On Error GoTo badpath
  661.     Kill Path
  662.     On Error GoTo 0
  663. Rem Open the file.
  664. Rem The file is binary because the sender will do the needed
  665. Rem CR/LF translation if its an "ascii" file.
  666.     Open Path For Binary Access Write As #download_file
  667.     Exit Sub
  668. badpath:
  669.     If Err <> 53 Then
  670.         MsgBox Error$ & Err
  671.     End If
  672.     Resume Next
  673. End Sub
  674. Private Sub open_upload_file (filename As String)
  675. Rem open a file to receive the data
  676.     Dim Path As String
  677. Rem If we are at the root, don't add an unneeded "\"
  678.     If Right$(Local_Directory.Path, 1) = "\" Then
  679.         Path = Local_Directory.Path & filename
  680.     Else
  681.         Path = Local_Directory.Path & "\" & filename
  682.     End If
  683. Rem Get a new file number
  684.     upload_file = FreeFile
  685. Rem Open the file.
  686. Rem The file is binary because the sender will do the needed
  687. Rem CR/LF translation if its an "ascii" file.
  688.     Open Path For Binary Access Read As #upload_file
  689. End Sub
  690. Private Sub OpenListPort ()
  691. Dim i As Integer
  692. Dim X As Integer
  693. Dim address As String
  694. Dim port As Integer
  695. Rem Let TCP/IP select a port number.
  696.     FtpDirData.ListenPort = 0
  697. Rem Start listening.
  698.     FtpDirData.Listen = True
  699. Rem Save the portnumber that TCP/IP assigned.
  700.     port = FtpDirData.ListenPort
  701. Rem At this point we need to get our local address so that
  702. Rem we can tell the server where to send the data.
  703. Rem Unfortunatly the FipDirData.LocalAddress property may have
  704. Rem an incorrect value at this point. The local IP address
  705. Rem cannot be determined until a connection has been made.
  706. Rem The system my have more than one IP address and TCP/IP
  707. Rem does not know witch one to use.
  708. Rem But we do have an open connection to the server for commands
  709. Rem and we can use it's LocalAddress property because it is known
  710. Rem to be good.
  711.     address = ftpcommand.LocalAddress
  712. Rem format the address and port for the ftp "port" command.
  713.     For i = 1 To 3
  714.         X = InStr(address, ".")
  715.         If X <> 0 Then Mid$(address, X, 1) = ","
  716.     Next i
  717.     listport = address & "," & port \ 256 & "," & port Mod 256
  718.      
  719. End Sub
  720. Private Sub OpenXfrPort ()
  721. Dim i As Integer
  722. Dim X As Integer
  723. Dim address As String
  724. Dim port As Integer
  725. Rem Let TCP/IP select a port number.
  726.     FtpXfrData.ListenPort = 0
  727. Rem Start listening.
  728.     FtpXfrData.Listen = True
  729. Rem Save the portnumber that TCP/IP assigned.
  730.     port = FtpXfrData.ListenPort
  731. Rem At this point we need to get our local address so that
  732. Rem we can tell the server where to send the data.
  733. Rem Unfortunatly the FipDirData.LocalAddress property may have
  734. Rem an incorrect value at this point. The local IP address
  735. Rem cannot be determined until a connection has been made.
  736. Rem The system my have more than one IP address and TCP/IP
  737. Rem does not know witch one to use.
  738. Rem But we do have an open connection to the server for commands
  739. Rem and we can use it's LocalAddress property because it is known
  740. Rem to be good.
  741.     address = ftpcommand.LocalAddress
  742. Rem format the address and port for the ftp "port" command.
  743.     For i = 1 To 3
  744.         X = InStr(address, ".")
  745.         If X <> 0 Then Mid$(address, X, 1) = ","
  746.     Next i
  747.     xfrport = address & "," & port \ 256 & "," & port Mod 256
  748. End Sub
  749. Private Sub Remote_Current_Directory_Click ()
  750. Rem An entry in the combo box has been selected.
  751.     ChangeCurrentDirectory (Remote_Current_Directory.Text)
  752.     ListCurrentDirectory
  753. End Sub
  754. Sub Remote_Current_Directory_KeyPress (KeyAscii As Integer)
  755. Rem User typed in a directory.
  756.     If KeyAscii = 13 Then
  757.         ChangeCurrentDirectory (Remote_Current_Directory.Text)
  758.         ListCurrentDirectory
  759.     End If
  760. End Sub
  761. Private Sub Remote_Directory_DblClick ()
  762. Rem Change the current directory on the server.
  763.     ChangeCurrentDirectory (Remote_Directory.List(Remote_Directory.ListIndex))
  764. Rem Get a new listing of the directory.
  765.     ListCurrentDirectory
  766. End Sub
  767. Private Sub Remote_File_List_DblClick ()
  768. Rem Request for a download
  769. Rem Open a file to save the data into
  770.     open_download_file (Remote_File_List.List(Remote_File_List.ListIndex))
  771.     If client_mode <> server_mode Then
  772.         Send_Command ("type " & client_mode)
  773.         server_mode = client_mode
  774.     End If
  775. Rem Start listening for a connection from the server
  776.     OpenXfrPort
  777. Rem Tell the server what port we are listening on.
  778.     Send_Command ("port " & xfrport)
  779. Rem Tell the sever to transfer the file to us.
  780.     Send_Command ("retr " & Remote_File_List.List(Remote_File_List.ListIndex))
  781. End Sub
  782. Private Sub Send_Command (command_line As String)
  783. Rem Send a command to the server and wait for a response
  784.     Static busy As Integer
  785.     If busy Then
  786.         Exit Sub
  787.     End If
  788.     If Not connected Then
  789.         Exit Sub
  790.     End If
  791.     busy = True
  792.     On Error GoTo errx:
  793. Rem Change the pointer to an hourglass
  794.     FtpDemo.MousePointer = 11
  795. Rem Add the command to the commandwindow
  796.     CommandWindow.SelStart = Len(CommandWindow.Text)
  797.     CommandWindow.SelText = command_line & Chr$(13) & Chr$(10)
  798. Rem Empty the reply string
  799.     reply = ""
  800. Rem Send the command.
  801.     ftpcommand.Send = command_line & Chr$(10)
  802. Rem Wait for a reply, ftpcommand.Data_received event will fill in the reply.
  803.     While reply = ""
  804.         DoEvents
  805.     Wend
  806. Rem Replies that begin with "1" are progress reports, ignore them
  807.     While Mid$(reply, 1, 1) = "1"
  808.         DoEvents
  809.     Wend
  810. Rem Some replies have a "-" in col 4, This means there is more to come
  811. Rem so skip over them
  812.     While Mid$(reply, 4, 1) = "-"
  813.         DoEvents
  814.     Wend
  815. Rem We have a response, return the pointer to normal.
  816.     FtpDemo.MousePointer = 0
  817.     busy = False
  818.     Exit Sub
  819. errx:
  820.     If Err = 20102 Then
  821.         MsgBox ftpcommand.WSALastErrorMsg
  822.     Else
  823.         MsgBox Error
  824.     End If
  825.     Resume Next
  826. End Sub
  827.