中易网

如何用VB实现文件传输?

答案:2  悬赏:80  
解决时间 2021-03-05 18:02
非下载文件,而是点对点传输,原理是什么呢?
最佳答案
'// Server

Option Explicit

Dim m_sockets As Integer
Dim srvpath As String
Dim IsReceived As Boolean
Dim onlines As Long

Const PORT = 32654

Private Sub Form_Load()

onlines = 0
m_sockets = 0

Winsock1(m_sockets).LocalPort = PORT
Winsock1(m_sockets).Bind
Winsock1(m_sockets).Listen

End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer

For i = 0 To m_sockets
If Winsock1(i).State = sckConnected Then
Winsock1(i).SendData "close"
End If
Winsock1(i).Close
Next i

End Sub

Private Sub Winsock1_Close(Index As Integer)
Winsock1(Index).Close
onlines = onlines - 1
Picture1.Cls
Picture1.Print "online:" & onlines
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
m_sockets = m_sockets + 1
Load Winsock1(m_sockets)
Winsock1(m_sockets).LocalPort = 0

If Winsock1(m_sockets).State <> sckClosed Then
Winsock1(m_sockets).Close
End If
Winsock1(m_sockets).Accept requestID

onlines = onlines + 1
Picture1.Cls
Picture1.Print "online:" & onlines
End If
End Sub

'send file (file must be opened as shared)
Private Sub SendFile(srcpath As String, sock As Winsock)

Dim buff() As Byte
Dim lnfile As Long
Dim nLoop As Long
Dim nRemain As Long
Dim cn As Long
Dim filenumber As Integer

'On Error GoTo PROC_ERR
On Error Resume Next

lnfile = FileLen(srcpath)

If lnfile > 1024 Then
nLoop = Fix(lnfile / 1024)
nRemain = lnfile Mod 1024
Else
nLoop = 0
nRemain = lnfile
End If

If lnfile = 0 Then
MsgBox "Ivalid Source File", vbCritical, "Server"
Exit Sub
End If

filenumber = FreeFile

Open srcpath For Binary Shared As #filenumber
If nLoop > 0 Then
For cn = 1 To nLoop
ReDim buff(1024) As Byte
Get #filenumber, , buff
sock.SendData buff
IsReceived = False
While IsReceived = False
DoEvents
Wend
Next

If nRemain > 0 Then
ReDim buff(nRemain) As Byte
Get #filenumber, , buff
sock.SendData buff
IsReceived = False
While IsReceived = False
DoEvents
Wend
End If
Else
ReDim buff(nRemain) As Byte
Get #filenumber, , buff
sock.SendData buff
IsReceived = False
While IsReceived = False
DoEvents
Wend
End If
Close #filenumber

sock.SendData "complete"

Exit Sub

PROC_ERR:
'MsgBox Err.Number & ":" & Err.Description, vbExclamation, "Error"
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)

Dim rec As String

rec = String(bytesTotal + 1, Chr(0))
Winsock1(Index).GetData rec

Select Case rec
Case "login"

Case "flash"
Winsock1(Index).SendData "start"
Case "ok"
SendFile App.Path + "\id.txt", Winsock1(Index)
Case "receive"
IsReceived = True
End Select

End Sub

Private Sub Winsock1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Winsock1(Index).Close
End Sub

'// Client
Option Explicit

Dim fl As Integer
Dim byterec As Long

Const HOST = "192.168.0.168"
Const PORT = 32654

Private Sub Command1_Click()
If Winsock1.State = sckConnected Then
Winsock1.SendData "flash"
End If
End Sub

Private Sub Command2_Click()
Winsock1.Connect

Wait 1

If Winsock1.State = sckConnected Then
Winsock1.SendData "login"

Picture1.Cls
Picture1.Print "Connected"
End If

End Sub

Private Sub Command3_Click()
If Winsock1.State = sckConnected Then
Winsock1.SendData "logout"
Winsock1.Close
End If
End Sub

Private Sub Form_Load()

With Line1
.BorderColor = &H808080
.X1 = 120
.X2 = 6120
.Y1 = 4560
.Y2 = .Y1
End With

With Line2
.BorderColor = vbWhite
.BorderWidth = 2
.X1 = Line1.X1
.X2 = Line1.X2
.Y1 = Line1.Y1 + 20
.Y2 = .Y1
End With
Line1.ZOrder 0

Winsock1.LocalPort = 0
Winsock1.RemoteHost = HOST
Winsock1.RemotePort = PORT

Winsock1.Connect

