服务器2.4 内存1G
我给出我的代码,请帮忙看看:
unit untMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, IdTCPConnection, IdTCPClient, IdBaseComponent,
IdComponent, IdTCPServer, StdCtrls, uReceiveThread, uSendThread, ExtCtrls;
type
TfrmMain = class(TForm)
edtSendNum: TEdit;
lbl1: TLabel;
edtReceiveNum: TEdit;
lbl2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
CS: TRTLCriticalSection;
ReceiveThread: TReceiveThread;
SendThread: TSendThread;
public
{ Public declarations }
end;
var
ComandList: TStringList;
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
//
InitializeCriticalSection(CS); //初始化临界区
ComandList := TStringList.Create;
ReceiveThread := TReceiveThread.Create(False); //建立接收线程
SendThread := TSendThread.Create(False); //建立发送线程
end;
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ComandList.Free;
end;
end.
-------------------------------
unit uReceiveThread; //用于监听端口,接收客户端命令
interface
uses
Classes, Sockets, Windows, Forms, SysUtils;
var
CS:TRTLCriticalSection; //定义临界区
type
TReceiveThread = class(TThread)
private
{ Private declarations }
TcpServer: TTcpServer; //监听和接收信息
procedure OnAccept(Sender: TObject; ClientSocket: TCustomIpClient);
procedure UpdateCaption;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
end;
implementation
uses untMain;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TReadThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TReadThread }
constructor TReceiveThread.Create(CreateSuspended: Boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TReceiveThread.Execute;
begin
InitializeCriticalSection(CS); //初始化临界区
TcpServer := TTcpServer.Create(nil); //创建TcpServer
TcpServer.LocalPort := '8990';
TcpServer.LocalHost := '192.168.1.7';
TcpServer.Active := True;
TcpServer.OnAccept := OnAccept;
{ Place thread code here }
end;
procedure TReceiveThread.OnAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
begin
with ClientSocket do
begin
EnterCriticalSection(CS);
try
ComandList.Add(ClientSocket.RemoteHost+'='+ClientSocket.Receiveln());
Synchronize(UpdateCaption);
finally
LeaveCriticalSection(CS);
end;
end;
end;
procedure TReceiveThread.UpdateCaption;
begin
frmMain.edtReceiveNum.Text := IntToStr(StrToInt(frmMain.edtReceiveNum.Text)+1);
end;
end.
----------------------------------
unit uSendThread; //用于打包向客户端发送命令
interface
uses
Classes, Sockets, Windows, SysUtils;
var
CS:TRTLCriticalSection; //定义临界区
type
TSendThread = class(TThread)
private
{ Private declarations }
TCPClient: TTCPClient;
procedure UpdateCaption;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
end;
implementation
uses untMain;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TSendThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TSendThread }
constructor TSendThread.Create(CreateSuspended: Boolean);
begin
FreeOnTerminate := True;
InitializeCriticalSection(CS); //初始化临界区
TCPClient := TTCPClient.Create(nil); //创建TcpServer
inherited Create(CreateSuspended);
end;
procedure TSendThread.Execute;
const
WAIT_TIME=100;
var
sComand: string;
Count: Integer;
begin
while (WaitForSingleObject(Handle, WAIT_TIME) = WAIT_OBJECT_0) or (not Terminated) do
begin
EnterCriticalSection(CS);
try
Count := ComandList.Count;
if ComandList.Count>0 then
begin
sComand := ComandList.Names[0];
ComandList.Delete(0);
end;
finally
LeaveCriticalSection(CS);
end;
if Count>0 then
with TCPClient do
begin
RemoteHost := sComand;
RemotePort := '8989';
try
if Connect then
begin
//命令处理区
Sendln('LOGON');
//EnterCriticalSection(CS);
try
//frmMain.edtSendNum.Text := IntToStr(StrToInt(frmMain.edtSendNum.Text)+1);
Synchronize(UpdateCaption);
finally
//LeaveCriticalSection(CS);
end;
end;
finally
Disconnect;
end;
end;
end;
{ Place thread code here }
end;
procedure TSendThread.UpdateCaption;
begin
frmMain.edtSendNum.Text := IntToStr(StrToInt(frmMain.edtSendNum.Text)+1);
end;
end.
请给出详细答案!!!!