各位大大您好!
小弟目前試寫個網路程式,出現錯誤!不知是寫法有誤,還是觀念有誤?
程式碼如下:
//server端!
#include
#pragma hdrstop #include "Unit1.h"
struct test
{
int command;
TMemoryStream *ms;
};
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ServerSocket1->Port = 9999;
ServerSocket1->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
test com;
Graphics::TBitmap *bit=new Graphics::TBitmap;
Socket->ReceiveBuf(&com,sizeof(test));
Edit1->Text = com.command;
bit->LoadFromStream((TStream*)com.ms);
Image1->Picture->Assign(bit);
}
//---------------------------------------------------------------------------
//client端!
#include
#pragma hdrstop #include "Unit1.h"
/* Bmp轉成jpeg */
#include "jpeg.hpp"
struct test
{
int command;
TMemoryStream *ms;
};
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ClientSocket1->Address = "127.0.0.1";
ClientSocket1->Port = 9999;
ClientSocket1->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
test aa;
//創造一暫時圖檔
Graphics::TBitmap *Bitmap=new Graphics::TBitmap;
//設定暫時圖檔屬性
Bitmap->Width = Screen->Width;
Bitmap->Height = Screen->Height;
Bitmap->PixelFormat = pf24bit; //取得桌面DC handle
HDC NewDC=::GetWindowDC(0); //將全螢幕複製至Bitmap上
::BitBlt(Bitmap->Canvas->Handle,0,0,Screen->Width,Screen->Height,NewDC,0,0,SRCCOPY); /* 取得滑鼠位置 */
POINT pt;
HCURSOR hCur=GetCursor();
GetCursorPos(&pt);
DrawIcon(Bitmap->Canvas->Handle, pt.x, pt.y, hCur); /* 將BitBlt壓縮成jpeg */
TJPEGImage *jpg=new TJPEGImage;
jpg->Assign(Bitmap);
jpg->CompressionQuality=70;
jpg->Compress();
jpg->SaveToStream((TStream*)aa.ms);
aa.command = 1;
ClientSocket1->Socket->SendBuf(&aa,sizeof(aa)); //釋放DC
ReleaseDC(Form1->Handle,NewDC);
//刪除Bitmap
delete Bitmap;
delete jpg;
}
錯誤如下: