一个运行很慢的SOCKET线程源码,请各位看看。 |
答題得分者是:yhzxj
|
wnhoo
高階會員 ![]() ![]() ![]() ![]() 發表:75 回覆:443 積分:198 註冊:2003-04-22 發送簡訊給我 |
我有一个运行很慢的SOCKET线程源码,请各位看看。 const_code.pas
//***********
unit const_code;
interface
var
userinfostr:string; const server_code='_001';
const conn_buffer_clear='_000';
const conn_login='_001';
const conn_login_user_get='_101';
const conn_login_user_reg='_201';
const conn_login_user_check='_301';
const conn_test='_002';
const conn_scr='_003';
const conn_file='_004';
const conn_text='_005';
const conn_cmd='_006';
implementation
end.
//#####################################
unit2.pas
//****************************
unit Unit2; interface uses
Classes, StdCtrls, SysUtils, ScktComp,const_code, Dialogs; type
TMyServerThread = class(TServerClientThread)
private
FMemo: TMemo;
FListBox,FListBox2,FListBox3,FListBox4, FListBox5, FListBox6 : TListBox;
Buffer: string;
buffersize:integer; protected
procedure ClientExecute; override;
procedure DoDisplayBuffer;
public
constructor Create(Memo: TMemo; listbox2,listbox3,listbox4,listbox5,listbox6,ListBox: TListBox; CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
end; implementation
{ TMyServerThread } constructor TMyServerThread.Create(Memo: TMemo;listbox2,listbox3,listbox4,listbox5,listbox6,ListBox: TListBox; CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
begin
inherited Create(CreateSuspended,ASocket);
FMemo := Memo;
FListBox := ListBox;
FListBox2 :=ListBox2;
FListBox3 :=ListBox3;
FListBox4 :=ListBox4;
FListBox5 :=ListBox5;
FListBox6 :=ListBox6;
end; procedure TMyServerThread.DoDisplayBuffer();
var
con: Integer;
begin
//在MEMO中显示客户端发送的信息
FMemo.Lines.Add('线程ID<' IntToStr(threadID)
'>远程客户端<' ClientSocket.RemoteHost ':'
IntToStr(ClientSocket.RemotePort) '> : ' Buffer '<' datetimetostr(now()) '>' );
//将收到的消息发给每个客户端
if buffersize> 3 then
for con:=0 to ClientSocket.ServerWinSocket.ActiveConnections-1 do
ClientSocket.ServerWinSocket.Connections[con].SendText(copy(buffer,5,buffersize-4) '<' datetimetostr(now()) '>'); end;
procedure TMyServerThread.ClientExecute;
var
Stream : TWinSocketStream;
n:integer;
begin
//程序正在运行 ,并且CLIENTSOCKET是活动的
while (not Terminated) and ClientSocket.Connected do
begin
try
Stream := TWinSocketStream.Create(ClientSocket, 60000);
try
//填充BUFFER
// FillChar(Buffer,sizeof(buffer),0); if Stream.WaitForData(60000) then
if buffersize=0 then ClientSocket.Close
else
begin
buffersize:=length(Buffer);
//缓冲清除
if copy(Buffer,1,4)=conn_buffer_clear then
Stream.CleanupInstance ;
//检测合法
if copy(Buffer,1,4)=conn_login_user_check then
begin
fListBox2.Items.Add(ClientSocket.RemoteHost);
fListBox3.Items.Add(ClientSocket.RemoteAddress) ;
fListBox4.Items.Add(inttostr(ClientSocket.RemotePort));
fListBox5.Items.Add(inttostr(threadID));
fListBox6.Items.Add(copy(buffer,5,buffersize-4));
Buffer:=conn_login_user_check '<消息服务系统>:用户【' copy(Buffer,5,buffersize-4) '】已经上线。';
buffersize:=length(Buffer);
end;
//获取当前用户信息
if copy(Buffer,1,4)=conn_login_user_get then
begin
userinfostr:='';
for n:=0 to fListBox6.Count-1 do
userinfostr:=userinfostr '【' fListBox6.Items.Strings[N] '】';
Buffer:=conn_login_user_get userinfostr;
buffersize:=length(Buffer);
end;
//成功登陆
if copy(Buffer,1,4)=conn_login then ; //发信息
synchronize(DoDisplayBuffer);
end;
finally
Stream.Free;
end;
except
HandleException;
end;
end;
end; end.
------
风花雪月 e梦情缘 |
yhzxj
一般會員 ![]() ![]() 發表:3 回覆:3 積分:6 註冊:2003-04-22 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |