关于delphi的serversocket动态创建 并监听多个端口的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 cnmir
  • 开始时间 开始时间
C

cnmir

Unregistered / Unconfirmed
GUEST, unregistred user!
网上找了点资料,但是还是出错了,哪位大哥能帮忙看看


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, D7ScktComp;
const
SNum = 10;

type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public

ServerSockets: array of TServerSocket;
procedure MyOnClientConnect(Sender: TObject; Socket: TCustomWinSocket);
{ Public declarations }
end;

var
Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
const SNum = 10;
var i : integer;
begin
ServerSockets := VarArrayCreate([0, SNum - 1], varVariant);
for i := 0 to SNum - 1 do
begin
ServerSockets := TServerSocket.Create(self);
with ServerSockets do
begin
Active := false;
Port := i;
OnClientConnect := MyOnClientConnect;
Active := true;
end;
end;
end;
procedure TForm1.MyOnClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
//how do you do???
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var i : integer;
begin
for i := 0 to SNum - 1 do
begin
ServerSockets.Active := false;
end;
end;



end.
 
for i := 0 to SNum - 1 do
begin
ServerSockets := TServerSocket.Create(self);
with ServerSockets do
begin
try
Active := false;
Port := i+5000;//大一点
OnClientConnect := MyOnClientConnect;
Active := true;
except
///端口被占用,换个端口
end;
end;
end;
 
好像不行哦,还是出异常了
好像是

ServerSockets := VarArrayCreate([0, SNum - 1], varVariant);
这一行就异常了 VarArrayCreate 有问题
 
有人还有好的法吗?
 
把:
ServerSockets: array of TServerSocket;
改为:
ServerSockets:Variant;
 
不行哦,
如果那样的话,那这个怎么办?serversocket不能定义成 Variant;
ServerSockets := TServerSocket.Create(self);
 
后退
顶部