文件管理 · 2024年1月6日

vbnet大文件传输|ftp上传文件用vbnet怎么实现

Ⅰ 如何用VB实现文件传输

'// 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

Ⅱ ftp上传文件用vb.net怎么实现

My.Computer.Network.UploadFile(本地文件路径,ftp服务器路径包括文件名,用户名,密码)

Ⅲ vb6.0怎么用winsock发送大文件

'读一段来发一段,直到结束,使用自2进制读取文件,每读取一段发送成功后指针向后移动sendfilesize再读取,当然中间要加eof判断Const sendfilesize = 1024 '发送文件块大小Private Sub Command1_Click() Dim a() As Byte ReDim a(sendfilesize) End Sub

Ⅳ vb怎么实现远程文件传输

用WINSOCK控件Winsock控件对用户是不可视的,可以很容易地访问TCP和UDP网络服务。其可以被Microsoft Access, Visual Basic ,Visual C++或Visual FoxPro开发人员使用。要编写客户和服务器应用程序,不需要了解TCP或调用底层Winsock API的具体细节。通过设置Winsock控件的属性和调用该控件的方法,可以很容易地连接到远程计算机并进行双向的数据交换。 属性 BytesReceived属性,LocalHostName属性,LocalIP属性,LocalPort属性,RemoteHost属性(ActiveX控件),SocketHandle属性,State属性(Winsock控件),Protocol属性(Winsock控件),Name属性,Parent属性,RemoteHost属性(ActiveX控件),RemotePort属性(ActiveX控件),Index属性(ActiveX控件),Tag属性(ActiveX控件),Object属性(ActiveX控件)。 方法 Accept方法,Bind方法,Close方法(Winsock控件),Listen方法,PeerData方法,SendData方法,GetData方法(WinSock控件),GetData方法(ActiveX控件)。 事件 Close事件,ConnectionRequest事件,DataArrival事件,SendComplete事件,SendProgress事件,Error事件,Connect事件(Winsock控件),Connect事件。Winsock 控件对用户来说是不可见的,它提供了访问 TCP 和 UDP 网络服务的方便途径。Microsoft Access、Visual Basic、Visual C++ 或 Visual FoxPro 的开发人员都可使用它。为编写客户或服务器应用程序,不必了解 TCP 的细节或调用低级的 Winsock APIs。通过设置控件的属性并调用其方法就可轻易连接到一台远程机器上去,并且还可双向交换数据。TCP 基础数据传输协议允许创建和维护与远程计算机的连接。连接两台计算机就可彼此进行数据传输。如果创建客户应用程序,就必须知道服务器计算机名或者 IP 地址(RemoteHost 属性),还要知道进行“侦听”的端口(RemotePort 属性),然后调用 Connect 方法。如果创建服务器应用程序,就应设置一个收听端口(LocalPort 属性)并调用 Listen 方法。当客户计算机需要连接时就会发生 ConnectionRequest 事件。为了完成连接,可调用 ConnectionRequest 事件内的 Accept 方法。建立连接后,任何一方计算机都可以收发数据。为了发送数据,可调用 SendData 方法。当接收数据时会发生 DataArrival 事件。调用 DataArrival 事件内的 GetData 方法就可获取数据。UDP 基础用户数据文报协议 (UDP) 是一个无连接协议。跟 TCP 的操作不同,计算机并不建立连接。另外 UDP 应用程序可以是客户机,也可以是服务器。为了传输数据,首先要设置客户计算机的 LocalPort 属性。然后,服务器计算机只需将 RemoteHost 设置为客户计算机的 Internet 地址,并将 RemotePort 属性设置为跟客户计算机的 LocalPort 属性相同的端口,并调用 SendData 方法来着手发送信息。于是,客户计算机使用 DataArrival 事件内的 GetData 方法来获取已发送的信息。

Ⅳ vb.net udpclient 发送大文件循环问题

'缓冲区长度Const BufLen As Integer=800'包发送间隔Const Interval As Integer=62'缓冲区Dim buf As Byte()=Nothing'远程(目的)网络端点Dim remoteep As IPEndPoint=Nothing'如果你已将s定义为类的成员变量(实例或共享),注释掉下面这句 Dim s As UdpClient=Nothing Dim willread As IntegerTrybuf=New Byte(BufLen-1) {}s=New UdpClient()'TextBox1.Text包含远程(目的)主机的IP地址'TextBox2.Text包含远程(目的)主机的端口号remoteep=New IPEndPoint(IPAddress.Parse(Trim(TextBox1.Text)),CInt(TextBox2.Text))'Label2.Text包含要发送的文件的路径Using fs As New FileStream(Label2.Text,FileMode.Open,FileAccess.Read)While fs.Position<>fs.Lengthwillread=BufLenIf fs.Length-fs.Position<BufLen Thenwillread=CInt(fs.Length-fs.Position)End Iffs.Read(buf,0,willread)s.Send(buf,willread,remoteep)Thread.Sleep(Interval)End WhileEnd UsingCatch ex As ExceptionMsgBox(ex.ToString())FinallyIf s IsNot Nothing Thens.Close()End IfEnd Try