2. 客户端程序(frmClient.frm) |
在窗体上建六个控件:一个名为 tcpClient的 Winsock控件用于通讯;一个名为txtIP 的 TextBox控件用于填写服务器的IP地址;一个名为ImgEdit1的 ImgEdit控件用于显示服务器传来的图像;三个CommandButton控件( cmdConnect、cmdGet_Pic和cmdDisconnect) 分别用于执行连接、取回图像和断开连接(见图二)。 |
|
客户端源代码如下: |
'====================== 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 |
|
客户端成功共享服务器端显示画面后的外观如图三所示。 |
|
|
二、共享声音 |
共享声音与共享显示的思想是一致的,只是这时是客户端向服务器端发送声音文件,以便共享服务器的声卡。服务器端应使用微软的多媒体控件(MMControl) 进行声音播放(使用 Ctrl+T 或菜单“工程->部件”来添加)。用该控件播放声音不仅是简单的,而且功能强大。 |
由于关键模块与共享显示一致,故此处略去源代码。 |