Announcement

Collapse
No announcement yet.

TCP hanging problem...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP hanging problem...

    Hello...

    Could maybe a couple of you try this example code out and let me know what happens on your machine. I cant get this code to run without hanging.

    Code:
    #compile exe
    #register none
    #include "win32api.inc"
    #include "wsock32.inc"
    
    function pbmain() as long
      dim message as string
      dim buffer as string
      dim hsock as long
    
    
    rem Open socket to PowerBasic FTP
      hsock = freefile
      tcp open port 21 at "ftp.powerbasic.com" as hsock timeout 500
    
      if err then
        msgbox "Unable to connect to FTP.POWERBASIC.COM"
        exit function
      else
        msgbox "We connected ok..." & $CRLF & _
               "but now let's see if we can get" & $CRLF & _
               "a response from the server."
      end if
    
    
    rem Read welcome message
      message = ""
    
      do
        tcp line hsock, buffer    rem program hangs up here in debugger
        message = (message & $CRLF & buffer)
      loop while len(buffer)
    
      msgbox message
    
    
    rem Send username and retreive message
      tcp print hsock, "user anonymous"
    
      message = ""
      do
        tcp line hsock, buffer
        message = (message & $CRLF & buffer)
      loop while len(buffer)
    
      msgbox message
    
    rem Send password and retreive message
      tcp print hsock, "pass [email protected]"
    
      message = ""
      do
        tcp line hsock, buffer
        message = (message & $CRLF & buffer)
      loop while len(buffer)
    
      msgbox message & format$(len(buffer))
    
    rem Close socket and bail out
      tcp close hsock
    end function
    Cheers!

  • #2
    The code is running, but it is waiting for a second "line" of text which will never be received as the code is written... try this slight variation (you'll still need to add additional error testing, but this modification works fine for me):
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #INCLUDE "win32api.inc"
    #INCLUDE "wsock32.inc"
    
    FUNCTION PBMAIN() AS LONG
      DIM message AS STRING
      DIM hsock AS LONG
    
    REM Open socket to PowerBasic FTP
      hsock = FREEFILE
      TCP OPEN PORT 21 AT "ftp.powerbasic.com" AS hsock TIMEOUT 5000
      IF ERR THEN
        MSGBOX "Unable to connect to FTP.POWERBASIC.COM"
        EXIT FUNCTION
      ELSE
        MSGBOX "We connected ok..." & $CRLF & _
               "but now let's see if we can get" & $CRLF & _
               "a response from the server."
      END IF
     
    REM Read welcome message
      TCP LINE hsock, Message    
      MSGBOX message
     
    REM Send username and retreive message
      TCP PRINT hsock, "user anonymous"
      TCP LINE hsock, Message
      MSGBOX message
     
    REM Send password and retreive message
      TCP PRINT hsock, "pass [email protected]"
      TCP LINE hsock, Message
      MSGBOX message
     
    REM Close socket and bail out
      TCP CLOSE hsock
      MSGBOX "Finished!"
    END FUNCTION
    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Hello Lance!

      I understand exactly what you mean here but this only works if the FTP server returns one line messages. The PowerBasic FTP server as well as many others return more than one line. I have also found a wierd quirk, Some FTP servers send an EOF after the first line has been written, even though there are a couple more lines to read in after that...

      The Welcome message seems to be the real problem area for this.

      Cheers!

      ------------------

      Comment


      • #4
        Mark --
        try this
        Code:
           #Compile Exe
           #Register None
           #Dim All
           #Include "win32api.inc"
           #Include "wsock32.inc"
        
           Global hsock As Long
           
           Global MessageF As String
           Sub DMessage (SendMsg As String)
              Dim Message As String
              MessageF = ""
              If SendMsg <> "" Then Tcp Print hsock, SendMsg Else SendMsg = "Welcome"
              Do
                 Tcp Line hsock, Message
                 MessageF = MessageF + Message + $CRLF
              Loop Until Mid$(Message, 4, 1) = " "  Or Len(Message) = 3
              MsgBox MessageF, , SendMsg
           End Sub
        
           Function PbMain() As Long
              hsock = FreeFile
              Tcp Open Port 21 At "ftp.powerbasic.com" As hsock TIMEOUT 5000
              If Err Then
                 MsgBox "Unable to connect"
                 Exit Function
              End If
              DMessage ""
              Do
                 Select Case Val(Mid$(MessageF, 1, 3))
                    Case 220 ' User
                       DMessage "user anonymous"
                    Case 331 ' password
                       DMessage "pass [email protected]"
                    Case 230 ' logged in, now possible to continue
                       MsgBox "Finished": Exit Do
                    Case Else
                       MsgBox "Unexpected reply": Exit Do
                 End Select
              Loop
              Tcp Close hsock
           End Function
        But should say, that RFC for FTP is enough difficult.
        If you seriously want to work with FTP, you should search Don Dickinson's PB library (www.basicguru.com) - at least, I saw it some monthes ago, or to use commercial Marshall Software DLL (search by Marshall and you will find http).

        ------------------

        Comment

        Working...
        X