Announcement

Collapse
No announcement yet.

FTP Client für das Compact Framework

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

  • FTP Client für das Compact Framework

    Hallo,

    ich versuche eine Datei auf einen FTP Server zu laden und eine Datei wieder herunter zu laden.

    Dies funktioniert mit abstrichen in der B-Note auch, aber sobald diese Funktionen in einem separaten Thread ausgeführt werden friert die Anwendung ein.
    Wenn ich den Debug-Vorgang starte und nicht vor dem Datei Transfere die FTP Verbindung auf und wieder abbaue friert die Anwendung ebenfalls ein. (Das meine ich mit abstrichen in der B-Note)

    Selbst wenn in dem separaten Thread lediglich nur ein open, SetType und Close ausgeführt wird friert die Anwendung ein.
    Der Thread wird wie folgt gestartet:
    <pre>
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim thread As New Thread(AddressOf sample.Main)
    thread.Priority = ThreadPriority.BelowNormal

    sample.lbl = Label2
    sample.tb = TextBox1

    thread.Start()
    End Sub
    </pre>

    Wenn es irgendwo einen Code für diese Funktionen gibt der mit dem Compact Framework compatibel ist bin ich für einen Link dankbar.

    Ich habe auch schon fremden Code versucht und getestet, dieser friert jedoch leider auch ein.

    Oder wenn jemand in dem beigefügten Code einen Fehler entdekt würde ich mich über einen Hinweis freuen.

    Im Übrigen bin ich etwas verwundert, das der Emulator einfriert und man nicht wirklich den Fehler via debugger finden kann.

    Oder funktioniert eine FTP Verbindung beim Compact Framework 1 gar nicht und FTPWebRequest aus dem Compact Framework 2 ist sozusagen das BugFix?

    Vielen Dank schon mal für jede Antwort.

    Gruß
    PS

    VS2003
    Compact Framework 1.1
    Getestet/Debuged wird mit dem Emulator Pocket PC 2002
    Code C# oder VB ist mir egal es muß nur funktioniren.

    <pre>

    Private Sub ReadFromStream()

    sOut = m_comRead.ReadLine()
    m_Log.Text = m_Log.Text + sOut + sCRLF
    'mindestens einmal lesen

    While Not (m_comRead.Peek = -1)
    Try
    sOut = m_comRead.ReadLine()
    m_Log.Text = m_Log.Text + sOut + sCRLF
    Catch e As Exception

    End Try

    End While
    End Sub

    Public Sub WriteToStream(ByVal sCommand As String)
    m_commandStream.Write(System.Text.Encoding.ASCII.G etBytes((sCommand + "" & vbLf)), 0, (sCommand.Length + 1))
    ReadFromStream()
    End Sub

    Public Sub Open(ByVal sHost As String, ByVal iPort As Integer, ByVal sUsername As String, ByVal sPassword As String)
    m_tcpClient = New TcpClient(sHost, iPort)
    'm_tcpClient.ReceiveBufferSize = 4096; // alocate a 4kb buffer (for extra large MOTDs)
    m_commandStream = m_tcpClient.GetStream
    ' get the command stream
    m_comRead = New StreamReader(m_commandStream)
    ' now we can read the stream using the StreamReader.
    ReadFromStream()
    WriteToStream(("USER " + sUsername))
    ' send our user name
    WriteToStream(("PASS " + sPassword))
    ' send our password
    End Sub

    Public Sub Close()
    WriteToStream("QUIT")
    ' send our user name
    m_comRead.Close()
    m_tcpClient.Close()
    m_comRead = Nothing
    m_tcpClient = Nothing
    End Sub

    Private Function GetCurrentTransferPort() As Integer
    Dim ipThis As System.Net.IPHostEntry
    Dim port As Integer = 0
    Dim bIPFound As Boolean = False
    sOut = Nothing
    ' get a list of IP addresses for this machine
    ipThis = System.Net.Dns.GetHostByName(System.Net.Dns.GetHos tName)
    'Get the current server port
    WriteToStream("PASV")
    Dim sTemp() As String = sOut.Remove(0, (sOut.IndexOf("(", 0) + 1)).Replace(").", "").Split(New Char() {Microsoft.VisualBasic.ChrW(44)})
    ' we will try all IP addresses assigned to this machine
    ' the first one that the remote machine likes will be chosen
    Dim i As Integer = 0
    Do While (i < ipThis.AddressList.Length)
    Dim ip As String = ipThis.AddressList(i).ToString.Replace(".", ",")
    Dim p1 As Integer = Convert.ToByte(sTemp((sTemp.GetUpperBound(0) - 1)))
    'r.Next(100);
    Dim p2 As Integer = Convert.ToByte(sTemp(sTemp.GetUpperBound(0)))
    'r.Next(100);
    port = ((256 * p1) _
    + p2)
    Dim sPortCom As String = ("PORT " _
    + (ip + ("," _
    + (p1.ToString + ("," + p2.ToString)))))
    ' Port command now looks like PORT 61,45,6,34,xx,yy where
    ' first 4 values is your IP address and xx and yy are random numbers
    ' 256*xx+yy will be the port number where the remote machine will connect and receive
    WriteToStream(sPortCom)
    If (sOut.Substring(0, 3) = "200") Then
    ' PORT command accepted
    bIPFound = True
    'TODO: Warning!!! break;If
    End If
    i = (i + 1)
    Loop
    If bIPFound Then
    Return port
    End If
    Return 0
    End Function

    Public Sub SendFile(ByVal sLocalFilename As String, ByVal sRemoteFilename As String)
    Dim conn As TcpListener
    Dim bData() As Byte = New Byte((1024) - 1) {}
    Dim bytesRead As Integer = 0
    Dim xfer As Socket = Nothing
    ' create a file stream for the file to sent
    Dim fStream As FileStream = New FileStream(sLocalFilename, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, False)
    Dim port As Integer = GetCurrentTransferPort()
    ' now we are ready for transfer
    ' set up a listener and start listening
    conn = New TcpListener(port)
    conn.Start()
    ' issue the upload command
    WriteToStream(("STOR " + Path.GetFileName(sRemoteFilename)))
    ' start the upload
    xfer = conn.AcceptSocket
    bytesRead = fStream.Read(bData, 0, 1024)
    xfer.Send(bData, 0, bytesRead, SocketFlags.None)

    While (bytesRead > 0)
    bytesRead = fStream.Read(bData, 0, 1024)
    xfer.Send(bData, 0, bytesRead, SocketFlags.None)

    End While
    fStream.Close()
    xfer.Shutdown(SocketShutdown.Both)
    xfer.Close()
    conn.Stop()
    conn = Nothing
    xfer = Nothing
    fStream = Nothing
    ReadFromStream()
    ' consume the "226 Transfer Complete" response
    End Sub
    </pre>

  • #2
    Hallo,

    wen es interessiert:

    Habe Code gefunden der anscheinend auch in einem Thread läuft.

    http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=70834AE5-E4AC-40E7-A76C-61D8C81D0744

    Wo genau der Fehler liegt habe ich jetzt nicht geprüft, aber ich vermute in der Procedur ReadFromStream was auch das folgende Zitat aus simpleFTP vermuten läßt.

    Zitat:
    I found several source files but none seem to work quite right. Some were just bad code and some were just too long and complicated for my taste.
    ...
    Assuming the connections suceeded, you need to read the entire reply sent by the server. This is where most of the code I found on the Internet failed.

    mfg
    P

    Comment

    Working...
    X