VB WINSOCK 实现文件传输
接收端接收到发送端发来的数据,接收到的数据里面有文件名和文件里面的内容,问题:如何接收到的数据将文件名和文件里面的内容分开代码如下:发送端:DimfpAsStringDi...
接收端接收到发送端发来的数据,接收到的数据里面有文件名和文件里面的内容,
问题:如何接收到的数据将文件名和文件里面的内容分开
代码如下:
发送端:
Dim fp As String
Dim fn As String
Dim fpn As String
Dim data() As Byte
Dim fl As Long
Private Sub Command1_Click()
fpn = fp + fn
fl = FileLen("D:\vb\winsockwork\fsd\1.txt")
' MsgBox fpn
' MsgBox fl
Winsock1.SendData fn
'Winsock1.SendData fl
Call dwj
End Sub
Private Sub Form_Load()
fp = "D:\vb\winsockwork\fsd\"
fn = "1.txt"
Text1.Text = fp
Text2.Text = 9001
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = Val(Text2.Text)
Winsock1.Listen
Form2.Show
'MsgBox fl
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
'MsgBox "请求连接"
End Sub
Sub dwj()
'MsgBox "读文件"
Open fpn For Binary As #1
ReDim data(LOF(1) - 1)
Get #1, , data()
Close 1
Winsock1.SendData data
'将读取的文件写入2.txt判断数是否傅一致
'Open "D:\vb\winsockwork\fsd\2.txt" For Binary As #2
'Put #2, , data()
'Close 2
End Sub
接收端:Dim fp As String
Dim fn As String
Dim fpn As String
Dim data() As Byte
Dim fl As Long
Private Sub Form_Load()
fp = "D:\vb\winsockwork\jsd\"
Winsock1.Protocol = sckTCPProtocol
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = Val( 9001)
Winsock1.Connect
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData fn, vbString, bytesTotal
'Winsock1.GetData fl, vbLong
fpn = fp + fn
Open fpn For Binary As #3
Winsock1.GetData data
Put #3, , data
Close 3
End Sub 展开
问题:如何接收到的数据将文件名和文件里面的内容分开
代码如下:
发送端:
Dim fp As String
Dim fn As String
Dim fpn As String
Dim data() As Byte
Dim fl As Long
Private Sub Command1_Click()
fpn = fp + fn
fl = FileLen("D:\vb\winsockwork\fsd\1.txt")
' MsgBox fpn
' MsgBox fl
Winsock1.SendData fn
'Winsock1.SendData fl
Call dwj
End Sub
Private Sub Form_Load()
fp = "D:\vb\winsockwork\fsd\"
fn = "1.txt"
Text1.Text = fp
Text2.Text = 9001
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = Val(Text2.Text)
Winsock1.Listen
Form2.Show
'MsgBox fl
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
'MsgBox "请求连接"
End Sub
Sub dwj()
'MsgBox "读文件"
Open fpn For Binary As #1
ReDim data(LOF(1) - 1)
Get #1, , data()
Close 1
Winsock1.SendData data
'将读取的文件写入2.txt判断数是否傅一致
'Open "D:\vb\winsockwork\fsd\2.txt" For Binary As #2
'Put #2, , data()
'Close 2
End Sub
接收端:Dim fp As String
Dim fn As String
Dim fpn As String
Dim data() As Byte
Dim fl As Long
Private Sub Form_Load()
fp = "D:\vb\winsockwork\jsd\"
Winsock1.Protocol = sckTCPProtocol
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = Val( 9001)
Winsock1.Connect
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData fn, vbString, bytesTotal
'Winsock1.GetData fl, vbLong
fpn = fp + fn
Open fpn For Binary As #3
Winsock1.GetData data
Put #3, , data
Close 3
End Sub 展开
2个回答
展开全部
可以在senddata的时候在数据前加入标识,如将fn=“fn [文件名]”,接收时可以用left函数提取标识,如Winsock1.GetData fn后 a = left(fn,2) if a = “fn" then ……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
假设文件名占用8个字节,将发送文件的第1个字节存储为文件名的长度8,然后用第2-第9个字节存储文件名,接收时,先获取第一个字节,得到文件名的长度8,然后截取在byte中的索引1-8的数据,还原文件名。
供参考
供参考
追问
我用了3个senddata 发送数据 .SendData fn '文件名 .SendData fl ‘文件大小 .SendData data ’文件内容。我接收到的数据.getData fn可以接收到全部数据,.getData data 接收到的数据是空?比如:我需要将文件名,文件大小,文件内容发送到接收端,接收端.getData 发送端和接收端的语句应该怎么写?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询