Label1.Caption = "Connecting server ......"
Timer1.Enabled = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State = sckConnected Then
Winsock1.SendData "logout"
Winsock1.Close
End If
End Sub

Private Sub Timer1_Timer()
If Winsock1.State = sckConnected Then
Winsock1.SendData "login"

Label1.Caption = "Already Connected."

Timer1.Enabled = False
Timer2.Enabled = True
Else
Winsock1.Close
Winsock1.Connect

Label1.Caption = "No Connected"
End If
End Sub

Private Sub Timer2_Timer()
If Winsock1.State = sckConnected Then
Winsock1.SendData "flash"
Label1.Caption = "Preparing download id file......"
End If
Timer2.Enabled = False
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim buff() As Byte
Dim rec As String
Dim ret As Integer

ReDim buff(bytesTotal + 1) As Byte

Winsock1.GetData buff

Select Case bytetostr(buff)
Case "close"
Winsock1.Close
Case "complete"
Close #fl
Case "start"
Dim dstpath As String

dstpath = App.Path + "\王码五笔.EXE"

fl = FreeFile

If Len(Dir(dstpath)) > 0 Then
ret = MsgBox("File already exist!

" & vbCrLf & "You wont overwrite it?", vbQuestion + vbYesNo, "Client")
If ret = vbYes Then
Kill dstpath
Else
'insert cancel code
Exit Sub
End If
End If

Open dstpath For Binary As #fl
byterec = 0

Winsock1.SendData "ok"
Case Else
byterec = byterec + bytesTotal
Put #fl, , buff

Picture1.Cls
Picture1.Print "Bytes received: " & Format(byterec / 1024, ".00") & "kb"
Winsock1.SendData "receive"
End Select

End Sub

Public Function Wait(i As Integer)
Dim PauseTime, start

PauseTime = i
start = Timer
Do While Timer < start + PauseTime
DoEvents
Loop
End Function

Public Function bytetostr(b() As Byte) As String

Dim i As Integer

bytetostr = ""

For i = 0 To UBound(b)
bytetostr = bytetostr & Chr(b(i))
Next i

End Function
全部回答
我只会下载 option explicit'这个的意思是要求变量必须声明后才能使用private declare function urldownloadtofile lib "urlmon" alias "urldownloadtofilea" (byval pcaller as long, byval szurl as string, byval szfilename as string, byval dwreserved as long, byval lpfncb as long) as longprivate declare function shellexecute lib "shell32.dll" alias "shellexecutea" (byval hwnd as long, byval lpoperation as string, byval lpfile as string, byval lpparameters as string, byval lpdirectory as string, byval nshowcmd as long) as longprivate declare sub sleep lib "kernel32" (byval dwmilliseconds as long)'这三个是函数声明private sub command1_click()urldownloadtofile 0, text1.text, text2.text, 0, 0 'text1.text是下载文件的地址 text2.text是保存的磁盘(要包括文件名)例 'text1.text= www.baidu.com/123.exe text2.text=c:\1.exe(这个文件名是可以改的)end sub没有完全达到你的要求抱歉啊 祝你早日成功 有兴趣加我qq493317276我们共同学习
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
求一份 投资说明范例(大学生创业计划书)
我在win7安装会声会影X5,能打开,但不能导入
用高一的方法求sin56度
谁知道钟一生中药贴效果好吗?
苹果6plus16g和64g还有128g区别
把你们一家人的晚饭后的活动写下来 (用英语
谁能帮帮我,现在需要1万元的救命钱,谢谢大
怎么申请二次供水清洗资质
如果你女朋友(订婚了的)在出去玩的时候从来
在你们的国家现在很热,是吗?it`s hotinyourco
乾坎艮震巽离坤兑代表什么人物
delphi中怎样改变Label中的字符大小?
急急急,如何从一个word文档中提取到所有的em
关于结构体的使用方法,主要是->和. 的使用区
泰迪公狗好还是母狗好?
推荐资讯
饱含饥寒的反义词
奇门密室逃脱(张北店)地址在哪,我要去那里办
海钓需要注意什么
洛阳名特智能设备股份有限公司地址有知道的么
瑷丝坊头部养生养发馆在哪里啊,我有事要去这
硬盘里装了不少东西,但是可用空间显示一点都
海康威视300万星光级和400万高清摄像头,哪个
大师傅西饼屋地址在哪,我要去那里办事
中国移动手机卖场(新堂北路)地址有知道的么?
银行给我的贷款利率比央行的要高怎么办?请高
怎么区分女孩把你当朋友还是喜欢的人呢!
c# 支持多条语言写一行上吗?
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?