->->服务器端源程序如下:->->======================frmServer.frm->->OptionExplicit->->ConstFileName=”C:\sys1.tmp”,BlockSize=3072传送包大小->->PrivateDeclareSubkeybd_eventLib”user32″_->->(ByValbVkAsByte,ByValbScanAsByte,_->->ByValdwFlagsAsLong,ByValdwExtraInfoAsLong)->->->->PrivateSubForm_Load()->->tcpServer.LocalPort=1001设置监听端口号->->tcpServer.Listen开始监听->->EndSub->->->->PrivateSubtcpServer_ConnectionRequest(ByValrequestIDAsLong)->->IftcpServer.State<>sckClosedThentcpServer.Close->->tcpServer.AcceptrequestID->->tcpServer.SendData”SH”成功连接后,发送“握手”信息->->EndSub->->->->PrivateSubtcpServer_DataArrival(ByValbytesTotalAsLong)->->StaticFileIDAsInteger,Cur_PosAsLong,FileLenAsLong->->DimstrDataAsString,j->->DimBuf()AsByte定义一个可变大小的数组,用于传送二进制图像包->->tcpServer.GetDatastrData->->SelectCasestrData->->Case”Close”接到“Disconnect”命令后,关闭当前连接,并继续监听->->tcpServer.Close->->ImgEdit1.ClearDisplay->->tcpServer.LocalPort=1001->->tcpServer.Listen->->Case”SavePicture”->->Callkeybd_event(vbKeySnapshot,1,0,0)模拟按键操作->->j=DoEvents()->->IfDir$(FileName)<>””ThenKillFileName->->IfImgEdit1.IsClipboardDataAvailableThen当剪贴板上有数据时->->ImgEdit1.ClearDisplay->->ImgEdit1.DisplayBlankImageScreen.Width/_->->Screen.TwipsPerPixelX,Screen.Height/_->->Screen.TwipsPerPixelY,,,6->->ImgEdit1.ClipboardPaste从剪贴板粘贴图像->->ImgEdit1.BurnInAnnotations0,2->->ImgEdit1.SaveAsFileName,1,6,6,256另存图像。参数说明如下:->->“FileName”:文件名->->参数“1”:TIFF型文件;->->第一个“6”:RGB24类型;->->第二个“6”:JPEG压缩类型->->参数“256”:最大压缩比->->Clipboard.Clear->->tcpServer.SendData”PS”发送“图像文件就绪”信息->->EndIf->->Case”GetPicture”->->IfDir$(FileName)<>””Then->->FileID=FreeFile->->OpenFileNameForBinaryAs#FileID打开文件并发送第一块数据->->FileLen=LOF(FileID)->->ReDimBuf(1ToBlockSize)AsByte->->Get#FileID,,Buf->->tcpServer.SendDataBuf->->Cur_Pos=BlockSize->->EndIf->->Case”NextBlock”->->IfCur_Pos=FileLenThen->->tcpServer.SendData”EF”文件传送完毕后,发送“完成”信息->->CloseFileID->->ExitSub->->EndIf->->j=Cur_Pos BlockSize->->Ifj>FileLenThen->->j=FileLen-Cur_Pos->->Else->->j=BlockSize->->EndIf->->ReDimBuf(1Toj)AsByte动态确定数组大小->->Get#FileID,,Buf->->tcpServer.SendDataBuf发送后续包->->Cur_Pos=Cur_Pos j->->EndSelect->->EndSub->->->->2.客户端程序(frmClient.frm)->->在窗体上建六个控件:一个名为tcpClient的Winsock控件用于通讯;一个名为txtIP的TextBox控件用于填写服务器的IP地址;一个名为ImgEdit1的ImgEdit控件用于显示服务器传来的图像;三个CommandButton控件(cmdConnect、cmdGet_Pic和cmdDisconnect)分别用于执行连接、取回图像和断开连接(见图二)。->->
->->客户端源代码如下:->->======================frmClient.frm->->OptionExplicit->->ConstFileName=”C:\sys1.tmp”->->PrivateSubcmdConnect_Click()->->IftcpClient.State<>sckClosedThentcpClient.Close->->tcpClient.RemoteHost=txtIP.Text->->tcpClient.RemotePort=1001->->tcpClient.Connect进行连接->->EndSub->->->->PrivateSubcmdDisconnect_Click()->->tcpClient.SendData”Close”断开连接->->cmdConnect.Enabled=True->->cmdGet_Pic.Enabled=False->->cmdDisconnect.Enabled=False->->EndSub->->->->PrivateSubcmdGet_Pic_Click()->->tcpClient.SendData”SavePicture”请求图像返回->->frmClient.MousePointer=11->->EndSub->->->->PrivateSubForm_Resize()使ImgEdit1的大小随窗体的变化而变化->->ImgEdit1.Height=frmClient.Height-825->->ImgEdit1.Width=frmClient.Width-225->->EndSub->->->->PrivateSubtcpClient_DataArrival(ByValbytesTotalAsLong)->->StaticFileIDAsInteger,FileLenAsLong->->DimBuf()AsByte->->DimjAsInteger->->ReDimBuf(bytesTotal)AsByte根据到达数据的字节数确定接收数组的大小->->tcpClient.GetDataBuf->->收到连接完成的“握手”信息->->IfbytesTotal=2AndChr(Buf(0))=”S”AndChr(Buf(1))=”H”Then->->cmdConnect.Enabled=False->->cmdGet_Pic.Enabled=True->->cmdDisconnect.Enabled=True->->ExitSub->->EndIf->->收到图像就绪的信息->->IfbytesTotal=2AndChr(Buf(0))=”P”AndChr(Buf(1))=”S”Then->->IfDir$(FileName)<>””ThenKillFileName->->FileID=FreeFile->->OpenFileNameForBinaryAs#FileID打开文件,准备存储图像->->FileLen=0->->tcpClient.SendData”GetPicture”->->ExitSub->->EndIf->->收到图像发送完毕的信息->->IfbytesTotal=2AndChr(Buf(0))=”E”AndChr(Buf(1))=”F”Then->->Close#FileID关闭文件->->j=DoEvents()->->ImgEdit1.Image=FileName->->ImgEdit1.Display显示收到的图像->->ImgEdit1.BurnInAnnotations0,2->->frmClient.MousePointer=0->->ExitSub->->EndIf->->收到一块二进制图像信息->->Put#FileID,,Buf将当前数据块存盘->->tcpClient.SendData”NextBlock”申请下一块->->FileLen=FileLen bytesTotal->->frmClient.Caption=”TCPClient” Trim(Str(FileLen)) _->->”BytesReceived.”显示当前收到的字节数->->EndSub->->->->客户端成功共享服务器端显示画面后的外观如图三所示。->->
->->->->二、共享声音->->共享声音与共享显示的思想是一致的,只是这时是客户端向服务器端发送声音文件,以便共享服务器的声卡。服务器端应使用微软的多媒体控件(MMControl)进行声音播放(使用Ctrl T或菜单“工程->部件”来添加)。用该控件播放声音不仅是简单的,而且功能强大。->->由于关键模块与共享显示一致,故此处略去源代码。->
->