|
-
Apr 2nd, 2005, 04:10 PM
#1
Visual Basic - Send File With Winsock
Just a small code snippet to transfer the contents of a file over a Winsock connection (the socket must first be connected). Here is a brief description of the parameters:
SendFile(SocketObject As Variant, FilePath As String, PacketSize As Long)
- SocketObject - The winsock control you will be using to send the file. It is passed as a variant so you can use a winsock control that is part of an array, or one that isn't.
- FilePath - The full path to the file you are sending.
- PacketSize - The amount of data to send at once, or in one packet. The file is sent in seperate chunks, and this specifies how many bytes to send at a time. For slower connections, like 56k, you will want to make this a small number, like 512 (1/2 KB) or 1024 (1 KB). For high speed internet connections you can make it much larger.
Code:
Public Sub SendFile(SocketObject As Variant, ByVal FilePath As String, ByVal PacketSize As Long)
Dim lonFF As Long, bytData() As Byte
Dim lonCurByte As Long, lonSize As Long
Dim lonPrevSize As Long
On Error GoTo ErrorHandler
lonSize = FileLen(FilePath)
Open FilePath For Binary Access Read As #lonFF
ReDim bytData(1 To PacketSize) As Byte
Do Until (lonSize - lonCurByte) < PacketSize
Get #lonFF, lonCurByte + 1, bytData()
lonCurByte = lonCurByte + PacketSize
SocketObject.senddata bytData()
DoEvents
Loop
lonPrevSize = lonSize - lonCurByte
If lonPrevSize > 0 Then
ReDim bytData(1 To lonPrevSize) As Byte
Get #lonFF, lonCurByte + 1, bytData()
lonCurByte = lonCurByte + lonPrevSize
SocketObject.senddata bytData()
End If
Close #lonFF
Exit Sub
ErrorHandler:
Debug.Print "Send File Error"
Debug.Print "---------------"
Debug.Print "Number: " & Err.Number
Debug.Print "Description: " & Err.Description
Debug.Print "File: " & FilePath
End Sub
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
|