'Client Code
'APIs to get the file size..
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Private Const OF_READ = &H0&
Private Sub Command2_Click()
Winsock1.Connect "xxx.xxx.xxx.xxx", 6000
End Sub
Private Sub Command3_Click()
Dim strFileName As String
Dim strFilePath As String
strFilePath = "c:\tmp\anything.exe"
strFileName = "anything.exe"
Dim lFileSize As Long
lFileSize = FileSize(strFilePath)
' Send the size of the file to the server.
' Send the file name to the server.
If Winsock1.State = sckConnected Then
Winsock1.SendData "FILESIZE=" & lFileSize
DoEvents
Winsock1.SendData "FILENAME=" & strFileName
DoEvents
End If
NF = FreeFile
Open strFilePath For Binary Access Read Lock Read As #NF
Do While (Loc(NF) < LOF(NF))
DoEvents
file = Input(4096, NF)
If Winsock1.State = sckConnected Then
Winsock1.SendData file
End If
Loop
Close #NF
End Sub
Private Function FileSize(ByVal strSrc As String) As Long
Dim Pointer As Long, sizeofthefile As Long, lpFSHigh
Pointer = lOpen(strSrc, OF_READ)
'size of the file
sizeofthefile = GetFileSize(Pointer, lpFSHigh)
lclose Pointer
FileSize = sizeofthefile
End Function
Private Sub Timer1_Timer()
Label1.Caption = Winsock1.State
End Sub
'Server Code
Private WF As Integer
Private dat As String
Private received As Long
Private g_strFileName As String
Private g_lFileSize As Long
Private g_strInPath As String
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
g_strInPath = "c:\incoming\" ' where to store the file..
received = 1
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Winsock1.State
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
Dim pos As Integer
If Left(dat, 8) = "FILESIZE" Then
pos = InStr(1, dat, "=")
g_lFileSize = Right(dat, Len(dat) - pos)
Exit Sub
ElseIf Left(dat, 8) = "FILENAME" Then
pos = InStr(1, dat, "=")
g_strFileName = Right(dat, Len(dat) - pos)
Exit Sub
End If
If received = 1 Then
Debug.Print "OPEN = " & g_strInPath & g_strFileName
WF = FreeFile
Open g_strInPath & g_strFileName For Binary As #WF
End If
Put #WF, received, dat
received = received + Len(dat)
Debug.Print received
If received >= g_lFileSize Then
Debug.Print "CLOSE = " & g_strInPath & g_strFileName
Close #WF
End If
End Sub