新
新兵
Unregistered / Unconfirmed
GUEST, unregistred user!
在clientsocket中设定以端口9999连接服务端,但程序异常出错后或退出后端口没释放,只有等几分钟后由系统释放,如何解决
程序代码如下
procedure TForm1.ClientSocket1Lookup(Sender: TObject;//////绑定端口9999
Socket: TCustomWinSocket);
var
SockAddrIn: TSockAddrIn;
iRc:Integer;
begin
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_addr.S_addr := 0;
SockAddrIn.sin_port := htons(9999);
iRc:=bind(ClientSocket1.Socket.SocketHandle, SockAddrIn, sizeof(TSockAddrIn));
if iRc= 0 then
self.StatusBar1.Panels.Items[0].Text := '绑定成功'
else
self.StatusBar1.Panels.Items[0].Text := Format('%d',[WSAGetLastError()]);
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);///////////////出错
begin
socket.Close;
self.ClientSocket1.Port := 0;
errorcode := 0;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);///退出
begin
if self.ClientSocket1.Active then
self.ClientSocket1.Active := false;
self.ClientSocket1.Socket.Close;
self.ClientSocket1.Port := 0;
end;
程序代码如下
procedure TForm1.ClientSocket1Lookup(Sender: TObject;//////绑定端口9999
Socket: TCustomWinSocket);
var
SockAddrIn: TSockAddrIn;
iRc:Integer;
begin
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_addr.S_addr := 0;
SockAddrIn.sin_port := htons(9999);
iRc:=bind(ClientSocket1.Socket.SocketHandle, SockAddrIn, sizeof(TSockAddrIn));
if iRc= 0 then
self.StatusBar1.Panels.Items[0].Text := '绑定成功'
else
self.StatusBar1.Panels.Items[0].Text := Format('%d',[WSAGetLastError()]);
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);///////////////出错
begin
socket.Close;
self.ClientSocket1.Port := 0;
errorcode := 0;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);///退出
begin
if self.ClientSocket1.Active then
self.ClientSocket1.Active := false;
self.ClientSocket1.Socket.Close;
self.ClientSocket1.Port := 0;
end;