VB的WINSOCK实现文件传输(例子简单点最好多注释,别复制别人的哦!)
1个回答
2013-08-07
展开全部
(自己收藏的一个代码,分为两个程序,添加了WINSOCK和CommonDialog部件)文件发送端:Private Sub Command1_Click()
Winsock1.Close
Winsock1.Connect "127.0.0.1", 5000
End SubPrivate Sub Command2_Click()
'======================================================================================一,文件名检测
Dim a() As Byte
If Winsock1.State = sckClosed Then '检测TCP连接
MsgBox "没有连接服务端,请先连接之", vbCritical, "Alert"
Exit Sub
End If CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量'======================================================================================二,发送数据
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
'-----------------------------------------------------1:小于1000直接发送,然后退出
If LOF(1) <= 1000 Then
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close '此时直接关闭二进制读取的文件
Exit Sub
End If
'-----------------------------------------------------2:如果文件体积大于1000字节,就分块发送
If LOF(1) > 1000 Then
lump = LOF(1) \ 1000 '块数
left_lump = LOF(1) Mod 1000 '剩下块的文件大小
For i = 1 To lump
ReDim a(1 To 1000) As Byte
Get #1, , a '每次调用GET方法,文件指针自动向后移动1000字节
Winsock1.SendData a '每次发送1000字节的数据
Next
End If
'-----------------------------------------------------3:得到分块发送后的最后一块数据
If left_lump > 0 Then '这里时分块发送数据时没有发送完毕的数据
ReDim a(1 To left_lump) As Byte '定义一个和剩余文件大小相同的字节数组
Get #1, , a 'GET方法自动获取填满数组空间的数据
Winsock1.SendData a '最后一次发送数据
End If
'--------------------------------------------------------------------------------------
Close
'======================================================================================
End SubPrivate Sub Command3_Click()
Winsock1.Close
End SubPrivate Sub Command4_Click()
'----------------------------------------------------------------------------------------------
CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量
'----------------------------------------------------------------------------------------------
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close
'----------------------------------------------------------------------------------------------
End SubPrivate Sub Winsock1_Connect()
List1.AddItem "TCP连接成功建立!"
End SubPrivate Sub Winsock1_SendComplete()
MsgBox "发送数据完毕!"
End SubPrivate Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
List2.AddItem CStr(bytesSent) + " " + CStr(bytesRemaining) '显示发送的过程!
End Sub 文件接收端:Private Sub Command1_Click()
Winsock1.Close
Winsock1.Connect "127.0.0.1", 5000
End SubPrivate Sub Command2_Click()
'======================================================================================一,文件名检测
Dim a() As Byte
If Winsock1.State = sckClosed Then '检测TCP连接
MsgBox "没有连接服务端,请先连接之", vbCritical, "Alert"
Exit Sub
End If CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量'======================================================================================二,发送数据
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
'-----------------------------------------------------1:小于1000直接发送,然后退出
If LOF(1) <= 1000 Then
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close '此时直接关闭二进制读取的文件
Exit Sub
End If
'-----------------------------------------------------2:如果文件体积大于1000字节,就分块发送
If LOF(1) > 1000 Then
lump = LOF(1) \ 1000 '块数
left_lump = LOF(1) Mod 1000 '剩下块的文件大小
For i = 1 To lump
ReDim a(1 To 1000) As Byte
Get #1, , a '每次调用GET方法,文件指针自动向后移动1000字节
Winsock1.SendData a '每次发送1000字节的数据
Next
End If
'-----------------------------------------------------3:得到分块发送后的最后一块数据
If left_lump > 0 Then '这里时分块发送数据时没有发送完毕的数据
ReDim a(1 To left_lump) As Byte '定义一个和剩余文件大小相同的字节数组
Get #1, , a 'GET方法自动获取填满数组空间的数据
Winsock1.SendData a '最后一次发送数据
End If
'--------------------------------------------------------------------------------------
Close
'======================================================================================
End SubPrivate Sub Command3_Click()
Winsock1.Close
End SubPrivate Sub Command4_Click()
'----------------------------------------------------------------------------------------------
CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量
'----------------------------------------------------------------------------------------------
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close
'----------------------------------------------------------------------------------------------
End SubPrivate Sub Winsock1_Connect()
List1.AddItem "TCP连接成功建立!"
End SubPrivate Sub Winsock1_SendComplete()
MsgBox "发送数据完毕!"
End SubPrivate Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
List2.AddItem CStr(bytesSent) + " " + CStr(bytesRemaining) '显示发送的过程!
End Sub
Winsock1.Close
Winsock1.Connect "127.0.0.1", 5000
End SubPrivate Sub Command2_Click()
'======================================================================================一,文件名检测
Dim a() As Byte
If Winsock1.State = sckClosed Then '检测TCP连接
MsgBox "没有连接服务端,请先连接之", vbCritical, "Alert"
Exit Sub
End If CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量'======================================================================================二,发送数据
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
'-----------------------------------------------------1:小于1000直接发送,然后退出
If LOF(1) <= 1000 Then
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close '此时直接关闭二进制读取的文件
Exit Sub
End If
'-----------------------------------------------------2:如果文件体积大于1000字节,就分块发送
If LOF(1) > 1000 Then
lump = LOF(1) \ 1000 '块数
left_lump = LOF(1) Mod 1000 '剩下块的文件大小
For i = 1 To lump
ReDim a(1 To 1000) As Byte
Get #1, , a '每次调用GET方法,文件指针自动向后移动1000字节
Winsock1.SendData a '每次发送1000字节的数据
Next
End If
'-----------------------------------------------------3:得到分块发送后的最后一块数据
If left_lump > 0 Then '这里时分块发送数据时没有发送完毕的数据
ReDim a(1 To left_lump) As Byte '定义一个和剩余文件大小相同的字节数组
Get #1, , a 'GET方法自动获取填满数组空间的数据
Winsock1.SendData a '最后一次发送数据
End If
'--------------------------------------------------------------------------------------
Close
'======================================================================================
End SubPrivate Sub Command3_Click()
Winsock1.Close
End SubPrivate Sub Command4_Click()
'----------------------------------------------------------------------------------------------
CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量
'----------------------------------------------------------------------------------------------
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close
'----------------------------------------------------------------------------------------------
End SubPrivate Sub Winsock1_Connect()
List1.AddItem "TCP连接成功建立!"
End SubPrivate Sub Winsock1_SendComplete()
MsgBox "发送数据完毕!"
End SubPrivate Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
List2.AddItem CStr(bytesSent) + " " + CStr(bytesRemaining) '显示发送的过程!
End Sub 文件接收端:Private Sub Command1_Click()
Winsock1.Close
Winsock1.Connect "127.0.0.1", 5000
End SubPrivate Sub Command2_Click()
'======================================================================================一,文件名检测
Dim a() As Byte
If Winsock1.State = sckClosed Then '检测TCP连接
MsgBox "没有连接服务端,请先连接之", vbCritical, "Alert"
Exit Sub
End If CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量'======================================================================================二,发送数据
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
'-----------------------------------------------------1:小于1000直接发送,然后退出
If LOF(1) <= 1000 Then
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close '此时直接关闭二进制读取的文件
Exit Sub
End If
'-----------------------------------------------------2:如果文件体积大于1000字节,就分块发送
If LOF(1) > 1000 Then
lump = LOF(1) \ 1000 '块数
left_lump = LOF(1) Mod 1000 '剩下块的文件大小
For i = 1 To lump
ReDim a(1 To 1000) As Byte
Get #1, , a '每次调用GET方法,文件指针自动向后移动1000字节
Winsock1.SendData a '每次发送1000字节的数据
Next
End If
'-----------------------------------------------------3:得到分块发送后的最后一块数据
If left_lump > 0 Then '这里时分块发送数据时没有发送完毕的数据
ReDim a(1 To left_lump) As Byte '定义一个和剩余文件大小相同的字节数组
Get #1, , a 'GET方法自动获取填满数组空间的数据
Winsock1.SendData a '最后一次发送数据
End If
'--------------------------------------------------------------------------------------
Close
'======================================================================================
End SubPrivate Sub Command3_Click()
Winsock1.Close
End SubPrivate Sub Command4_Click()
'----------------------------------------------------------------------------------------------
CommonDialog1.Filter = "EXE FILE|*.exe"
CommonDialog1.ShowOpen '打开对话框
If CommonDialog1.FileName = "" Then
Exit Sub
End If
send_filename = CommonDialog1.FileName '得到要发送的文件名,设置file_name变量
'----------------------------------------------------------------------------------------------
Open send_filename For Binary As #1 '使用二进制方法,打开要发送的文件
ReDim a(LOF(1) - 1) As Byte '因为数组从0开始,所以这里数组上标要减去1
Get #1, , a '把文件内容存入数组
Winsock1.SendData a '使用senddata丰富一次性发送数据
Close
'----------------------------------------------------------------------------------------------
End SubPrivate Sub Winsock1_Connect()
List1.AddItem "TCP连接成功建立!"
End SubPrivate Sub Winsock1_SendComplete()
MsgBox "发送数据完毕!"
End SubPrivate Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
List2.AddItem CStr(bytesSent) + " " + CStr(bytesRemaining) '显示发送的过程!
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询