VB解释代码
Option Explicit
Private Enum MyState
stClose = 0
stConnected = 1
stDataRequest = 2
End Enum
Private WskState As MyState
Private Sub Form_Load()
Me.Show
Winsock1.Connect "127.0.0.1", "7878"
WskState = stClose
End Sub
Private Sub Winsock1_Connect()
Print "链接成功"
WskState = stConnected
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Dim r
Dim d() As Byte
Static Flen As Long
Static TransLen As Long
Dim fname As String
Select Case WskState
Case stConnected
Winsock1.GetData s
r = Split(s, Chr(0))
If r(0) = "FILE" And UBound(r) = 3 Then
Flen = Val(r(2))
fname = r(1)
While InStr(fname, "\\") > 0
fname = Mid(fname, InStr(fname, "\\") + 1)
Wend
CommonDialog1.FileName = fname
On Error GoTo Ers '错误执行ERS
CommonDialog1.CancelError = True
CommonDialog1.ShowSave
On Error GoTo 0
Open CommonDialog1.FileName For Output As #1
Close #1
Open CommonDialog1.FileName For Binary As #1
Winsock1.SendData "FILEOK"
WskState = stDataRequest
TransLen = 0
End If
Case stDataRequest
Winsock1.GetData d(), vbArray + vbByte, bytesTotal
TransLen = TransLen + bytesTotal
If TransLen >= Flen Then
Dim i As Long
For i = 0 To Flen Mod bytesTotal
Put #1, , d(i)
Next i
WskState = stConnected
Winsock1.SendData "DATAOK"
Close #1
Else
Put #1, , d
Winsock1.SendData "PACKETOK"
End If
Case Else
Winsock1.GetData s
Print "Unknown reuqest :" & s
End Select
Exit Sub
Ers: '按下取消
Winsock1.SendData "CANCEL" '发送识别码
Print "拒绝传输"
End Sub
Private 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)
Winsock1.Close
Call Form_Load
End Sub
|
请高手将上面代码做一个全部解释
答得好有得加分!
答案:Option Explicit '变量必须先声明后使用
Private Enum MyState '定义枚举类型MyState
stClose = 0
stConnected = 1
stDataRequest = 2
End Enum
Private WskState As MyState '定义WskState 为枚举类型MyState
Private Sub Form_Load() '窗体加载
Me.Show '显示窗体
Winsock1.Connect "127.0.0.1", "7878" '连接127.0.0.1(本机)的7878端口
WskState = stClose 'MyState设为stClose(0)-未连接
End Sub
Private Sub Winsock1_Connect() '建立连接时
Print "链接成功"
WskState = stConnected 'MyState设为stClose(1)-已连接
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) '收到数据
Dim s As String '存储数据
Dim r '字符串数组,用以分割s
Dim d() As Byte
Static Flen As Long '长度.注:Static:静态变量,其不随Sub结束而释放
Static TransLen As Long
Dim fname As String '名称
Select Case WskState '判断状态
Case stConnected '如果已连接
Winsock1.GetData s '获取数据至s
r = Split(s, Chr(0)) '以Chr(0)标记分割s至字符串数组r
If r(0) = "FILE" And UBound(r) = 3 Then '如果r(0)为"FILE"且s中有4个Chr(0)--即符合预定义格式
Flen = Val(r(2)) '长度=r(2)的值
fname = r(1) '名称=r(1)
While InStr(fname, "\\") > 0 '寻找"\\"符号
fname = Mid(fname, InStr(fname, "\\") + 1) 'fname<--"\\"右边的内容
Wend
CommonDialog1.FileName = fname '通用对话框的文件名显示为fname
On Error GoTo Ers '设置错误陷阱,错误执行ERS
CommonDialog1.CancelError = True '忽略错误
CommonDialog1.ShowSave '显示"保存文件" 窗口
On Error GoTo 0 '取消上面设置的错误陷阱
Open CommonDialog1.FileName For Output As #1 '创建文件
Close #1 '本行及上行代码多余,可删.
Open CommonDialog1.FileName For Binary As #1 '以二进制方式打开文件
Winsock1.SendData "FILEOK" '发送数据"FILEOK"
WskState = stDataRequest '设置连接标记
TransLen = 0 '长度为0
End If
Case stDataRequest '如果标记为stDataRequest(可以译作"请求数据"吗???)
Winsock1.GetData d(), vbArray + vbByte, bytesTotal '获取数据
TransLen = TransLen + bytesTotal '设置长度
If TransLen >= Flen Then '如本长度<文件长度
Dim i As Long
For i = 0 To Flen Mod bytesTotal '循环写入文件
Put #1, , d(i) '写入文件
Next i
WskState = stConnected '设置连接标记
Winsock1.SendData "DATAOK" '发送数据"DATAOK"
Close #1 '关闭文件
Else '否则
Put #1, , d '将d()写入文件
Winsock1.SendData "PACKETOK" '发送数据"PACKETOK"
End If
Case Else '如果标记为stClose
Winsock1.GetData s '获取数据至s
Print "Unknown reuqest :" & s '未知请求:s
End Select
Exit Sub
Ers: '按下取消
Winsock1.SendData "CANCEL" '发送识别码
Print "拒绝传输"
End Sub
Private 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) '连接出错
Winsock1.Close '关闭连接
Call Form_Load '重建连接
End Sub
上一个:用递归与非递归来编写C++汉诺塔程序
下一个:vb常用函数