導航:首頁 > 文件教程 > vbnet大文件傳輸

vbnet大文件傳輸

發布時間:2023-08-07 07:01:02

Ⅰ 如何用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 Integer

Try
buf=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.Length
willread=BufLen
If fs.Length-fs.Position<BufLen Then
willread=CInt(fs.Length-fs.Position)
End If
fs.Read(buf,0,willread)
s.Send(buf,willread,remoteep)
Thread.Sleep(Interval)
End While
End Using
Catch ex As Exception
MsgBox(ex.ToString())
Finally
If s IsNot Nothing Then
s.Close()
End If
End Try

閱讀全文

與vbnet大文件傳輸相關的資料

熱點內容
一個文件盒省內寄順豐多少錢 瀏覽:41
誅仙62坐騎怎麼升級到63 瀏覽:926
linux以日期查看日誌記錄 瀏覽:446
工業大數據是什麼東西 瀏覽:881
魅族note3怎麼重置網路 瀏覽:510
c語言程序設計模 瀏覽:92
兒童怎麼做可編程機 瀏覽:603
數據計算屬於什麼統計學 瀏覽:921
07word怎麼去掉標記 瀏覽:979
qq緩存的數據是什麼 瀏覽:348
LED主Kv文件多少兆 瀏覽:856
蘋果edge怎麼刪除下載文件 瀏覽:471
sas邏輯回歸代碼 瀏覽:572
用於keil下的stc器件資料庫 瀏覽:400
新聞網站後台如何操作前台 瀏覽:539
在剪映app中怎麼查看視頻尺寸 瀏覽:9
linux文件成分包括 瀏覽:886
文件轉換免費的軟體 瀏覽:644
linuxwpsxlsx 瀏覽:482
小米手機怎麼上移動網路連接失敗怎麼辦 瀏覽:598

友情鏈接