服务器端
Option Explicit
Dim fs
Dim SaveFile
Dim LngFilePack As Long
Dim LngNowPack As Long
Private Sub cmdCloseService_Click()
Unload Me
End Sub
Private Sub Form_Load()
Socket(0).LocalPort = 1259
Socket(0).Listen
lbl.Caption = "正在监听1259端口...."
StrFileName = "E:\program\数据交换模型\Server\File\基本数据库概念.zip"
StrFileLen = FileLen(StrFileName)
Call DividePacks(StrFileName, 3000)
DoEvents
Pause (2000)
'LngPacketSize = 7000
LngFilePack = LngChunkPacks
End Sub
Private Sub Socket_Close(Index As Integer)
Socket(Index).Close
Erase StrDataChunk
Erase LngPosition
Pause (2000)
End Sub
Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
Load Socket(Socket.Count)
Socket(Socket.Count - 1).LocalPort = 0
Socket(Socket.Count - 1).Accept requestID
End If
End Sub
Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'On Error GoTo Errorhandler
Dim VtData
Dim VarData
Dim SelectionPart As String
Dim I As Integer
Select Case SelectionPart
Case "QFILE"
Pause (200)
If Socket(Index).State <> sckConnected Then GoTo ErrorConn Else Socket(Index).SendData ("AGREE")
Pause (200)
Case "AGREE"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QNAME")
Pause (200)
Case "QNAME"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FNAME|" & StrFileName)
Pause (200)
Case "FNAME"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QLENG")
Pause (200)
Case "QLENG"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FLENG" & StrFileLen)
Pause (200)
Case "FLENG"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QPACK")
Pause (200)
Case "QPACK"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FPACK|" & CStr(LngFilePack))
Pause (200)
Case "FPACK"
Pause (200)
StrFilePack = Trim(Mid(VtData, 7, Len(VtData) - 6))
LngFilePack = Val(StrFilePack)
ReDim StrDataChunk(LngFilePack)
ReDim LngPosition(LngFilePack)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("QPCKL")
Pause (200)
Case "QPCKL"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("FPCKL|" & LngPacketSize)
Pause (200)
Case "FPCKL"
Pause (200)
LngPacketSize = Val(Trim(Mid(VtData, 7, Len(VtData) - 6)))
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("START")
Pause (200)
Case "START", "SNDNX"
Pause (200)
If LngFilePack > LngNowPack Then
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("DPACK|" & StrDataChunk(LngNowPack))
List1.AddItem Len(StrDataChunk(LngNowPack))
LngNowPack = LngNowPack + 1
Pause (200)
Else
Pause (400)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("CLOSE")
End If
Case "SNDPR"
Pause (200)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("DPACK|" & StrDataChunk(LngNowPack))
Pause (200)
Case "DPACK"
Pause (200)
If LngFilePack < UBound(StrDataChunk) Then
VarData = Mid(VtData, 7, Len(VtData) - 6)
If Len(VarData) = LngPacketSize Then
StrDataChunk(LngNowPack) = VarData
LngNowPack = LngNowPack + 1
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("SNDNX")
Pause (200)
Else
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("SNDPR")
Pause (200)
End If
Else
Pause (400)
If Socket(Index).State = sckConnected Then Socket(Index).SendData ("CLOSE")
End If
Case "CLOSE"
Pause (200)
Socket(Index).Close
Set fs = CreateObject("Scripting.FileSystemObject")
Set SaveFile = fs.CreateTextFile(App.Path & "\file\" & StrFileName, True)
For I = 0 To LngFilePack - 1
SaveFile.Write (StrDataChunk(I))
Next
SaveFile.Close
Set fs = Nothing
Set SaveFile = Nothing
End Select
Set VtData = Nothing
Set VarData = Nothing
Exit Sub
Private Sub Socket_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)
Socket(Index).Close
MsgBox "Err: [10.3.0.151] " & "[ " & Number & "] [" & Description & "]"
End Sub
Option Explicit
Dim fs
Dim SaveFile
Dim LngFilePack As Long
Dim LngNowPack As Long
Private Sub cmdSend_Click()
If Socket.State = sckConnected Then
MsgBox "连接建立成功!"
Socket.SendData ("QFILE")
Exit Sub
End If
Socket.Protocol = sckTCPProtocol
Socket.RemoteHost = "10.3.0.151"
Socket.RemotePort = 1259
Socket.Connect
If Socket.State = sckConnected Then MsgBox "连接建立成功!"
End Sub
Private Sub Form_Load()
'strfilename = "E:\Downloads\HappyEO3sc_setup_305.exe"
End Sub
Private Sub Socket_Close()
Socket.Close
Erase StrDataChunk
Erase LngPosition
Pause (2000)
End Sub
Private Sub Socket_DataArrival(ByVal bytesTotal As Long)
'On Error GoTo Errorhandler
Dim VtData
Dim VarData
Dim SelectionPart As String
Dim StrFilePack As String
Dim I As Integer
Select Case SelectionPart
Case "QFILE"
Pause (200)
If Socket.State <> sckConnected Then GoTo ErrorConn Else Socket.SendData ("AGREE")
Pause (200)
Case "AGREE"
Pause (200)
If Socket.State = sckConnected Then Socket.SendData ("QNAME")
Pause (200)
Case "QNAME"
Pause (200)
If Socket.State = sckConnected Then Socket.SendData ("FNAME|" & StrFileName)
Pause (200)
Case "FNAME"
Pause (200)
StrFileName = Mid(VtData, 7)
If Socket.State = sckConnected Then Socket.SendData ("QLENG")
Pause (200)
Case "QLENG"
Pause (200)
'If Socket.State = sckConnected Then Socket.SendData ("FLENG" & StrFileLen)
Pause (200)
Case "FLENG"
Pause (200)
StrFileLen = Mid(VtData, 7)
If Socket.State = sckConnected Then Socket.SendData ("QPACK")
Pause (200)
Case "QPACK"
Pause (200)
'If Socket.State = sckConnected Then Socket.SendData ("FPACK|" & StrFilePack)
Pause (200)
Case "FPACK"
Pause (200)
StrFilePack = Trim(Mid(VtData, 7, Len(VtData) - 6))
LngFilePack = Val(StrFilePack)
ReDim StrDataChunk(LngFilePack)
ReDim LngPosition(LngFilePack)
If Socket.State = sckConnected Then Socket.SendData ("QPCKL")
Pause (200)
Case "QPCKL"
Pause (200)
If Socket.State = sckConnected Then Socket.SendData ("FPACK|" & LngPacketSize)
Pause (200)
Case "FPCKL"
Pause (200)
LngPacketSize = Val(Trim(Mid(VtData, 7, Len(VtData) - 6)))
If Socket.State = sckConnected Then Socket.SendData ("START")
Pause (200)
Case "START", "SNDNX"
Pause (200)
If LngFilePack > LngNowPack Then
If Socket.State = sckConnected Then Socket.SendData ("DPACK|" & StrDataChunk(LngNowPack))
LngNowPack = LngNowPack + 1
Pause (200)
Else
Pause (400)
If Socket.State = sckConnected Then Socket.SendData ("CLOSE")
End If
Case "SNDPR"
If Socket.State = sckConnected Then Socket.SendData ("DPACK|" & StrDataChunk(LngNowPack))
Pause (200)
Case "DPACK"
Pause (200)
If LngFilePack > LngNowPack Then
VarData = Mid(VtData, 7, Len(VtData) - 6)
MsgBox Len(VarData) & " " & LngNowPack
If Len(VarData) = LngPacketSize Then
StrDataChunk(LngNowPack) = VarData
LngNowPack = LngNowPack + 1
If Socket.State = sckConnected Then Socket.SendData ("SNDNX")
lbl.Caption = Str(LngNowPack)
Pause (200)
Else
If Socket.State = sckConnected Then Socket.SendData ("SNDPR")
Pause (200)
End If
Else
Pause (400)
If Socket.State = sckConnected Then Socket.SendData ("CLOSE")
End If
Case "CLOSE"
Pause (200)
Socket.Close
Set fs = CreateObject("Scripting.FileSystemObject")
'Set SaveFile = fs.CreateTextFile(App.Path & "\file\" & StrFileName, True)
Set SaveFile = fs.CreateTextFile(App.Path & "\file\01.zip", True)
For I = 0 To LngFilePack - 1
SaveFile.Write (StrDataChunk(I))
Next
SaveFile.Close
Set fs = Nothing
Set SaveFile = Nothing
End Select
Set VtData = Nothing
Set VarData = Nothing
Exit Sub
Private Sub Socket_Error(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)
Socket.Close
MsgBox "Err: [" & txtHost.Text & "] " & "[ " & Number & "] [" & Description & "]"
End Sub
Declare Function GetTickCount Lib "kernel32" () As Long
Public LngChunkPackSize As Long '
Public LngPosition() As Long
Public StrDataChunk() As String
Public Authorizationstring As String
Public TaskIDToCheck As String
Public TimeToCheck As String
Public TaskTimeToCheck As String
Public StrKey As String
Public ControlString1 As String
Public BrokenFileName As String
Public ResumeFileName As String
Public BrokenFileLen As Long
Public ResumeFileLen As Long
Public ResumeFilePacket As String * 512
Public ResumeFilePacketPos As Long
Public BrokenFileNum As Long
Public ResRetval As Long
Public StrFileName As String
Public StrFileLen As String
Public StrFilePack As String
Public OptionsIP As String
Public OptionsTop As Long
Public OptionsLeft As Long
Public LngNextPack As Long
Public LngTotalPacks As Long
Public StrDistributedFileName As String
Public LngDistributedFileLength As Long
Public LngDistributedFileNum As Long
Public LngDistributedFileChunkPos As Long
Public StrDistributedFileDataChunk As String
Public StrDistributedFileBackUpName As String
Public StrDistributedFileSavingLocation As String
Public StrDistributedFileTargetLocation As String
Public StrDistributedFileSavingName As String
Public ScreenFileNum As Long
Public SendScreen As Boolean
Public ResumeTargetLocation As String
Public ResumeTargetFolder As String
Public ResumeFileName2 As String
Public KioskAppHwnd As Long
Public SckStatusRemoteIP As String
Public SckStatusRemotePort As String
Public GelenDosya As String
Public MAX_CHUNK As Long
Public bReplied As Boolean
Public lTIme As Long
Public LastRemoteIP As String
Public LngPacketSize As Long '每包的长度
Public LngChunkPacks As Long '拆分的包数量
Public LngLastChunkSize As Long '拆分后的余数
Sub Pause(HowLong As Long)
Dim u%, tick As Long
tick = GetTickCount()
Do
u% = DoEvents
Loop Until tick + HowLong < GetTickCount
End Sub
Public Function DividePacks(ByVal FileToDivide As String, ByVal PacketSize As Long)
Dim Position As Long
Dim TestRetval As Long
Dim LngSendFileNum As Long, LngFileSize As Long
'Dim StrChunkPacks
Dim lngIndex As Long
'On Error GoTo Errorhandler
If PacketSize = 0 Then PacketSize = 1024
StrFileName = FileToDivide
frmServer.lbl.Caption = StrFileName
LngSendFileNum = FreeFile() '得到空的文件号
Erase StrDataChunk '重载
Erase LngPosition '重载
'==================================================================
'取得文件大小,以指定大小拆分成包
LngFileSize = (FileLen(StrFileName))
LngPacketSize = PacketSize
If LngFileSize < LngPacketSize Then
LngChunkPacks = 1
ReDim StrDataChunk(LngChunkPacks)
ReDim LngPosition(LngChunkPacks)
Else
LngLastChunkSize = LngFileSize Mod LngPacketSize
If LngLastChunkSize = 0 Then
LngChunkPacks = LngFileSize / LngPacketSize
ReDim StrDataChunk(LngChunkPacks)
ReDim LngPosition(LngChunkPacks)
Else
'StrChunkPacks = LngFileSize / LngPacketSize
'StrChunkPacks = Left(StrChunkPacks, (InStr(1, StrChunkPacks, ",")) - 1)
LngChunkPacks = (LngFileSize \ LngPacketSize) + 1
ReDim StrDataChunk(LngChunkPacks)
ReDim LngPosition(LngChunkPacks)
End If
End If
'===================================================================
'拆分过程
Open StrFileName For Binary As LngSendFileNum
Close
'====================================================================
'校验包的完整性
If LngFileSize < LngPacketSize Then
TestRetval = Len(StrDataChunk(1))
If TestRetval = LngFileSize Then
Pause (200)
Else
Pause (200)
Exit Function
End If
Else
TestRetval = ((LngChunkPacks - 1) * LngPacketSize) + LngLastChunkSize
If TestRetval = LngFileSize Then
Pause (200)
Else
Pause (200)
Exit Function
End If
End If