' frmClient.frm
Option Explicit
Const FileName = "C:\sys1.tmp"
Private Sub cmdConnect_Click()
If tcpClient.State <> sckClosed Then tcpClient.Close
tcpClient.RemoteHost = txtIP.Text
tcpClient.RemotePort = 1001
tcpClient.Connect ' 进行连接
End Sub
Private Sub cmdDisconnect_Click()
tcpClient.SendData "Close" ' 断开连接
cmdConnect.Enabled = True
cmdGet_Pic.Enabled = False
cmdDisconnect.Enabled = False
End Sub
Private Sub cmdGet_Pic_Click()
tcpClient.SendData "Save Picture" ' 请求图像返回
frmClient.MousePointer = 11
End Sub
Private Sub Form_Resize() ' 使 ImgEdit1 的大小随窗体的变化而变化
ImgEdit1.Height = frmClient.Height - 825
ImgEdit1.Width = frmClient.Width - 225
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Static FileID As Integer, FileLen As Long
Dim Buf() As Byte
Dim j As Integer
ReDim Buf(bytesTotal) As Byte ' 根据到达数据的字节数确定接收数组的大小
tcpClient.GetData Buf
' 收到连接完成的“握手”信息
If bytesTotal = 2 And Chr(Buf(0)) = "S" And Chr(Buf(1)) = "H" Then
cmdConnect.Enabled = False
cmdGet_Pic.Enabled = True
cmdDisconnect.Enabled = True
Exit Sub
End If
' 收到图像就绪的信息
If bytesTotal = 2 And Chr(Buf(0)) = "P" And Chr(Buf(1)) = "S" Then
If Dir$(FileName) <> "" Then Kill FileName
FileID = FreeFile
Open FileName For Binary As #FileID ' 打开文件,准备存储图像
FileLen = 0
tcpClient.SendData "Get Picture"
Exit Sub
End If
' 收到图像发送完毕的信息
If bytesTotal = 2 And Chr(Buf(0)) = "E" And Chr(Buf(1)) = "F" Then
Close #FileID ' 关闭文件
j = DoEvents() ImgEdit1.Image = FileName
ImgEdit1.Display ' 显示收到的图像
ImgEdit1.BurnInAnnotations 0, 2
frmClient.MousePointer = 0
Exit Sub
End If
' 收到一块二进制图像信息
Put #FileID, , Buf ' 将当前数据块存盘
tcpClient.SendData "Next Block" ' 申请下一块
FileLen = FileLen + bytesTotal
frmClient.Caption = "TCP Client " + Trim(Str(FileLen)) + _
" Bytes Received." ' 显示当前收到的字节数
End Sub
|