?? recvstream.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop
#include "RecvStream.h"
#include "ChildWin.h"
#include "Socket.cpp"
#include "Ping.cpp"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TRecvStreamThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TRecvStreamThread::TRecvStreamThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
bool __fastcall TRecvStreamThread::LoadImage(TImage *Image1)
{
CPingReply reply;
CPing PingHost;
bool bRtn = false; // 函數(shù)返回值初始為FALSE
MainForm->StatusBar->SimpleText = "正在連接主機...";
// 先PING主機,檢測網(wǎng)絡(luò)是否暢通
bool rtn = PingHost.Ping(RemoteAddress.c_str(),reply,64,4000,32);
if (rtn) {
u_short RecvPort=0;
int RecvSocket = BindSocket(&RecvPort); // 動態(tài)分配接收端口
if (RecvSocket) {
// 將接收端口和色深、圖象品質(zhì)合成一條命令,參數(shù)之間以'\n'分隔
AnsiString Msg = IntToStr(RecvPort) + "\n" +
IntToStr(CL) + "\n" +
IntToStr(CQ) + "\n";
// 向遠程主機發(fā)送命令
if (SendMsg(RemoteAddress,LISTENPORT,Msg)) {
TMemoryStream *Stream;
try {
// 定義一個數(shù)據(jù)流并分配內(nèi)存
Stream = new TMemoryStream;
MainForm->StatusBar->SimpleText = "正在接收數(shù)據(jù)...";
Application->ProcessMessages(); // 處理系統(tǒng)消息
// 開始接收圖象到數(shù)據(jù)流中
if (RecvStream(RecvSocket,Stream)) {
TJPEGImage *jpeg; // 定義JPEG圖象
try {
jpeg = new TJPEGImage; // 分配內(nèi)存
// 從數(shù)據(jù)流中載入圖象
jpeg->LoadFromStream(Stream);
// 顯示圖象
Image1->Picture->Bitmap->Assign(jpeg);
MessageBeep(MB_OK); // 發(fā)出提示聲音
// 返回值為TRUE,表示成功
bRtn = true;
}
__finally {
delete jpeg; // 釋放資源
}
}
else
MessageBox(0,"接收數(shù)據(jù)流失敗","冰河",MB_ICONERROR);
}
__finally {
delete Stream; // 釋放資源
}
}
else
MessageBox(0,("無法與主機'"+ RemoteAddress +"'建立連接").c_str(),
"冰河",MB_ICONERROR);
}
else
MessageBox(0,"分配端口失敗,無法繼續(xù)接收數(shù)據(jù)","冰河",MB_ICONERROR);
}
else
MessageBox(0,("主機'"+RemoteAddress+"'沒有響應(yīng)").c_str(),"冰河",MB_ICONERROR);
return bRtn;
}
void __fastcall TRecvStreamThread::Execute()
{
//---- Place thread code here ----
if (LoadImage(RemoteScreen))
MainForm->StatusBar->SimpleText = "數(shù)據(jù)接收完畢";
else
MainForm->StatusBar->SimpleText = "接收數(shù)據(jù)失敗";
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -