# Network File Transfer vb.net



## shuggans (Dec 27, 2011)

Hello all.  I'm developing an application that automatically sends files from a machine running the client app to a server running the server app.  The files in question are very large files, (500mb - 2GB in size) and I was wondering what is the best way to transfer them qucikly and efficiently over the network.  Should I utilize FTP transfer, or should I develop a TCP transfer client/server?

If TCP, can anybody give me a basic run down of the networkstream class used with the tcplistener? Thanks guys.


----------



## Deleted member 3 (Dec 27, 2011)

Assuming the network means LAN I'd simply use the File.Copy method on a network share.


----------



## shuggans (Dec 27, 2011)

Network shares are out of the question for my purposes


----------



## Deleted member 3 (Dec 27, 2011)

Since you have to run an application on both the server and the client. You could simply install a simple http/ftp server and use webclient from the server to download the files.


----------



## FordGT90Concept (Dec 27, 2011)

FTP would be the easiest.  There's open source libraries for FTP on virtually every language.  LAN/network share would be ideal though.


----------



## shuggans (Dec 27, 2011)

I need to use a file transfer solution in conjunction with the filesystemwatcher class on the client.  The client app will need to wait until the file is done growing in size before sending(or can I send bytes as they are added to a file?)


----------



## Deleted member 3 (Dec 27, 2011)

Quick google: http://kenegozi.com/blog/2008/06/28/ftp-upload-in--dot-net-you-aint-need-no-libraries


----------



## shuggans (Dec 27, 2011)

I need to use a file transfer solution in conjunction with the filesystemwatcher class on the client. The client app will need to wait until the file is done growing in size before sending(or can I send bytes as they are added to a file?)

I found this code:


```
Imports System.Net.Sockets
Imports System.Text
Module Module1
    Sub Main()
        Dim serverSocket As New TcpListener(8888)
        Dim requestCount As Integer
        Dim clientSocket As TcpClient
        serverSocket.Start()
        msg("Server Started")
        clientSocket = serverSocket.AcceptTcpClient()
        msg("Accept connection from client")
        requestCount = 0

        While (True)
            Try
                requestCount = requestCount + 1
                Dim networkStream As NetworkStream = _
                        clientSocket.GetStream()
                Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                Dim dataFromClient As String = _
                        System.Text.Encoding.ASCII.GetString(bytesFrom)
                dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                msg("Data from client -  " + dataFromClient)
                Dim serverResponse As String = _
                    "Server response " + Convert.ToString(requestCount)
                Dim sendBytes As [Byte]() = _
                    Encoding.ASCII.GetBytes(serverResponse)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                networkStream.Flush()
                msg(serverResponse)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End While


        clientSocket.Close()
        serverSocket.Stop()
        msg("exit")
        Console.ReadLine()
    End Sub

    Sub msg(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(" >> " + mesg)
    End Sub
End Module
```

But it looks like it's jsut used to send text data.  what do i need to do to stream receive a file instead of text?


----------



## Deleted member 3 (Dec 27, 2011)

I would recommend uploading via FTP. Writing just a client is a lot easier than writing both server and client. Besides, tcplistener is deprecated, it might create new headaches later on.


----------



## shuggans (Dec 27, 2011)

I have a server app already I want this to integrate with, writing the tcplistener peice wouldn't be any more of a headache than I've gone through already .  Is FTP as fast as writing a client/server transfer?


----------



## Deleted member 3 (Dec 27, 2011)

Depending on how crappy the code you end up with is it might be faster. Expect full bandwidth usage from FTP. Perhaps you can just use ftp.exe in your application, at least as a place holder. Takes 5min to write that way.


----------



## FordGT90Concept (Dec 27, 2011)

shuggans said:


> I need to use a file transfer solution in conjunction with the filesystemwatcher class on the client.  The client app will need to wait until the file is done growing in size before sending(or can I send bytes as they are added to a file?)


That can be done separately.  Just put in a Timer that checks the filesize periodically and once it hasn't changed for so long, then invoke FTP or File.Copy.

It doesn't matter what solution you use, you really have to wait because it is good practice to start the file transfer with the file size.  That way it will be abundantly obvious on the reciving end if the transfer is incomplete/failed.


----------

