VB implement file transfer in the LAN

xiaoxiao2021-03-06  58

In order to design unity and user operation, we hope to integrate the server with the client to form a program so that the user understands, it is more intuitive (in fact, it is also convenient to debug, everyone can test it on this machine Pass the file to yourself). So, we need to use two WINSOCK controls in the program, one responsible for listening, one responsible for sending, when the sender is successful, then select a file to be sent (can be any binary), then file name and file byte The length is sent to the receiving end. After receiving this message, the file name and file length are resolved, and then the sending end can start sending the file; send the file to this message, send the file to receive the file. After receiving the end, the binary stream is written back, and the file is saved. Here, pay attention to the two points, one is due to the maximum content of 8K each time, so you need to decompose files, each transfer fixed number of byte streams, which can determine the process of file transmission based on this number every time you send and receive. Once the number of byte streams is equal to the size of the file, it is necessary to close the corresponding file handle; the other is because I only use a Winsock control to receive, you need to pay attention to the unicode transcoding, parse the identifiable information when receiving text.

Source code

'The following code is both a server and a client.

'Uses a replysence sending method

'Auto split file, including 2

Option expedition

'Private Declare Sub Sleep LIB "Kernel32" (Byval dwmilliseconds as ring)

DIM mybyte () as byte 'sending a number group

Const filecomesmsg = "a file is coming" "has file arrival

Const remoteisreadymsg = "sender is ready" is ready

Const fileiSovermsg = "the file is ended" file is completed

Const Remotedenymsg = "The user cande" "user cancels

Const filecountmsg = "the file length is" file length

Const receveVieisreadymsg = "receiver is ready" is ready to receive

DIM Arrdata () as byte 'received information

DIM Filesave as integer 'Save the handle of the file

DIM FileHandle As INTEGER 'Handle of Sending File

DIM FILESIZE AS DOUBLE '

DIM Sendbyte As Long

Dim ReceivebyTe As Long

DIM MYLOCATION AS DOUBLE

DIM mymsg as string 'message

Does the DIM FILESOVER AS BOOLEAN 'file have been completed

Const receificport = 7905

Const buffer_size = 5734

Private sub cmdconnect_click ()

Timer2.enabled = TRUE

End Sub

Private subcmdsend_click ()

ON Error Goto Errorhandlewith CommonDialog1

.Cancelerror = true

.Dialogtitle = "Select the file you want to transfer"

.Filter = "all files (*. *) | *. *"

.Showopen

End with

FileHandle = freefile

Open Commondialog1.FileName for Binary Access Read as #fileHandle

cmdsend.enabled = false

FileSize = CDBL (Filelen (Commondialog1.FileName)

Label1.caption = "Wait for the response >>>"

Msgbox ("Selected File Size" & Lof (FileHandle) & "Byte")

IF Winsocksend.State = Sckconnected Then

Winsocksend.sendData filecomesmsmsg & commondialog1.filename 'Send file information

END IF

EXIT SUB

ErrorHandle:

cmdsend.enabled = true

Msgbox ("You didn't choose a file!")

End Sub

Private sub flow_load ()

WinsockReceive.localPort = ReceivePort

WinsockReceive.Listen

FILESOVER = TRUE

Label1.caption = "Prepare Transport >>>"

End Sub

Public function sendchunk ()

DIM MyBytesize As Long

IF Winsocksend.State <> SCKCONNECTED THEN EXIT FUNCTION

MyBytesize = buffer_size

If LOF (FileHandle) - LOC (FileHandle)

Redim mybyte (0 to mybytesize - 1)

Get #filehandle, Mybyte

Winsocksend.senddata mybyte

SendByte = sendbyte mybytesize

ProgressBar1.Value = int ((100 / filesis) * sendbyte)

IF sendbyte> = filesize kil

FILESOVER = TRUE

Winsocksend.senddata fileiSovermsg

END IF

END FUNCTION

Private sub timer2_timer ()

IF Winsocksend.State = Sckconnected Then

Timer2.enabled = false

cmdconnect.enabled = false

Elseif Winsocksend.State <> 1 and winsocksend.State <> 6 and winsocksend.state <> 7 and winsocksend.state <> 8 and winsocksend.state <> 9 thenwinsocksend.connect txthost.text, receivedport

Elseif Winsocksend.State = 8 or Winsocksend.State = 9 THEN

Winsocksend.close

END IF

End Sub

Private Sub WinsockRecEceptRece_ConnectionRequest (Byval Requestid As Long)

If WinsockRecEive.State <> Sckclosed Ten WinsockRecEceptive.close

WinsockReceive.accept Requestid

End Sub

Private sub winsockreceive_dataarrival (byval bytestotal as long)

Redim Arrdata (0 to Bytestotal - 1)

WinsockReceive.getdata Arrdata, Vbbyte VBarray

Mymsg = strconv (arrdata, vbunicode) 'binary turn to string

SELECT CASE MID (Mymsg, 1, 17)

Case filecomesmsg 'These message senders and acceptors can receive

'Display Save Dialog

ON Error Goto Errorhandle

CommonDialog1.filename = MID (Mymsg, 17, Len (MyMSG))

CommonDialog1.dialogtitle = "Select the path to save the file"

CommonDialog1.showsave

Filesave = freefile

ReceiveByte = 0

cmdsend.enabled = false

WinsockReceive.senddata Recevieisreadymsg

Case fileiSovermsg

Close #filesave

Msgbox ("Document Transfer Success!") 'Everyone is processed

cmdconnect.enabled = true

cmdsend.enabled = true

Label1.caption = "Prepare Transport >>>"

ProgressBar1.Value = 0

WinsockReceive.senddata fileiSovermsg

WinsockRecEceptive.close

WinsockReceive.Listen

Case filecountmsg

FILESIZE = MID (MyMSG, 18, Len (MyMSG))

Open commitialog1.filename for binary access write #filesave

WinsockReceive.senddata Remoteisreadymsg

Label1.caption = "file ready to transfer!"

FILESOVER = FALSE

Case Else

IF receivebyte

ReceiveByte = ReceiveByte bytestotal

Put #filesave, ArrdatawinsockRecEth.senddata Remoteisreadymsg

ProgressBar1.Value = int ((100 / filesis) * ReceiveByte)

END IF

End SELECT

EXIT SUB

ErrorHandle:

WinsockReceive.senddata Remotedenymsg

cmdconnect.enabled = true

End Sub

Private sub winsocksend_dataarrival (Byval Bytestotal As Long)

Winsocksend.getdata mymsg

SELECT CASE MYMSG

Case Recevieisreadymsg

Winsocksend.senddata FileCountmsg & FileSize

FILESOVER = FALSE

Sendbyte = 0

Case Remoteisreadymsg

'If the file has not ended, continue to transfer

IF not fileisover

Label1.caption = "file is being transferred >>>"

Sendchunk

Else

Winsocksend.senddata fileiSovermsg

END IF

Case fileiSovermsg

'Host processing

Close #filehandle

Msgbox ("Document Transfer Success!") 'Everyone is processed

Winsocksend.senddata fileiSovermsg

Winsocksend.close

cmdconnect.enabled = true

ProgressBar1.Value = 0

cmdsend.enabled = true

Label1.caption = "Prepare Transport >>>"

Case Remotedenymsg

Msgbox ("User terminates the transfer!")

cmdsend.enabled = true

Label1.caption = "Prepare Transport >>>"

Close #filehandle

End SELECT

EXIT SUB

End Sub

This program is

WinXPSP1 VB6

with

Win2000sp4 VB6

The following debugging is successful.

转载请注明原文地址:https://www.9cbs.com/read-114403.html

New Post(0)