请大家提点在serversocket中写多线程的经验.有源码更好!(100分)

  • 主题发起人 主题发起人 kevincsg
  • 开始时间 开始时间
Delphi目录下Demos/Threads/下的例子
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
procedure OnThreadEnd(Sender: TObject);
public
{ Public declarations }
end;

TSumThread = class(TThread)
private
FStart,FEnd:integer;
protected
procedure Execute; override;
public
sum:integer;
constructor Create(aStart, aEnd: integer;Suspended:boolean=false);
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{ TSumThread }

constructor TSumThread.Create(aStart, aEnd: integer;Suspended:boolean=false);
begin
FStart:=aStart;
FEnd:=aEnd;
inherited Create(Suspended);
end;

procedure TSumThread.Execute;
var i:integer;
begin
sum:=0;
for i:=FStart to Fend do
sum:=sum+i;
end;


procedure TForm1.OnThreadEnd(Sender: TObject);
begin
Memo1.lines.Add(IntToStr(TsumThread(Sender).Sum));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
aTh:array [0..3] of TSumThread;
i:integer;
begin
for i:=0 to 3 do begin
aTh:=TSumThread.Create(i*100+1,(i+1)*100,true);
aTh.FreeOnTerminate:=true;
aTh.OnTerminate:=OnThreadEnd;
ath.Resume;

end;
end;

end.
 
我是想知道在每当一个clientsocket连上来的时候,tserversocket自动生成一个线程来处理单独的
clientsocket.
 
请各位富翁指教!
 
我最近也在做关于Socket的程序,仅供参考,欢迎指教
TServerSocket设置成stThreadBlocking

{ TServerThread }

procedure TServerThread.ClientExecute;
var
Stream:TWinSocketStream;
TimeOut:integer;
begin
FreeOnTerminate:=true;
TimeOut:=ConfigRecord.AdvanceIniRecord.IdleTimeOut*1000;//空闲等待时间

while (not Terminated) and ClientSocket.Connected do
begin
try
Stream:=TWinSocketStream.Create(ClientSocket,TimeOut);
try
if Stream.WaitForData(TimeOut) then
begin
//ProcessClientSocket(Stream,Self);
end else
ClientSocket.Close;
finally
Stream.Free;
end;

except
ClientSocket.Close;
end;
end;
end;

procedure TMainFrm.ServerSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread:=TServerThread.Create(false,ClientSocket);
end;

procedure TMainFrm.WMClientThreadBegin(var Msg: TMessage);
begin
inc(ClientSocketCount);
SetStatusBar(ClientSocketCount);
end;

procedure TMainFrm.WMClientThreadEnd(var Msg: TMessage);
begin
dec(ClientSocketCount);
SetStatusBar(ClientSocketCount);
end;

{设置状态栏提示信息}
procedure TMainFrm.SetStatusBar(OnLineUser: integer);
begin
StatusBar1.Panels[1].Text:=Format('在线用户:%d',[OnLineUser]);
end;

{启动服务器}
procedure TMainFrm.RunServerSocket;
begin
with ServerSocket do
begin
Port:=ConfigRecord.AdvanceIniRecord.TcpIpPort;
ThreadCacheSize:=ConfigRecord.AdvanceIniRecord.CacheSize;
Active:=true;
end;
end;

procedure TMainFrm.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
SendMessage(MainFrm.Handle,WM_ClientThreadEnd,0,0);
end;

procedure TMainFrm.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
SendMessage(MainFrm.Handle,WM_ClientThreadBegin,0,0);
end;

procedure TServerThread.ProcessClientSocket(Stream: TWinSocketStream;
ServerClientThread: TServerClientThread);
var
SessionMsg:TSessionRecord;
ReceiveCount:integer;
begin
FillChar(SessionMsg,SizeOf(TSessionRecord),0);
ReceiveCount:=Stream.Read(SessionMsg,SizeOf(TSessionRecord));
if ReceiveCount>0 then
begin
case SessionMsg.SessionMessage of
SM_UserLogin:begin
ProcessUserLogin(Stream,ServerClientThread);
end;
end;
end;
end;
 
谢谢pcexplorer,请问有没有相关的资料,如果可以的话,能发至kevin-csg@163.com吗
 
请教 长笑水 一个问题
在你所给出的一段程序中
private
procedure OnThreadEnd(Sender: TObject);
是自己定义的私有过程,为什么会在线程结束时自动被系统调用?
此过程的参数(Sender: TObject);具体代表什么含义?
非常关注,也希望知道的朋友给出答案!
 
我用free方法结束正在运行的线程时为什么程序就没反应了?
为什么线程结束后其句柄仍然<>nil,怎么判断线程是否已经结束,
怎么立即释放正在运行的线程?
朝闻道,夕死可矣!
 
用ICS控件吧,里面有一个多线程的TCPSERVER例子,对你会有帮助的
 
多人接受答案了。
 
后退
顶部