The UDP protocol is a connectionless protocol, and he and the TCP protocol have a fast transmission speed, accounting for less resources. However, since the UDP protocol itself does not automatically find the package, there is often a packet loss, which will cause the transfer of file packet loss because the time has been in a hurry and the level is limited, and it is not optimized in efficiency. It is simply realized. Please see the source code. Note: Main function: Gue the file into the 4K size package in the length of Baotou on the package, accepting 1 package judgment length and acceptable length
If you meet, continue, if you have a packet, then you want everyone to do any good suggestions to inform me, I will try to improve the option expedition '================== ============================= ===================== ========== 'udp file transfer' client 'author: shadow' ============================ ==== '============================================= = DIM FILENUMBER AS INTEGER 'The length of the handle of the file DIM LENFILE AS Long' file Private submmand2_click () ClosefileEnd Sub
Private Sub Form_Load () Winsock0.LocalPort = 5698Winsock0.BindbeginfileEnd SubPrivate Sub Winsock0_DataArrival (ByVal bytesTotal As Long) Dim FileByte () As ByteWinsock0.GetData FileByte, vbArray vbByte 'receiver type: byte array Dim mendByte () As Byte, i As Long, J AS String 'Getting Package J = Ubound (filebyte)' Merged Baotou for i = 0 to 7 Step 2 Temp = Temp & Chr (FileByte (i)) Next 'Compare Length Packed without Val (Temp) = j Then
Redim Mendbyte (J - 8) 'proposes Baotou for i = 0 to J - 8 MendByte (i) = filebyte (i 7) Next' write file PUT #FileNumber,, MendByte 'Send Continue to send the request frMmain.winsock0.senddata "OK" Else 'The packet loss is issued, requested to return frmmain.winsock0.senddata "no" end ifnd subpublic sub becomfile () filenumber = freefile' gets unused file number open "c: /aaa.exe" for binary as # FILENUMBER 'Opens File End Sub
Public Sub CloseFile () Close File Handle Close #filenumbend Sub
Option Explicitdim getFilenum As integeritdim lenfile as longdim sendbaye () AS BYTE 'Send package' ================================ udp file transfer 'author: shadow' server '================================
Private sub fascist1_click () getfilenum = freefile 'acquired unused file number lenfile = filelen ("d: /aa.rar")' gets the length of the file that needs to be transmitted open "D: /aa.rar" for binary as #getfilenum 'Open the file you need to transfer Command1.Enabled = false' Transfer file call tcpsendfile (frmmain.winsock0, getfilenum, splitfile) text1.text = nownd Sub
Private suform_load () frmmain.winsock0.remotehost = "192.168.0.12" Server ipfrmmain.winsock0.remoteport = 5698
End sub '==================================================================================================================================================================================== =========================== For clarity, the following uses two sub-processes to complete how many bytes of data can be passed. And transfer data '============================================== ===================================== private function splitfile () AS long 'Unpacking On Error ResMe Next Dim getcount as long' calculates this Sub-sending bytes if lenfile> = 4000 kiln getcount = 4000 lenfile = lenfile - getcount else getcount = lenfile lenfile = lenfile - getCount end if splitfile = getCount
End FunctionPrivate Sub TCPSendFile (objWinSock As Winsock, FileNumber As Integer, SendLen As Long) Dim FileByte () As Byte, i As Long, j As Long Dim temp As String ReDim Sendbaye (0) Dim tempa As String * 4 ReDim FileByte (SendLen - 1) TEMPA = sendlen 7 sendbaye = tempa 'Gets the length negative value to Baotou get #filenumber,, filebyte' read file redim preserve sendbaye (sendlen 7) 'put Baotou to file head for i = 0 to Ubound FileByte) Sendbaye (i 7) = FileByte (i) Next frmmain.Winsock0.SendData SendbayeEnd SubPrivate Sub Winsock0_DataArrival (ByVal bytesTotal As Long) Dim str As Stringfrmmain.Winsock0.GetData strSelect Case strCase "ok" 'successfully continue sending If LenFile = 0 THEN 'Send completed msgbox "success" exit sub hand tcpsendfile (frmmain.winsock0, getfilenum, splitfile) case "no" unsuccessful retransmission to a package frmmain.winsock0.sendData Sendbayeend Sub