Results 1 to 8 of 8

Thread: Share, send and receive files over network

  1. #1

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    Share, send and receive files over network

    Hi all

    I'm working on a network chat for my office I want to add a way for users to share files or be able to send and receive file. Du's anyone have sum sample code or any suggestions for this.

    Thanks
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Share, send and receive files over network

    How are you currently doing the chatting? You'd probably do essentially the same thing for the files. You'd likely send a message or a header to tell the receiver that a file was coming rather than a message and then just send a Byte array created from the file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    Re: Share, send and receive files over network

    Thanks for the reply I will look in to suing the Byte array.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  4. #4
    Junior Member
    Join Date
    Feb 2008
    Posts
    22

    Smile Re: Share, send and receive files over network

    <<Server>>


    Imports System.Net.Sockets
    Public Class serverForm

    Private listen As Threading.Thread

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    listen = New Threading.Thread(AddressOf listener)
    listen.Start()
    End Sub

    Private Sub serverForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    listen.Abort()
    End Sub



    Sub listener()
    Dim l As New TcpListener(Net.IPAddress.Parse("192.168.1.64"), 8765)
    l.Start()
    Do
    If l.Pending Then l.BeginAcceptTcpClient(AddressOf accepting, l)
    Threading.Thread.Sleep(1000)
    Loop
    End Sub

    Sub accepting(ByVal ar As IAsyncResult)
    'receives filename, filelength, filebytes
    Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
    Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
    Dim filename, filepath As String, filelen As Long
    'using binaryreader (wrapped networkstream) to read a String and a Long
    Dim br As New IO.BinaryReader(clientSocket.GetStream)
    filename = br.ReadString
    filelen = br.ReadInt64
    'filepath = IO.Path.Combine(Application.StartupPath, filename)

    filepath = IO.Path.Combine("C:\Documents and Settings\PATKA\Desktop\", filename)
    Dim buffer(8092) As Byte, readstotal As Long = 0
    Dim reads As Integer = -1
    'using filestream to write read filebytes directly from networkstream
    Using fs As New IO.FileStream(filepath, IO.FileMode.Create, IO.FileAccess.Write)
    Do Until readstotal = filelen
    reads = clientSocket.GetStream.Read(buffer, 0, buffer.Length)
    fs.Write(buffer, 0, reads)
    readstotal += reads
    Loop
    End Using
    MsgBox("received: " & filename)
    br.Close()
    clientSocket.Close()
    End Sub
    End Class

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    <<client>>


    Imports system.net.sockets
    Public Class clientForm

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    Dim d As New sending(AddressOf sends)
    d.BeginInvoke(OpenFileDialog1.FileName, Nothing, Nothing)
    End Sub

    Private Delegate Sub sending(ByVal filename As String)

    Private Sub sends(ByVal fullfilename As String)
    'sends filename, filelength, filebytes
    Dim info As New IO.FileInfo(fullfilename)
    Dim tcp As New TcpClient
    tcp.Connect("217.42.144.230", 80)
    'writes a String and a Long with binarywriter (wrapping networkstream)
    Dim bw As New IO.BinaryWriter(tcp.GetStream)
    bw.Write(info.Name)
    bw.Write(info.Length)
    'using filestream to read file, writes this directly to networkstream
    Using fs As New IO.FileStream(fullfilename, IO.FileMode.Open, IO.FileAccess.Read)
    Dim buffer(8092) As Byte, reads As Integer = -1
    Do Until reads = 0
    reads = fs.Read(buffer, 0, buffer.Length)
    tcp.GetStream.Write(buffer, 0, reads)
    Loop
    End Using
    bw.Close()
    tcp.Close()
    End Sub


    End Class




    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    This will do what u want!!!!!!!!!!1

  5. #5

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    Re: Share, send and receive files over network

    Thanks thats what i was looking for.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  6. #6
    Member
    Join Date
    Feb 2007
    Posts
    48

    Re: Share, send and receive files over network

    Is anyone able to point me in the right direction of how to calculate the transfer speed with this code?

  7. #7
    New Member
    Join Date
    Mar 2010
    Posts
    1

    Re: Share, send and receive files over network

    hey guys, thanks for this code. I've been searching for whole 3 days for an effective code for file transfer... I've also done a bit of modification on the code where i can transfer a larger file by modifying the buffer size depending on the size of the file...

  8. #8
    New Member
    Join Date
    May 2012
    Posts
    2

    Re: Share, send and receive files over network

    Hi all,

    I didn't understand if we create 2 projects. 1 for server and 1 for client. I tried but it I did something wrong. Could you send me an example of solution?

    Thanks
    Michael

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width