客户端 Dim strFile As String Dim buffer() As Byte Dim lBytes As LongPrivate Sub CmdBrowse_Click() CommonDialog1.ShowOpen strFile = CommonDialog1.FileName End SubPrivate Sub CmdSend_Click() CmdSend.Enabled = False lBytes = 0 ReDim buffer(FileLen(strFile) - 1) Open strFile For Binary As 1 Get #1, 1, buffer Close #1 'Load Winsock1 Winsock1.RemoteHost = "server" Winsock1.RemotePort = 1001 Winsock1.Connect LblState.Caption = "connecting...." End SubPrivate Sub Form_Load()End SubPrivate Sub Winsock1_Close() LblState.Caption = "connection closed" End SubPrivate Sub Winsock1_Connect() LblState.Caption = "connected" Winsock1.SendData buffer End SubPrivate Sub Winsock1_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) MsgBox Description, vbOKOnly, "error" RichTextBox1.Text = HelpFile End SubPrivate Sub Winsock1_SendComplete() LblState = "send complete" 'Unload Winsock1 CmdSend.Enabled = True End SubPrivate Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long) lBytes = lBytes + bytesSent LblState.Caption = lBytes & " out of " & UBound(buffer) & " bytes sent" End Sub 服务器端 Dim lPos As Long Dim lDataSend As Long Dim intDataArrivalTimes As Integer Public bFinished As BooleanPrivate Sub Cmdrun_Click() If Cmdrun.Caption = "run" Then Cmdrun.Caption = "stop" Wstcp.LocalPort = 1001 Wstcp.Listen lDataSend = 0 Else Wstcp.Close Close #1 Cmdrun.Caption = "run" End If End SubPrivate Sub Wstcp_Close() Close #1 End SubPrivate Sub Wstcp_ConnectionRequest(ByVal requestID As Long) If Wstcp.State <> sckClosed Then Wstcp.CloseWstcp.Accept requestID intDataArrivalTimes = 1 If Dir("e:\try\aaa.jpg") <> "" Then Kill "e:\try\aaa.jpg" Open "e:\try\aaa.jpg" For Binary As 1 lPos = 1 End SubPrivate Sub Wstcp_DataArrival(ByVal bytesTotal As Long) 'ReDim buffer(1 To bytesTotal) As Byte Dim strFile As String Dim strFileName As String Dim intI As Integer Dim buffer() As Byte Wstcp.GetData buffer Put #1, lPos, buffer lPos = lPos + UBound(buffer) + 1 End If End SubPrivate Sub Wstcp_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) MsgBox Description, vbOKOnly, "error" End Sub
Dim strFile As String
Dim buffer() As Byte
Dim lBytes As LongPrivate Sub CmdBrowse_Click()
CommonDialog1.ShowOpen
strFile = CommonDialog1.FileName
End SubPrivate Sub CmdSend_Click()
CmdSend.Enabled = False
lBytes = 0
ReDim buffer(FileLen(strFile) - 1)
Open strFile For Binary As 1
Get #1, 1, buffer
Close #1
'Load Winsock1
Winsock1.RemoteHost = "server"
Winsock1.RemotePort = 1001
Winsock1.Connect
LblState.Caption = "connecting...."
End SubPrivate Sub Form_Load()End SubPrivate Sub Winsock1_Close()
LblState.Caption = "connection closed"
End SubPrivate Sub Winsock1_Connect()
LblState.Caption = "connected"
Winsock1.SendData buffer
End SubPrivate Sub Winsock1_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)
MsgBox Description, vbOKOnly, "error"
RichTextBox1.Text = HelpFile
End SubPrivate Sub Winsock1_SendComplete()
LblState = "send complete"
'Unload Winsock1
CmdSend.Enabled = True
End SubPrivate Sub Winsock1_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
lBytes = lBytes + bytesSent
LblState.Caption = lBytes & " out of " & UBound(buffer) & " bytes sent"
End Sub
服务器端
Dim lPos As Long
Dim lDataSend As Long
Dim intDataArrivalTimes As Integer
Public bFinished As BooleanPrivate Sub Cmdrun_Click()
If Cmdrun.Caption = "run" Then
Cmdrun.Caption = "stop"
Wstcp.LocalPort = 1001
Wstcp.Listen
lDataSend = 0
Else
Wstcp.Close
Close #1
Cmdrun.Caption = "run"
End If
End SubPrivate Sub Wstcp_Close()
Close #1
End SubPrivate Sub Wstcp_ConnectionRequest(ByVal requestID As Long)
If Wstcp.State <> sckClosed Then Wstcp.CloseWstcp.Accept requestID
intDataArrivalTimes = 1
If Dir("e:\try\aaa.jpg") <> "" Then Kill "e:\try\aaa.jpg"
Open "e:\try\aaa.jpg" For Binary As 1
lPos = 1
End SubPrivate Sub Wstcp_DataArrival(ByVal bytesTotal As Long)
'ReDim buffer(1 To bytesTotal) As Byte
Dim strFile As String
Dim strFileName As String
Dim intI As Integer
Dim buffer() As Byte
Wstcp.GetData buffer
Put #1, lPos, buffer
lPos = lPos + UBound(buffer) + 1
End If
End SubPrivate Sub Wstcp_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)
MsgBox Description, vbOKOnly, "error"
End Sub