Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
got_r00t?

Transferência de ficheiros

Recommended Posts

Dotinho

já exprimentas-te :

  Dim destino As String

        My.Computer.Network.DownloadFile(file, destino)

        'ou então

        Dim wc As New Net.WebClient

        wc.DownloadFile(New Uri(file), destino)


Luis Lourenço

Share this post


Link to post
Share on other sites
got_r00t?

já exprimentas-te :

  Dim destino As String

        My.Computer.Network.DownloadFile(file, destino)

        'ou então

        Dim wc As New Net.WebClient

        wc.DownloadFile(New Uri(file), destino)

com esse código e cm se estivesse a fazer download/upload de um ficheiro para um server.

O meu objectivo e transferir ficheiros por TCP.

Ja consegui fazer a parte do client, penso que esta correcta.

        Dim myfile As String = ""
        Dim client = New TcpClient("127.0.0.1", 8000)
        Dim writer As New StreamWriter(client.GetStream())
        Dim filePath As String = TextBox1.Text
        Dim filestream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
        Dim reader As New IO.BinaryReader(filestream)
        Dim byteRead As Byte
        For i As Integer = 0 To reader.BaseStream.Length() - 1
            byteRead = reader.ReadByte
            writer.Write(byteRead)
        Next
        reader.Close()
        filestream.Close()
        writer.Flush()
        MsgBox("Fim")

Mas não estou a conseguir construir o ficheiro...

If Listener.Pending = True Then
            Dim filePath As String = TextBox1.Text
            Dim filestream As New IO.FileStream(filePath, IO.FileMode.CreateNew, IO.FileAccess.Write)
            Dim client As TcpClient = Listener.AcceptTcpClient()
            Dim writer As NetworkStream = client.GetStream()
            Dim mstream As New System.IO.MemoryStream
            Dim ByteArray(0) As Byte
            Dim i As Int32
            i = writer.Read(ByteArray, 0, ByteArray.Length)
            While (i <> 0)
               writer.Read(ByteArray, 0, 1)
                mstream.Write(ByteArray, 0, 1)
                mstream.WriteTo(filestream)
                i = writer.Read(ByteArray, 0, ByteArray.Length)
            End While
            mstream.Flush()
            MsgBox("Fim")
        End If

Alguma dica?

Share this post


Link to post
Share on other sites
got_r00t?

Obrigado, mas já vi os links quase todos do google, no entanto nenhum me mostra um exemplo de código simples, de como fazer uma transferência por TCP.

Bem, depois de muitas experiências, completei o meu código...

No entanto tenho um problema, era suposto que ele transferi-se qualquer ficheiro de qualquer tamanho, mas sem razão aparente, quando esta a enviar os pacotes, se o ficheiro for grande ele simplesmente crasha... Já fiz step into, e não da erro, não da nada, simplesmente para...

Deixo aqui o código para experimentarem.

Dim portNumber As Integer = 8123
            Dim localAddr As String = txt_ip.Text
            Dim Client As New TcpClient(localAddr, portNumber)
            Dim CnnStream As NetworkStream = Client.GetStream
            Dim FilePath As String = txt_filepath.Text
            Dim File As New IO.FileStream(FilePath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim FileStream As New IO.BinaryReader(File)
            Dim BufferSize As Integer = 1024
            Dim Buffer(BufferSize - 1) As Byte
            Dim FileBuffer As Integer
            While (1)
                FileBuffer = FileStream.Read(Buffer, 0, Buffer.Length)
                If FileBuffer = 0 Then Exit While
                CnnStream.Write(Buffer, 0, FileBuffer)
            End While
            CnnStream.Close()
            FileStream.Close()
            File.Close()

Dim Client As TcpClient = Listener.AcceptTcpClient
                Dim CnnStream As NetworkStream = Client.GetStream
                Dim FilePath As String = txt_filepath.Text
                Dim File As New IO.FileStream(FilePath, IO.FileMode.Create, IO.FileAccess.Write)
                Dim FileStream As New IO.BinaryWriter(File)
                Dim BufferSize As Integer = 1024
                Dim Buffer(BufferSize - 1) As Byte
                Dim FileBuffer As Integer
                While CnnStream.DataAvailable = True
                    FileBuffer = CnnStream.Read(Buffer, 0, Buffer.Length)
                    FileStream.Write(Buffer, 0, FileBuffer)
                End While
                Listener.Stop()
                CnnStream.Close()
                FileStream.Close()
                File.Close()
                MsgBox("Done")

Penso que o problema esta mesmo no envio dos pacotes, e não ao reconstruir o ficheiro...

Vejam se conseguem qqq

Obrigado

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.