求用Winsock+Tcp协议+二进制来传输文件的代码,三十分全部付出.......

RussellMX 2004-12-17 04:47:12
求用Winsock+Tcp协议+二进制来传输文件的代码,三十分全部付出.......
...全文
214 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mustudent 2005-07-05
  • 打赏
  • 举报
回复
up
好象没有udp的传送吧zyg0(影子(转了1圈,我又回来了)
你的好象也是tcp的文件传送
wosirius 2005-07-04
  • 打赏
  • 举报
回复
mm
zhujiechang 2005-07-04
  • 打赏
  • 举报
回复
最近刚写了相关的下载程序,分段的代码如下:
'传送的默认大小
'OpenFileNum打开的文件号
ChunkSize = FT_BUFFER_SIZE(我设置为5734)

'计算发送的位置
If (LOF(OpenFileNum) - Loc(OpenFileNum)) < FT_BUFFER_SIZE Then _
ChunkSize = (LOF(OpenFileNum) - Loc(OpenFileNum))

ReDim Chunk(0 To ChunkSize - 1)
然后
'read the chunk
Get #OpenFileNum, , Chunk
'Send the data
mvarGetWinSock.SendData Chunk
zyg0 2004-12-19
  • 打赏
  • 举报
回复
是tcp的,不是udp的
zyg0 2004-12-19
  • 打赏
  • 举报
回复
Option Explicit
'==============================================
'===============================
'udp传文件
'客户端
'作者: 影子
'================================
'==============================================
Dim FileNumber As Integer '用来存文件的句柄
Dim LenFile As Long '文件的长度
Private Sub Command2_Click()
closefile
End Sub

Private Sub Form_Load()
Winsock0.LocalPort = 5698
Winsock0.Listen
beginfile
End Sub

Private Sub Winsock0_ConnectionRequest(ByVal requestID As Long)
If Me.Winsock0.State <> sckClosed Then Me.Winsock0.Close
Me.Winsock0.Accept requestID
End Sub

Private Sub Winsock0_DataArrival(ByVal bytesTotal As Long)
Dim FileByte() As Byte
Winsock0.GetData FileByte, vbArray + vbByte '接收类型为:字节数组


' 写文件
Put #FileNumber, , FileByte,
' 发送继续发送的请求
frmmain.Winsock0.SendData "ok"

End Sub

Public Sub beginfile()
FileNumber = FreeFile '取得未使用的文件号
Open "c:\aaa.exe" For Binary As #FileNumber '打开文件
End Sub

Public Sub closefile() '关闭文件句柄
Close #FileNumber
End Sub

Option Explicit
Dim GetFileNum As Integer
Dim LenFile As Long
Dim Sendbaye() As Byte '发送的包
'===============================
'udp传文件
'作者: 影子
'服务器端
'================================


Private Sub Command1_Click()
GetFileNum = FreeFile '取得未使用的文件号
LenFile = FileLen("d:\aa.rar") '获得需传送的文件的长度
Open "d:\aa.rar" For Binary As #GetFileNum '打开需传送的文件
Command1.Enabled = False
' 传送文件
Call TCPSendFile(frmmain.Winsock0, GetFileNum, SplitFile)
Text1.Text = Now
End Sub

Private Sub Form_Load()
frmmain.Winsock0.RemoteHost = "192.168.0.12" '服务器ip
frmmain.Winsock0.RemotePort = 5698
Winsock0.Connect

End Sub
'=========================================================================
'为了清晰,下面分别用两个子过程来完成计算这次还可以传多少个字节的数据和传送数据
'==========================================================================
Private Function SplitFile() As Long '拆包
On Error Resume Next
Dim GetCount As Long
'计算出这次可发送的字节数
If LenFile >= 4000 Then
GetCount = 4000
LenFile = LenFile - GetCount
Else
GetCount = LenFile
LenFile = LenFile - GetCount
End If
SplitFile = GetCount

End Function
Private Sub TCPSendFile(objWinSock As Winsock, FileNumber As Integer, SendLen As Long)
Dim FileByte() As Byte



Dim tempa As String * 4
ReDim FileByte(SendLen - 1)

Get #FileNumber, , FileByte '读取文件

frmmain.Winsock0.SendData FileByte
End Sub

Private Sub Winsock0_DataArrival(ByVal bytesTotal As Long)
Dim str As String
frmmain.Winsock0.GetData str
Select Case str
Case "ok"
'成功继续发送
If LenFile = 0 Then '发送完成
MsgBox "成功"
Exit Sub
End If
Call TCPSendFile(frmmain.Winsock0, GetFileNum, SplitFile)

End Select
End Sub
RussellMX 2004-12-19
  • 打赏
  • 举报
回复
你是一次传送的,能不能改成分包传送的??大家也帮帮忙改一改,相信对于大家来说不是难题吧?
JayJay 2004-12-19
  • 打赏
  • 举报
回复
Private Sub Command3_Click()
Dim myFile() As Byte
Dim lngFile As Long
Dim FileName As String
Static i As Single
FileName = VB.App.Path & "\my.gif"
lngFile = FileLen(FileName)
ReDim myFile(lngFile - 1) As Byte
Open FileName For Binary As #1
Get #1, , myFile
Close #1
WskClient.SendData myFile
End Sub

Private Sub WskServer_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 & "\my.gif"
ReDim myFile(0 To bytestotal - 1)
WskServer(Index).GetData myFile
Open myPath For Binary As #1
Put #1, lenth + 1, myFile
Close #1
lenth = lenth + UBound(myFile) - LBound(myFile) + 1
End Sub

上面这段程序符合你的要求,但是也只能传8K的。
houniao006 2004-12-18
  • 打赏
  • 举报
回复
我写的只能传输8k!
要的话联系我!
houniao006@163.com
helanshan 2004-12-17
  • 打赏
  • 举报
回复
不是很难,建议自己写..
写到不会的地方再问..
cindytsai 2004-12-17
  • 打赏
  • 举报
回复
帮顶一下!

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