请教用idtcpserver做服务器时,在客户端连接的情况下如何关闭服务器?老是会出错
请问用idtcpserver做服务器时,在客户端连接的情况下怎么关闭服务器?老是会出错
我用idtcpserver做服务器作了一个GPRS客户端连接的服务器,请问怎么在客户端连接的情况下关闭服务器?
我的disconnect事件的代码是
procedure TUDP_Client.IdTCPServerDisconnect(AThread: TIdPeerThread);
var
Client : TClient;
begin
{ Retrieve Client Record from Data pointer }
Client := Pointer(AThread.Data);
{ Remove Client from the Clients TList }
Clients.Delete(Client.ListLink);
{ Remove Client from the Clients List Box }
if ((Client.Addr <> 0) and (Client.Addr <> 1)) then
lbClients.Items.Delete(lbClients.Items.IndexOf(IntToStr(Client.Addr)))
else
lbClients.Items.Delete(lbClients.Items.IndexOf(Client.DNS));
{ Free the Client object }
Client.Free;
AThread.Data := nil;
end;
然后做了一个断开连接的按钮:
我做了一个断开连接的按钮
我的代码如下:
procedure TfrmMain.sbtnKillRTUClick(Sender: TObject);
var
Client : TClient;
begin
if (lbClients.ItemIndex <> -1) then
begin
Client := Clients.Items[lbClients.ItemIndex];
TIdPeerThread(Client.Thread).Connection.CheckForGracefulDisconnect;
TIdPeerThread(Client.Thread).Connection.Disconnect;
lbClients.Items.Delete(lbClients.ItemIndex);
Clients.Delete(lbClients.ItemIndex);
end;
end;
可是每次按这个断开按钮时总会跳出错误∶raise exception class EIdclosedsocket with message 'Disconnected '
或者list index out of bounds(-1)
或者告诉我not connected
请问这是怎么回事呢?到底应该怎样断开才不会出错呢?
菜鸟急问,请各位高手不吝赐教,谢谢!!!
------解决方案--------------------
最好不要用ItemIndex属性来控制,因为在网络中随时可能会出现各种情况,而如果你没有处理好任何一点,都有可能使lbClients里面的信息和实际的有差别
Client := Clients.Items[lbClients.ItemIndex];
就像你上面写的AThread.Data里面放了IP什么的,那么就也可以多放个名字啊,你选择一个用户断开后,还是循环找名字,就不会出现你说的情况了
另外,ondisconn事件里面你先删界面的,再remove(把delete换掉看看)
我用idtcpserver做服务器作了一个GPRS客户端连接的服务器,请问怎么在客户端连接的情况下关闭服务器?
我的disconnect事件的代码是
procedure TUDP_Client.IdTCPServerDisconnect(AThread: TIdPeerThread);
var
Client : TClient;
begin
{ Retrieve Client Record from Data pointer }
Client := Pointer(AThread.Data);
{ Remove Client from the Clients TList }
Clients.Delete(Client.ListLink);
{ Remove Client from the Clients List Box }
if ((Client.Addr <> 0) and (Client.Addr <> 1)) then
lbClients.Items.Delete(lbClients.Items.IndexOf(IntToStr(Client.Addr)))
else
lbClients.Items.Delete(lbClients.Items.IndexOf(Client.DNS));
{ Free the Client object }
Client.Free;
AThread.Data := nil;
end;
然后做了一个断开连接的按钮:
我做了一个断开连接的按钮
我的代码如下:
procedure TfrmMain.sbtnKillRTUClick(Sender: TObject);
var
Client : TClient;
begin
if (lbClients.ItemIndex <> -1) then
begin
Client := Clients.Items[lbClients.ItemIndex];
TIdPeerThread(Client.Thread).Connection.CheckForGracefulDisconnect;
TIdPeerThread(Client.Thread).Connection.Disconnect;
lbClients.Items.Delete(lbClients.ItemIndex);
Clients.Delete(lbClients.ItemIndex);
end;
end;
可是每次按这个断开按钮时总会跳出错误∶raise exception class EIdclosedsocket with message 'Disconnected '
或者list index out of bounds(-1)
或者告诉我not connected
请问这是怎么回事呢?到底应该怎样断开才不会出错呢?
菜鸟急问,请各位高手不吝赐教,谢谢!!!
------解决方案--------------------
最好不要用ItemIndex属性来控制,因为在网络中随时可能会出现各种情况,而如果你没有处理好任何一点,都有可能使lbClients里面的信息和实际的有差别
Client := Clients.Items[lbClients.ItemIndex];
就像你上面写的AThread.Data里面放了IP什么的,那么就也可以多放个名字啊,你选择一个用户断开后,还是循环找名字,就不会出现你说的情况了
另外,ondisconn事件里面你先删界面的,再remove(把delete换掉看看)