用winsock控件,基于TCP/IP进行文件传送Private Sub cmdSend_Click() '向服务器发送文件 Dim myFile() As Byte Dim lngFile As Long '文件长度 Dim FileName As String '文件名称 Static i As Single
sockClient.Close sockClient.Connect
Do DoEvents If sockClient.State = sckConnected Then Exit Do Else i = i + 1 If i > 200000 Then Dim box As String box = MsgBox("是否放弃?", vbYesNo, "连接失败:") If box = vbYes Then i = 0 Exit Do Else i = 0 End If End If End If DoEvents Loop lenth = 0 '检查是否与服务器连接 If sockClient.State = sckConnected Then FileName = VB.App.Path & "\1.jpg" '取得文件名及路径 lngFile = FileLen(FileName) '取得文件长度 ReDim myFile(lngFile - 1) As Byte '初始化数组
Open FileName For Binary As #1 '打开文件 Get #1, , myFile '将文件写入数组 Close #1 '关闭文件 sockClient.SendData myFile '发送 Else MsgBox "没有连接" End If End Sub Private Sub sockServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) '接收文件 Static i As Long Dim myFile() As Byte Dim myLong As Double Dim myPath As String myPath = VB.App.Path & "\a.doc" cmdC.Caption = "正在接收数据... " & bytesTotal ReDim myFile(0 To bytesTotal - 1) '此处也可以是(0 To bytesTotal-1)
sockServer(Index).GetData myFile i = i + 1
Open myPath For Binary As #1 '新建文件 Put #1, lenth + 1, myFile '将收到的数据写入新文件中 Close #1 '关闭 '记录文件长度 lenth = lenth + UBound(myFile) - LBound(myFile) + 1
'向服务器发送文件
Dim myFile() As Byte
Dim lngFile As Long '文件长度
Dim FileName As String '文件名称
Static i As Single
sockClient.Close
sockClient.Connect
Do
DoEvents
If sockClient.State = sckConnected Then
Exit Do
Else
i = i + 1
If i > 200000 Then
Dim box As String
box = MsgBox("是否放弃?", vbYesNo, "连接失败:")
If box = vbYes Then
i = 0
Exit Do
Else
i = 0
End If
End If
End If
DoEvents
Loop
lenth = 0
'检查是否与服务器连接
If sockClient.State = sckConnected Then FileName = VB.App.Path & "\1.jpg" '取得文件名及路径
lngFile = FileLen(FileName) '取得文件长度
ReDim myFile(lngFile - 1) As Byte '初始化数组
Open FileName For Binary As #1 '打开文件
Get #1, , myFile '将文件写入数组
Close #1 '关闭文件 sockClient.SendData myFile '发送 Else
MsgBox "没有连接"
End If
End Sub
Private Sub sockServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'接收文件
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = VB.App.Path & "\a.doc"
cmdC.Caption = "正在接收数据... " & bytesTotal
ReDim myFile(0 To bytesTotal - 1) '此处也可以是(0 To bytesTotal-1)
sockServer(Index).GetData myFile
i = i + 1
Open myPath For Binary As #1 '新建文件
Put #1, lenth + 1, myFile '将收到的数据写入新文件中
Close #1 '关闭
'记录文件长度
lenth = lenth + UBound(myFile) - LBound(myFile) + 1
End Sub
这一句改成:
myPath = VB.App.Path & "\a.jpg"
要示然文件格式不对