Results 1 to 15 of 15

Thread: VB.Net client/server file transfer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    VB.Net client/server file transfer

    Hi I am using this as an guideline for my client/server: http://www.vbforums.com/showthread.php?t=502795

    I was wondering if there is an example on how to do a file transfer client to server and back?

    I did a few searches but couldn't find anything, could be my search tags weren't exact enough.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: VB.Net client/server file transfer

    I've never tried it but I think as you are already just sending and receiving Bytes using that method that is in that thread you linked to then all you would need to do is get the Byte representation of the file and just send that in the same way as anything else.
    So for example you could open a FileStream to the file you want to send and then just call the Read or ReadByte method on the FileStream to fill up a Byte array with the data from the file, then send that Byte array in the same way you would send any other message.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    Thanks for the help.

    Wondering if the File.ReadAllBytes() would work just as well?

    The way I am sending info to the server is sending some kind of identifier to tell what kind of message it is. Like "m|abc123" Would mean the client sent a chat message of abc123. How would I incorporate that into sending the file? Like.."f|bytes"
    That way the server would know everything after f| is part of a file?

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: VB.Net client/server file transfer

    yeah ReadAllBytes is probably a lot easier totally forgot that existed...
    and yeah thats how I would do it, just use an identifier at the start of each message, so your receiving server would split the string on the first instance of the | character and then process the message based on the characters found before that.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    I am unsure on how to add the string to the byte array.
    I have my readallbytes into the byte array, but how would I add say "f|" at the very beginning converting the string to byte format?

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: VB.Net client/server file transfer

    You can convert a string to bytes using System.Text.Encoding.ASCII.GetBytes (or something like that)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    Hm, I send the bytes and everything, but when I display the info the server is receiving it is always shown as: "System.Byte[]"

    I have tried the encoding getstring but it just displays System.Byte[] always. Any ideas on what I am doing wrong?

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: VB.Net client/server file transfer

    Are you sure you are using GetString correctly? Its a function so it will return the resulting string, not convert the original byte array (but I'm guessing you already know that). Can you post the relevant parts of your code here for us to take a look at?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    I am most likely not understanding on how to use something properly, here's exactly what I am doing:

    Client side I am sending a simple text file that says test 101 inside of it.

    vb Code:
    1. Dim b As Byte() = File.ReadAllBytes(txtFile.Text)
    2. sw.Write("f|")
    3. sw.Write(b)
    4. sw.Write("|" & EndChr & ControlChars.Cr)
    5. sw.Flush()

    Server side I get the data and do this just for testing:
    vb Code:
    1. Dim message As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesRead)
    2. MsgBox(message)
    It says
    Code:
    "f|System.Byte[]|~:
    The beginning and end of it is good just the system.byte[] I need to figure out what to do with it.
    Now if I dont send the f| and everything, just the byte array itself and when I receive it server side and do a file.writeallbytes it will write System.Byte[] to the file and not the actual data.

    Do I have to do some kind of conversion or something to the data that is displaying System.Byte[]? I'm doing something wrong or missing something, but unsure of what that is.

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: VB.Net client/server file transfer

    I'm assuming your "sw" object is of type StreamWriter? Change it to type NetworkStream (use the TcpClient.GetStream method to return a NetworkStream for the current connection) and then you can use the WriteByte method
    Last edited by chris128; Sep 2nd, 2009 at 05:58 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    Ok, I have got everything working and updated.

    Right now I am just using File.ReadAllBytes and sending it all instantly with a BinaryWriter.

    How would I go about determining the speed of the upload and how much is currently uploaded? Basically..a progress bar telling me how far along the upload is (if it is a larger file).

  12. #12
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: VB.Net client/server file transfer

    Quote Originally Posted by Disyne View Post
    Ok, I have got everything working and updated.

    Right now I am just using File.ReadAllBytes and sending it all instantly with a BinaryWriter.

    How would I go about determining the speed of the upload and how much is currently uploaded? Basically..a progress bar telling me how far along the upload is (if it is a larger file).
    In order for you to do this, I believe you must do something that I still think you should, and that is read and send portions of the file continuously.
    Create a buffer (array of bytes) of say 8192 bytes.
    Open a FileStream to the file you are intending to send, and then just do the following until you reach the end of the FileStream:

    1. Fill the buffer up as much as possible with data from the FileStream, possibly using its Read method. Note that the Read method also returns an integer telling you exactly how much bytes where actually read. It is not always so that the buffer will be completely filled.

    2. Write the bytes that was placed in the buffer, to the NetworkStream, using the BinaryWriter.

    3. Keep track of how much you've sent so far. Report progress if you'd like. Though it may be too frequent to report progress for each 8192 bytes sent, so you might want to consider making the buffer larger, or just keeping track so that you are not reporting progress every iteration.


    The reason I think you should've done something like this in the first place is that in case of really large files, you would'nt be hogging up memory by reading it all into a byte array, then sending it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    I have made a new class for the client side called FileUpload:
    vb Code:
    1. Imports System.IO
    2. Public Class FileUpload
    3.     Public PB As ProgressBar
    4.     Public FilePath As String
    5.     Public FileName As String
    6.     Private fs As FileStream
    7.     Private BYTES_TO_READ As Integer = 10000
    8.     Private buffer(BYTES_TO_READ) As Byte
    9.     Private totalRead As Integer = 0
    10.  
    11.     Public Sub FileUpload()
    12.         fs = New FileStream(FilePath, FileMode.Open)
    13.         If fs.CanRead Then
    14.             SendServer("UPLO", FileName, fs.Length)
    15.             Do Until totalRead = fs.Length
    16.                 totalRead += fs.Read(buffer, 0, BYTES_TO_READ)
    17.                 SendRaw(buffer)
    18.                 ProgressSet(totalRead / fs.Length * 100)
    19.             Loop
    20.         End If
    21.         fs.Close()
    22.     End Sub
    23.  
    24.     Public Delegate Sub ProgressSetInvoker(ByVal value As Integer)
    25.     Public Sub ProgressSet(ByVal value As Integer)
    26.         If PB.InvokeRequired Then
    27.             PB.Invoke(New ProgressSetInvoker(AddressOf ProgressSet), value)
    28.         Else
    29.             PB.Value = value
    30.         End If
    31.     End Sub
    32. End Class
    It (visually) seems to be working.

    Now server side, it is receiving my identifier command "UPLO" and the name and size of the file being sent. I then order the server to ReadBytes the amount of the filesize.

    d is the filename.
    d2 is the filesize in bytes.

    It is not not going past bw.ReadBytes(d2)
    I am unsure what I am doing wrong.

    vb Code:
    1. Case "UPLO"
    2. d = bw.ReadString()
    3. d2 = bw.ReadInt32()
    4. Dim bdata() As Byte = bw.ReadBytes(d2)
    5. File.WriteAllBytes(UsersDir & Username & "\" & d, bdata)


    Here are the SendRaw and SendServer subs:
    vb Code:
    1. Public Sub SendServer(ByVal command As String, ByVal ParamArray data() As String)
    2.         Dim bw As IO.BinaryWriter
    3.         Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(command)
    4.         Try
    5.             SyncLock client.GetStream
    6.                 bw = New IO.BinaryWriter(client.GetStream)
    7.                 bw.Write(b)
    8.                 For i As Integer = 0 To UBound(data)
    9.                     bw.Write(data(i))
    10.                 Next
    11.                 bw.Flush()
    12.             End SyncLock
    13.         Catch ex As Exception
    14.             MessageBox.Show(ex.ToString)
    15.         End Try
    16.     End Sub
    17.  
    18.     Public Sub SendRaw(ByVal data() As Byte)
    19.         Dim bw As IO.BinaryWriter
    20.         Try
    21.             SyncLock client.GetStream
    22.                 bw = New IO.BinaryWriter(client.GetStream)
    23.                 bw.Write(data)
    24.                 bw.Flush()
    25.             End SyncLock
    26.         Catch ex As Exception
    27.             MessageBox.Show(ex.ToString)
    28.         End Try
    29.     End Sub
    Last edited by Disyne; Sep 4th, 2009 at 02:34 PM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    I have had some progress it seems.

    This is the server side part that handles the upload from the client:

    d = filename
    d2 = file size in bytes

    vb Code:
    1. d = bw.ReadString()
    2.                         d2 = bw.ReadString()
    3.                         Dim read As Integer = 0
    4.                         Dim totalRead As Integer = 0
    5.                         Dim bdata(10000) As Byte
    6.                         Dim fs As FileStream = File.Create(UsersDir & Username & "\" & d)
    7.                         Do Until totalRead = d2
    8.                             read = Me.mClient.GetStream.Read(bdata, 0, 10000)
    9.                             totalRead += read
    10.                             fs.Write(bdata, 0, read)
    11.                         Loop

    This is the client side part that handles the upload:
    vb Code:
    1. Public Sub FileUpload()
    2.         fs = New FileStream(FilePath, FileMode.Open)
    3.         If fs.CanRead Then
    4.             SendServer("UPLO", FileName, fs.Length.ToString)
    5.             Do Until totalRead = fs.Length
    6.                 read = fs.Read(buffer, 0, BYTES_TO_READ)
    7.                 totalRead += read
    8.                 ReDim sendBuffer(read - 1)
    9.                 Array.Copy(buffer, sendBuffer, read - 1)
    10.                 SendRaw(sendBuffer)
    11.                 ProgressSet(totalRead / fs.Length * 100)
    12.             Loop
    13.         End If
    14.         fs.Close()
    15.     End Sub
    The server receives the total amount of bytes that it should, but say if I upload an image. The server will receive it and make the file and the size will be the same on the client side, but the image will be partially correct other parts will be discolored or distorted.

    Now if I sent a .txt file the server will receive all the text inside of it, but the last character will be replaced with a space.

    So say I sent a .txt file with the following inside : "test 123"
    The server would write to the .txt "test 12 "
    Always a space as the last character.
    Last edited by Disyne; Sep 5th, 2009 at 01:27 PM.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: VB.Net client/server file transfer

    bump

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