Using the Put() function/Using Winsock 
Author Message
 Using the Put() function/Using Winsock

when working with strings in vb, you have to be carefull some times as vb
strings are unicode.  when taking a byte array and sending it to the client,
you need to be carefull how you convert it back.  I used the get/put code
for xferring binary files myself and had no issues, however, i read the file
in as a string instead of a byte array.
try this...

dim fdata as string
    ' this will make the string a certain size so that you can read the
specific number of bytes from the file (packetsize), if there is less bytes
than the packet size, make sure you keep track of that and adjust packet
size accordingly for the last get so you don't try to read 4k when there is
only 1k left.
fdata = String(PacketSize, " ")

get #fhandle,, fdata
socket.senddata fdata

dim dat() as byte
dim tmp as variant
dim fdata as string

socket.getdata dat  ' get the dat in a binary array so that string data
isn't cut off when a
    ' null char is found
tmp = dat                ' use a variant, then a string, this makes the byte
arr convert to the string properly
fdata = tmp
    ' it's ok if the binary is represented as a string, you just cant read
it, but its the same data as before.
put #fhandle,, fdata    ' now write the string to the file.

I used this routine in a file copy of my own for an auto update program.
The only difficulties i had was that i was recieving a string on the client,
because the null char would cut some of it off occasionally, so i had to get
the data as a byte array and convert it back to a string.  My packets
actually tossed header info into the string such as curr packet, etc...

Good Luck


P.S.  Still open your files as binary, just get a string instead of a byte
array from the file.

> I am using Winsock to send a text file from the server-
> side to the client-side. On the server side, I am using
> the Get() function to read the data from the file and send
> it to the client-side. The server-side portion of the code
> works fine. On the client-side, I am using the Put()
> function to store the data to the file. But the data is
> not being written correctly. When I view the client-side
> text file, there are additional spaces between each
> character. So on the server side, my text file has "this
> is a  text file" and on the client side after using the Put
> () function, the text file reads "t h i s   i s   a     t
> e x t   f i l e ". Below is the code I am using on the
> server-side:

> Private Sub SendFile(ByVal PicSaveName As String)

> Dim iFreeFile As Integer
> Dim caFileData() As Byte
> iFreeFile = FreeFile

> Open PicSaveName For Binary As iFreeFile
> ReDim caFileData(1 To LOF(iFreeFile))
> Get iFreeFile, , caFileData
> Close
> Server.SendData caFileData

> End Sub

> Again, I have tested the code, and this portion of the
> code works properly. Below is the code I am using on the
> clien-side:

> Dim FileLength
> Dim FileData
> Dim FileName As String
> Dim caFileData() As Byte
> Dim iFreeFile As Integer
> iFreeFile = FreeFile


> MsgBox "file name = " & FileName
> MsgBox "file length = " & FileLength
> MsgBox "file data = " & FileData

> caFileData = FileData
> MsgBox caFileData

> Open FileName For Binary As iFreeFile
> Put iFreeFile, , caFileData
> Close

> End Sub

> The "MsgBox caFileData" command displays the the file data
> to be "this is a  text file". But when I view the file
> after the Put() function has been used, the text file has
> padding between each of the letters. I have checked the
> MSDN library and it says that the "Put writes all
> variables to disk contiguously; that is, with no padding
> between records." So I don't understand why there is
> padding since I have opened this file in Binary.
> I eventually would like to send picture files (.jpg) from
> the server to the client, so any help regarding text or
> picture files would be greatly appreciated. Also, if you
> can suggest a better way of doing this, I would appreciate
> that also.
> Thanks,
> Nimish

Sat, 15 May 2004 05:40:16 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Using the Put() function/Using Winsock

2. Using the Put() function/Using Winsock

3. Executing Function using String Containing Function Name

4. Calling a sub or function using a variable through another sub or function

5. textwidth() function when text is to be printed using the tab() function

6. Uploading a file using https put

7. French characters lost when using Print,Write or Put

8. Can't put files using Internet Transfer Control 6 SP4

9. How to get/put Blob from richtextbox using VB6

10. Scrolling Screen13 using GET & PUT

11. Putting Bitmaps into my program using QB

12. Putting a picture in Access using vb code


Powered by phpBB® Forum Software