利用VB6实现客户端与服务端传输文件 ━━━━━━━━━━━━━━━━━━━━━━━━━ 如果文件大的话,一定不要大于8k 一个例子我自己做的,基本没有问题了 服务器代码: Option Explicit Private Sub Command1_Click() Dim BytDate() As Byte Dim FileName As String Dim lngFile As Long Dim i As Long FileName = "D:\Image\Oct2003.MDB " '取得文件名及路径 lngFile = FileLen(FileName) \ 1024 '取得文件长度 Me.ProgressBar1.Min = 0 Me.ProgressBar1.Max = lngFile + 1 ProgressBar1.Value = 0 For i = 0 To lngFile ReDim myFile(1023) As Byte '初始化数组 Open FileName For Binary As #1 '打开文件 Get #1, i * 1024 + 1, myFile '将文件写入数组 Close #1 '关闭文件 Winsock1.SendData myFile '发送 DoEvents ProgressBar1.Value = ProgressBar1.Value + 1 Next i If ProgressBar1.Value = ProgressBar1.Max Then MsgBox "OK" End Sub Private Sub Form_Load() Winsock1.Protocol = sckTCPProtocol Winsock1.LocalPort = 2001 Winsock1.Listen FormCLI.Show End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) If Winsock1.State <> 0 Then Winsock1.Close Winsock1.Accept requestID End Sub ━━━━━━━━━━━━━━━━━━━━━━━━━ 客户端: Option Explicit Private Sub Form_Load() With Winsock1 .Protocol = sckTCPProtocol .RemoteHost = "192.168.0.69" .RemotePort = 2001 .Connect End With End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Static i As Long Dim myFile() As Byte Dim myLong As Double Dim myPath As String myPath = "d:\abc.MDB" ReDim myFile(bytesTotal - 1) '此处也可以是(0 To bytesTotal-1) Winsock1.GetData myFile Open myPath For Binary As #1 '新建文件 myLong = FileLen(myPath) Put #1, myLong + 1, myFile '将收到的数据写入新文件中 Close #1 '关闭 End Sub |
|