F
foolnpl
Unregistered / Unconfirmed
GUEST, unregistred user!
要写一段代法实现这样的功能:每隔一段时间(时间间隔不能太长)给120(甚
至更多)个节点发送信息。为了提高效率,建立了若干(如12个)线程,每个
线程负责和10个节点顺次进行“连接--发送需要发出的信息--断开”。为了试
验这种做法是否可行,偶在先试了一个线程负责一个节点的情况,发送信息的节
点用clientsocket,接受信息的节点用serversocket,可是下面的代码总是无法
和接受信息的节点连接上;如果把tsendthread.execute的代码放在
tsendthread.create(...)中,则可以和接受信息的节点连接上。偶是DELPHI初学者,请各位大侠赐教。
unit thethread;{thethread.pas }
interface
uses
scktcomp,classes;
const threadnumber=3;
{使用到的线程数量}
const everythreadmax=5;{每个线程最多负责向everythreadmax个节点发送信息}
type
socketinfor=record
host: string;
{要连接的节点的host}
tosendstring :string;
{发给节点的信息}
end;
tsendthread=class(Tthread)
private
thinforarray: array[1..everythreadmax] of socketinfor;
thnum: integer;
{thnum纪录该线程要连接的节点数}
thclient_socket:tclientsocket;{该线程使用的Clientsocket对象}
procedure opensocket(var i:integer);
protected
procedure execute;override;
public
constructor create(var num: integer;
var tinforarray :array of socketinfor;var thdclient_socket: tclientsocket);
end;
var
clientnumber: array[1..threadnumber] of integer;
{clientnumber:第i个线程要连接的节点个数 }
mtinforarray: array[1..threadnumber,1..everythreadmax] of socketinfor;
clientsocketarray: array[1..threadnumber] of tclientsocket;
t1: tsendthread;
implementation
constructor tsendthread.create(var num: integer;
var tinforarray :array of socketinfor;var thdclient_socket: tclientsocket);
var
ei: integer;
begin
thnum:=num;
thclient_socket:=thdclient_socket;
for ei:=1 to thnumdo
begin
thinforarray[ei].host:=tinforarray[ei].host;
thinforarray[ei].tosendstring:=tinforarray[ei].tosendstring;
end;
freeonterminate:=true;
inherited create(false);
end;
procedure tsendthread.opensocket(var i:integer);
begin
if thClient_Socket.active then
thclient_socket.active:=false;
with thClient_Socketdo
begin
Host := thinforarray.host;
Active := True;
end;
end;
procedure tsendthread.execute;
var ei:integer;
begin
for ei:=1 to thnumdo
begin
thi:=ei;
opensocket(ei);
thclient_Socket.Socket.SendText(thinforarray[ei].tosendstring);
end;
end;
end.
//调用时{main.pas}
mtinforarray[1,1].host:='401b02';
mtinforarray[1,1].tosendstring:='Thread: NO.1,Client: NO.1';
clientsocketarray[1]:=clientsocket1;
clientnumber[1]:=1;
t1:=tsendthread.create(clientnumber[1],mtinforarray[1],clientsocketarray[1]);
至更多)个节点发送信息。为了提高效率,建立了若干(如12个)线程,每个
线程负责和10个节点顺次进行“连接--发送需要发出的信息--断开”。为了试
验这种做法是否可行,偶在先试了一个线程负责一个节点的情况,发送信息的节
点用clientsocket,接受信息的节点用serversocket,可是下面的代码总是无法
和接受信息的节点连接上;如果把tsendthread.execute的代码放在
tsendthread.create(...)中,则可以和接受信息的节点连接上。偶是DELPHI初学者,请各位大侠赐教。
unit thethread;{thethread.pas }
interface
uses
scktcomp,classes;
const threadnumber=3;
{使用到的线程数量}
const everythreadmax=5;{每个线程最多负责向everythreadmax个节点发送信息}
type
socketinfor=record
host: string;
{要连接的节点的host}
tosendstring :string;
{发给节点的信息}
end;
tsendthread=class(Tthread)
private
thinforarray: array[1..everythreadmax] of socketinfor;
thnum: integer;
{thnum纪录该线程要连接的节点数}
thclient_socket:tclientsocket;{该线程使用的Clientsocket对象}
procedure opensocket(var i:integer);
protected
procedure execute;override;
public
constructor create(var num: integer;
var tinforarray :array of socketinfor;var thdclient_socket: tclientsocket);
end;
var
clientnumber: array[1..threadnumber] of integer;
{clientnumber:第i个线程要连接的节点个数 }
mtinforarray: array[1..threadnumber,1..everythreadmax] of socketinfor;
clientsocketarray: array[1..threadnumber] of tclientsocket;
t1: tsendthread;
implementation
constructor tsendthread.create(var num: integer;
var tinforarray :array of socketinfor;var thdclient_socket: tclientsocket);
var
ei: integer;
begin
thnum:=num;
thclient_socket:=thdclient_socket;
for ei:=1 to thnumdo
begin
thinforarray[ei].host:=tinforarray[ei].host;
thinforarray[ei].tosendstring:=tinforarray[ei].tosendstring;
end;
freeonterminate:=true;
inherited create(false);
end;
procedure tsendthread.opensocket(var i:integer);
begin
if thClient_Socket.active then
thclient_socket.active:=false;
with thClient_Socketdo
begin
Host := thinforarray.host;
Active := True;
end;
end;
procedure tsendthread.execute;
var ei:integer;
begin
for ei:=1 to thnumdo
begin
thi:=ei;
opensocket(ei);
thclient_Socket.Socket.SendText(thinforarray[ei].tosendstring);
end;
end;
end.
//调用时{main.pas}
mtinforarray[1,1].host:='401b02';
mtinforarray[1,1].tosendstring:='Thread: NO.1,Client: NO.1';
clientsocketarray[1]:=clientsocket1;
clientnumber[1]:=1;
t1:=tsendthread.create(clientnumber[1],mtinforarray[1],clientsocketarray[1]);