客戶端源代碼如下:
'====================== 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 ' 進(jìn)行連接
End Sub
Private Sub cmdDisconnect_Click()
tcpClient.SendData "Close" ' 斷開(kāi)連接
cmdConnect.Enabled = True
cmdGet_Pic.Enabled = False
cmdDisconnect.Enabled = False
End Sub
Private Sub cmdGet_Pic_Click()
tcpClient.SendData "Save Picture" ' 請(qǐng)求圖像返回
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 ' 根據(jù)到達(dá)數(shù)據(jù)的字節(jié)數(shù)確定接收數(shù)組的大小
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 ' 打開(kāi)文件,準(zhǔn)備存儲(chǔ)圖像
FileLen = 0
tcpClient.SendData "Get Picture"
Exit Sub
End If
' 收到圖像發(fā)送完畢的信息
If bytesTotal = 2 And Chr(Buf(0)) = "E" And Chr(Buf(1)) = "F" Then
Close #FileID ' 關(guān)閉文件
j = DoEvents()
ImgEdit1.Image = FileName
ImgEdit1.Display ' 顯示收到的圖像
ImgEdit1.BurnInAnnotations 0, 2
frmClient.MousePointer = 0
Exit Sub
End If
' 收到一塊二進(jìn)制圖像信息
Put #FileID, , Buf ' 將當(dāng)前數(shù)據(jù)塊存盤(pán)
tcpClient.SendData "Next Block" ' 申請(qǐng)下一塊
FileLen = FileLen + bytesTotal
frmClient.Caption = "TCP Client " + Trim(Str(FileLen)) + _
" Bytes Received." ' 顯示當(dāng)前收到的字節(jié)數(shù)
End Sub
客戶端成功共享服務(wù)器端顯示畫(huà)面后的外觀如圖三所示
二、共享聲音
共享聲音與共享顯示的思想是一致的,只是這時(shí)是客戶端向服務(wù)器端發(fā)送聲音文件,以便共享服務(wù)器的聲卡。服務(wù)器端應(yīng)使用微軟的多媒體控件(MMControl) 進(jìn)行聲音播放(使用 Ctrl+T 或菜單“工程->部件”來(lái)添加)。用該控件播放聲音不僅是簡(jiǎn)單的,而且功能強(qiáng)大。
由于關(guān)鍵模塊與共享顯示一致,故此處略去源代碼。