不熟悉网络编成,将原码贴出: Option ExplicitPrivate Sub cmdClear_Click() txtView = "" txtFileName = "" End SubPrivate Sub cmdClose_Click() Unload Me End SubPrivate Sub cmdSend_Click() Dim FName_Only As String
If txtFileName = "" Then MsgBox "No file selected to send...", vbCritical Else If frmServer.tcpServer.State <> sckClosed Then FName_Only$ = GetFileName(txtFileName) SendFile FName_Only$ End If End If End Sub Private Sub Form_Load() tcpServer.LocalPort = Port tcpServer.Listen bInconnection = False Status "Listening.... (Not Connected)" End SubPrivate Sub Form_Unload(Cancel As Integer) SendData "ServerClosed," Pause 500 tcpServer.Close End End Sub Private Sub tcpServer_Close() If tcpServer.State <> sckClosed Then tcpServer.Close Form_Load End SubPrivate Sub tcpServer_ConnectionRequest(ByVal requestID As Long) On Error GoTo IDERROR If tcpServer.State <> sckClosed Then tcpServer.Close tcpServer.Accept requestID bInconnection = True Status "Listening... Connected." SendData "Accepted," Exit Sub IDERROR: MsgBox Err.Description, vbCritical End SubPrivate Sub tcpServer_DataArrival(ByVal bytesTotal As Long) ' Dim Command As String Dim NewArrival As String Dim Data As String Static DataCnt As Long
Select Case Command$ Case "OpenFile" Dim Fname As String Fname$ = App.Path & "\SourceData\ini" & "\System_.doc" Open Fname$ For Binary As #1 Status "File opened.... " & Data$ Case "CloseFile" Close #1 Status "File Transfer complete..." Pause 3000 Status "Listening... (Connected)" Case Else Put #1, , NewArrival$ DataCnt& = DataCnt& + 1 Status "Recieving Data... " & (MAX_CHUNK * DataCnt&) & " bytes" End Select End SubPrivate Sub cmdBrowse_Click() cdOpen.ShowOpen If Not vbCancel Then txtFileName = cdOpen.FileName End If End Sub怎样用2进制来写?
着是模块中: Option Explicit Declare Function GetTickCount Lib "kernel32" () As Long Public Const Port = 1256 Public Const MAX_CHUNK = 4169 Public bInconnection As BooleanSub Pause(HowLong As Long) Dim u%, tick As Long tick = GetTickCount()
Do u% = DoEvents Loop Until tick + HowLong < GetTickCount End Sub Sub SendFile(Fname As String) Dim DataChunk As String Dim passes As Long SendData "OpenFile," & Fname$ Pause 200 Open Fname$ For Binary As #1
Do While Not EOF(1) passes& = passes& + 1 DataChunk$ = Input(MAX_CHUNK, #1) SendData DataChunk$ Status "Transfering... " & (MAX_CHUNK * passes&) & " bytes" frmServer.txtView = frmServer.txtView & DataChunk$ Pause 200 DoEvents Loop SendData "CloseFile," Status "Listening..... (Connected)" passes& = 0 Close #1 End SubSub SendData(sData As String) On Error GoTo ErrH Dim TimeOut As Long frmServer.tcpServer.SendData sData Do Until (frmServer.tcpServer.State = 0) Or (TimeOut < 10000) DoEvents TimeOut = TimeOut + 1 If TimeOut > 10000 Then Exit Do Loop
ErrH: Exit Sub End Sub Function GetFileName(Fname As String) As String Dim i As Integer Dim tempStr As String
For i% = 1 To Len(Fname$) tempStr$ = Right$(Fname$, i%)
If Left$(tempStr$, 1) = "\" Then GetFileName$ = Mid$(tempStr$, 2, Len(tempStr$)) Exit Function End If Next i End Function Public Function EvalData(sIncoming As String, iRtLt As Integer, _ Optional sDivider As String) As String Dim i As Integer Dim tempStr As String Dim sSplit As String
If sDivider = "" Then sSplit = "," Else sSplit = sDivider End If Select Case iRtLt
Case 1 For i = 0 To Len(sIncoming) tempStr = Left(sIncoming, i)
If Right(tempStr, 1) = sSplit Then EvalData = Left(tempStr, Len(tempStr) - 1) Exit Function End If Next
Case 2 For i = 0 To Len(sIncoming) tempStr = Right(sIncoming, i)
If Left(tempStr, 1) = sSplit Then EvalData = Right(tempStr, Len(tempStr) - 1) Exit Function End If Next End Select
End FunctionPublic Sub Status(Msg As String) frmServer.lblStatus = " Status : " & Msg$ End Sub
另外,你传送的其他二进制文件都正常吗?传传jpg之类的文件看看。
文件传输过程中建议添加md5编码之类的校验,保证数据的完整和正确
Option ExplicitPrivate Sub cmdClear_Click()
txtView = ""
txtFileName = ""
End SubPrivate Sub cmdClose_Click()
Unload Me
End SubPrivate Sub cmdSend_Click()
Dim FName_Only As String
If txtFileName = "" Then
MsgBox "No file selected to send...", vbCritical
Else
If frmServer.tcpServer.State <> sckClosed Then
FName_Only$ = GetFileName(txtFileName)
SendFile FName_Only$
End If
End If
End Sub
Private Sub Form_Load()
tcpServer.LocalPort = Port
tcpServer.Listen
bInconnection = False
Status "Listening.... (Not Connected)"
End SubPrivate Sub Form_Unload(Cancel As Integer)
SendData "ServerClosed,"
Pause 500
tcpServer.Close
End
End Sub
Private Sub tcpServer_Close()
If tcpServer.State <> sckClosed Then tcpServer.Close
Form_Load
End SubPrivate Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
On Error GoTo IDERROR
If tcpServer.State <> sckClosed Then tcpServer.Close
tcpServer.Accept requestID
bInconnection = True
Status "Listening... Connected."
SendData "Accepted,"
Exit Sub
IDERROR:
MsgBox Err.Description, vbCritical
End SubPrivate Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
'
Dim Command As String
Dim NewArrival As String
Dim Data As String
Static DataCnt As Long
tcpServer.GetData NewArrival$, vbString
Command = EvalData(NewArrival$, 1)
Data$ = EvalData(NewArrival$, 2)
Select Case Command$
Case "OpenFile"
Dim Fname As String
Fname$ = App.Path & "\SourceData\ini" & "\System_.doc"
Open Fname$ For Binary As #1
Status "File opened.... " & Data$
Case "CloseFile"
Close #1
Status "File Transfer complete..."
Pause 3000
Status "Listening... (Connected)"
Case Else
Put #1, , NewArrival$
DataCnt& = DataCnt& + 1
Status "Recieving Data... " & (MAX_CHUNK * DataCnt&) & " bytes"
End Select
End SubPrivate Sub cmdBrowse_Click()
cdOpen.ShowOpen
If Not vbCancel Then
txtFileName = cdOpen.FileName
End If
End Sub怎样用2进制来写?
Option Explicit
Declare Function GetTickCount Lib "kernel32" () As Long
Public Const Port = 1256
Public Const MAX_CHUNK = 4169
Public bInconnection As BooleanSub Pause(HowLong As Long)
Dim u%, tick As Long
tick = GetTickCount()
Do
u% = DoEvents
Loop Until tick + HowLong < GetTickCount
End Sub
Sub SendFile(Fname As String)
Dim DataChunk As String
Dim passes As Long SendData "OpenFile," & Fname$
Pause 200
Open Fname$ For Binary As #1
Do While Not EOF(1)
passes& = passes& + 1
DataChunk$ = Input(MAX_CHUNK, #1)
SendData DataChunk$
Status "Transfering... " & (MAX_CHUNK * passes&) & " bytes"
frmServer.txtView = frmServer.txtView & DataChunk$
Pause 200
DoEvents
Loop
SendData "CloseFile,"
Status "Listening..... (Connected)"
passes& = 0
Close #1
End SubSub SendData(sData As String)
On Error GoTo ErrH
Dim TimeOut As Long
frmServer.tcpServer.SendData sData
Do Until (frmServer.tcpServer.State = 0) Or (TimeOut < 10000)
DoEvents
TimeOut = TimeOut + 1
If TimeOut > 10000 Then Exit Do
Loop
ErrH:
Exit Sub
End Sub
Function GetFileName(Fname As String) As String
Dim i As Integer
Dim tempStr As String
For i% = 1 To Len(Fname$)
tempStr$ = Right$(Fname$, i%)
If Left$(tempStr$, 1) = "\" Then
GetFileName$ = Mid$(tempStr$, 2, Len(tempStr$))
Exit Function
End If
Next i
End Function
Public Function EvalData(sIncoming As String, iRtLt As Integer, _
Optional sDivider As String) As String
Dim i As Integer
Dim tempStr As String
Dim sSplit As String
If sDivider = "" Then
sSplit = ","
Else
sSplit = sDivider
End If
Select Case iRtLt
Case 1
For i = 0 To Len(sIncoming)
tempStr = Left(sIncoming, i)
If Right(tempStr, 1) = sSplit Then
EvalData = Left(tempStr, Len(tempStr) - 1)
Exit Function
End If
Next
Case 2
For i = 0 To Len(sIncoming)
tempStr = Right(sIncoming, i)
If Left(tempStr, 1) = sSplit Then
EvalData = Right(tempStr, Len(tempStr) - 1)
Exit Function
End If
Next
End Select
End FunctionPublic Sub Status(Msg As String)
frmServer.lblStatus = " Status : " & Msg$
End Sub