|
-
Jan 28th, 2008, 04:32 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 28th, 2008, 04:34 PM
#2
Re: [2005] Send File over sockets (Binary to byte array?)
You'd read it into a byte array using the IO.BinaryReader.
-
Jan 28th, 2008, 06:17 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] Send File over sockets (Binary to byte array?)
I tried with the following code:
Server:
vb Code:
Dim fileContents As Byte()
fileContents = My.Computer.FileSystem.ReadAllBytes("C:\TEST.txt")
Dim str As String
Dim enc As New System.Text.ASCIIEncoding()
str = enc.GetString(fileContents)
Client:
vb Code:
Dim fStream As New IO.FileStream("C:\Copy.txt", IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fStream)
Dim b() As Byte
Dim utf As New System.Text.UTF7Encoding()
b = utf.GetBytes(byteData)
Dim i As Integer
For i = 0 To b.Length - 1
b(i) = CByte(i)
Next i
bw.Write(b)
bw.Close()
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.
-
Jan 28th, 2008, 06:24 PM
#4
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.
-
Jan 28th, 2008, 06:29 PM
#5
Thread Starter
Hyperactive Member
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.
-
Jan 28th, 2008, 06:49 PM
#6
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.
-
Jan 28th, 2008, 07:25 PM
#7
Thread Starter
Hyperactive Member
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?
-
Jan 28th, 2008, 08:07 PM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|