搞清楚了,是我2000服务器性能太高,传到PC上的数据控制端处理不过来,一查控制端CPU占用90%多,而服务端CPU仅占用30%多.导致服务端与控制端不同步.这么说要把控制端代码修改一下,但我确找不出在哪下手,请
[red]guanyueguan和QSmile[/red]帮忙加个工或者提示一下.我把代码贴上,要整个源代码请给个邮箱,我一并奉上.
unit Unit1;
{*******************************************}
{ 图片传输客户端程序 }
{ 作者:guanyueguan }
{*******************************************}
//unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ZLIBEX, WSocket, Buttons;
const
DEF_BUFFER = 4096;
type
TCapCmd = record
Cmd: Byte;
Size: Integer;
Width: Integer;
Height: Integer;
end;
PBaseCmd = ^TBaseCmd;
TBaseCmd = record
Cmd: Byte;
X, Y: Integer;
Button: TMouseButton;
end;
PRecCmd = ^TRecCmd;
TRecCmd = record
PakHead: Boolean;
StartTime: Cardinal;
FramCount: Cardinal;
Rate: Cardinal;
FramSize: Integer;
RecSize: Integer;
RecBuf: array[0..DEF_BUFFER - 1] of Char;
end;
TfrmMain = class(TForm)
//pnlA: TPanel;
sbA: TScrollBox;
pbA: TPaintBox;
lblA: TLabel;
wsA: TWSocket;
chkA: TCheckBox;
btnConnect: TSpeedButton;
btnDisconnect: TSpeedButton;
btnWin: TSpeedButton;
pnlA: TPanel;
procedure btnConnectClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure pbAPaint(Sender: TObject);
procedure btnDisconnectClick(Sender: TObject);
procedure wsASessionConnected(Sender: TObject; ErrCode: Word);
procedure wsASessionClosed(Sender: TObject; ErrCode: Word);
procedure wsADataAvailable(Sender: TObject; ErrCode: Word);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure btnWinClick(Sender: TObject);
procedure pbAMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure pbADblClick(Sender: TObject);
procedure pbAMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure pbAMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FScrBmp: Graphics.TBitmap;
FRecStream: TMemoryStream;
FScrStream: TMemoryStream;
FRecBmp: Graphics.TBitmap;
FCCmd: TCapCmd;
FRCmd: TRecCmd;
FCmd: TBaseCmd;
FScrCmd: TBaseCmd;
FCmdBuf: array[0..SizeOf(TCapCmd) - 1] of Char;
FRect: TRect;
FHost: string;
//
procedure ShowScr;
procedure ShowRate;
procedure SendCmd(ACmd: TBaseCmd);
public
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
FScrBmp := Graphics.TBitmap.Create;
FRecBmp := Graphics.TBitmap.Create;
FRecStream := TMemoryStream.Create;
FScrStream := TMemoryStream.Create;
FScrCmd.Cmd := 0;
FHost := '192.168.0.1';
end;
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FScrBmp.Free;
FRecBmp.Free;
FRecStream.Free;
FScrStream.Free;
wsA.Close;
end;
procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
if InputQuery('Host', 'Please Input Host:', FHost) and (StrLen(PChar(FHost)) > 0) then
begin
try
wsA.Addr := FHost;
wsA.Port := '9009';
wsA.Connect;
except on e: Exception do
lbla.Caption := e.Message;
end;
end;
end;
procedure TfrmMain.btnDisconnectClick(Sender: TObject);
begin
wsA.Close;
FRecStream.Clear;
FScrStream.Clear;
btnConnect.Enabled := true;
btnDisconnect.Enabled := false;
end;
procedure TfrmMain.wsASessionConnected(Sender: TObject; ErrCode: Word);
begin
if ErrCode <> 0 then
begin
ShowMessage('Connect Error!');
btnConnect.Enabled := true;
btnDisconnect.Enabled := false;
end
else
begin
btnConnect.Enabled := false;
btnDisconnect.Enabled := true;
FRCmd.StartTime := GetTickCount;
FRCmd.FramCount := 0;
FRCmd.PakHead := true;
end;
end;
procedure TfrmMain.wsASessionClosed(Sender: TObject; ErrCode: Word);
begin
btnConnect.Enabled := True;
btnDisconnect.Enabled := False;
lblA.Caption := 'Connect Closed!';
end;
procedure TfrmMain.wsADataAvailable(Sender: TObject; ErrCode: Word);
var
nLen: Integer;
begin
try
if FRCmd.PakHead then
begin
nLen := wsA.Receive(@FCmdBuf[0], sizeof(TCapCmd));
if nLen = sizeof(TCapCmd) then
begin
Move(FCmdBuf[0], FCCmd, sizeof(TCapCmd));
if FCCmd.Cmd = 1 then
begin
FScrBmp.Width := FCCmd.Width;
FScrBmp.Height := FCCmd.Height;
FRCmd.PakHead := True;
end
else if FCCmd.Cmd = 2 then
begin
FRCmd.FramSize := FCCmd.Size;
FRCmd.RecSize := 0;
FRecStream.Clear;
FRecStream.SetSize(FRCmd.FramSize);
FRCmd.PakHead := False;
end;
end;
Exit;
end;
if FRCmd.FramSize <= 0 then
begin
ShowRate;
FRCmd.PakHead := True;
Exit;
end;
if FRCmd.FramSize - FRCmd.RecSize > DEF_BUFFER then
nLen := DEF_BUFFER
else
nLen := FRCmd.FramSize - FRCmd.RecSize;
nLen := wsA.Receive(@FRCmd.RecBuf[0], nLen);
if nLen > 0 then
begin
FRecStream.WriteBuffer(FRCmd.RecBuf, nLen);
Inc(FRCmd.RecSize, nLen);
if FRCmd.RecSize >= FRCmd.FramSize then
begin
FScrStream.Clear;
FRecStream.Position := 0;
ZDecompressStream(FRecStream, FScrStream);
//FScrStream.SaveToFile('1.bmp');
FScrStream.Position := 0;
try
while FScrStream.Position < FScrStream.Size do
begin
FScrStream.Read(FRect, sizeof(TRect));
FRecBmp.Width := FRect.right - FRect.left;
FRecBmp.Height := FRect.bottom - FRect.top;
FRecBmp.LoadFromStream(FScrStream);
BitBlt(FScrBmp.Canvas.Handle, FRect.left, FRect.top, FRecBmp.Width, FRecBmp.Height, FRecBmp.Canvas.Handle, 0, 0, SRCCOPY);
end;
ShowScr;
ShowRate;
except on e: Exception do
lblA.Caption := e.Message;
end;
FRCmd.FramSize := 0;
FRCmd.RecSize := 0;
FRCmd.PakHead := True;
end;
end;
except on e: Exception do
lblA.Caption := e.Message;
end;
end;
procedure TfrmMain.pbAPaint(Sender: TObject);
begin
ShowScr();
end;
procedure TfrmMain.ShowScr;
begin
if (pbA.Width <> FScrBmp.Width) or (pba.Height <> FScrBmp.Height) then
begin
pbA.Left := 0;
pbA.Top := 0;
pbA.Width := FScrBmp.Width;
pbA.Height := FScrBmp.Height;
ClientWidth := FScrBmp.Width;
ClientHeight := FScrBmp.Height + pnlA.Height;
end;
BitBlt(pbA.Canvas.Handle, 0, 0, FScrBmp.Width, FScrBmp.Height, FScrBmp.Canvas.Handle, 0, 0, SRCCOPY);
end;
procedure TfrmMain.ShowRate;
begin
Inc(FRCmd.FramCount);
FRCmd.Rate := FRCmd.FramCount * 1000 div (GetTickCount - FRCmd.StartTime);
lblA.Caption := IntToStr(FRCmd.FramCount) + '/' + IntToStr(FRCmd.Rate);
frmMain.Caption:=inttostr(FRecStream.Size);
end;
procedure TfrmMain.SendCmd(ACmd: TBaseCmd);
begin
if chkA.Checked and (wsA.State = wsConnected) then
wsA.Send(@ACmd, SizeOf(TBaseCmd));
end;
procedure TfrmMain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
FCmd.Cmd := 10;
FCmd.X := Key;
//FCmd.Y := Shift;
SendCmd(FCmd);
end;
procedure TfrmMain.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
FCmd.Cmd := 11;
FCmd.X := Key;
//FCmd.Y := Shift;
SendCmd(FCmd);
end;
procedure TfrmMain.btnWinClick(Sender: TObject);
begin
FCmd.Cmd := 8;
SendCmd(FCmd);
end;
procedure TfrmMain.pbAMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
FCmd.Cmd := 1;
FCmd.X := X;
FCmd.Y := Y;
SendCmd(FCmd);
end;
procedure TfrmMain.pbADblClick(Sender: TObject);
begin
if FCmd.Button = mbLeft then
FCmd.Cmd := 6
else
FCmd.Cmd := 7;
SendCmd(FCmd);
end;
procedure TfrmMain.pbAMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FCmd.Button := Button;
FCmd.X := X;
FCmd.Y := Y;
if FCmd.Button = mbLeft then
FCmd.Cmd := 2
else
FCmd.Cmd := 4;
SendCmd(FCmd);
end;
procedure TfrmMain.pbAMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FCmd.Button := Button;
FCmd.X := X;
FCmd.Y := Y;
if FCmd.Button = mbLeft then
FCmd.Cmd := 3
else
FCmd.Cmd := 5;
SendCmd(FCmd);
end;
end.