Results 1 to 8 of 8

Thread: [2005] Send File over sockets (Binary to byte array?)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    [2005] Send File over sockets (Binary to byte array?)

    I'm writing an updater code for my application so my server will send a binary (executable) file to the client.


    How would I convert the binary file into a byte array, or whatever the easiest solution would be to send a file across the internet.

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

    Re: [2005] Send File over sockets (Binary to byte array?)

    You'd read it into a byte array using the IO.BinaryReader.
    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)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: [2005] Send File over sockets (Binary to byte array?)

    I tried with the following code:

    Server:
    vb Code:
    1. Dim fileContents As Byte()
    2.         fileContents = My.Computer.FileSystem.ReadAllBytes("C:\TEST.txt")
    3.         Dim str As String
    4.         Dim enc As New System.Text.ASCIIEncoding()
    5.         str = enc.GetString(fileContents)

    Client:
    vb Code:
    1. Dim fStream As New IO.FileStream("C:\Copy.txt", IO.FileMode.CreateNew)
    2.  
    3.                 Dim bw As New IO.BinaryWriter(fStream)
    4.                 Dim b() As Byte
    5.                 Dim utf As New System.Text.UTF7Encoding()
    6.                 b = utf.GetBytes(byteData)
    7.                 Dim i As Integer
    8.                 For i = 0 To b.Length - 1
    9.                     b(i) = CByte(i)
    10.                 Next i
    11.                 bw.Write(b)
    12.                 bw.Close()
    13.                 fStream.Close()

    byteData is the string that is sent from the server to client, it contains all of the file info
    Last edited by tylerm; Jan 28th, 2008 at 06:23 PM.

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

    Re: [2005] Send File over sockets (Binary to byte array?)

    It seems like you're reading the file into a byte array, then you create a string from it, and send the string. Correct? You should send the byte array directly to the networkstream, and read it directly into a byte array.
    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)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: [2005] Send File over sockets (Binary to byte array?)

    Well I'm using "Winsock_Orcas" instead of the Sockets included with .Net

    Basically, I'm sending this command/string to the client:
    _wsks.Item(_usr.wskGUID).Send("Client_Update§" & str)

    Since different files are being sent for different reasons, I wanted to have a way to determine which file was being sent.

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

    Re: [2005] Send File over sockets (Binary to byte array?)

    If you had been using the .Net classes, all you've needed to do is to use a BinaryWriter and write the bytes that the BinaryReader returned into the networkstream.
    Now you will have to do in a most unefficient way, that is send each byte as a string. The classes in the System.Text namespace arent what you need, as they convert the byte values to the corresponding character using the ASCII table.
    If you're sending an array of {30,0,1,200,64}, you might want to construct the string something like this: "FileDescriptionHere§30,0,1,200,64"

    Altough I would (as usual) recommend you to use the .Net socket classes.
    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)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: [2005] Send File over sockets (Binary to byte array?)

    So how would I get each byte (1,0,30,64,200) from a specific file?

    Then when I do send this array how would I convert the string back into bytes?

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

    Re: [2005] Send File over sockets (Binary to byte array?)

    Seeing that you already know how to read them into an array, simply iterate through the array and append each value to a string, with a ',' between each.

    Upon receiving the string, you split it on each ',' giving you an array of strings, containing the byte values. You will need to use the Array.ConvertAll method to convert the string array to a byte array, then write the byte array to a new empty file using the BinaryWriter.

    As you can see this is quite an unefficient and lengthy procedure, especially as string manipulation is relatively slow.
    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)

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