Board index » delphi » Socket.Connected not made false after closing socket

Socket.Connected not made false after closing socket

Hi,
Sorry for giving the wrong subject line.
It is "Socket.Connected" not made false after closing socket
NOT socket.active.

I am restating my problem here.

In a project, I am using Delphi Winsock component for implementing
socket communication.
TClientSocket object is used to connect to a remote listening socket.
(The scope of the project is limited to the client implementation ,
and not the server. And I have no visibility of the server software).

As per a particular requirement, I need to forcibly close the client
socket. This is triggering the DisconnectEvent and the corresponding
event handler is getting called. But in the Disconnect event handler,
the socket is again closed only if socket.connected is true. The code
segment is as follows:

within a custom routine (where I am forcibly closing the client
socket)
-----------------------------------------------------------------------
TestFrm.TestClient.socket.close;

within Disconnect event handler
-------------------------------
    if(Testfrm.TestClient.socket.connected)then
       TestFrm.TestClient.socket.close;

But this always caused an exception 10038 (WSAENOTSOCK: An operation
was attempted on something that is not a socket), which means I am
trying to close a socket which is already closed. I am sure, this is
happening within the Disconnect event handler. i.e. socket.connected
is not false even after closing the socket from the custom routine.

I tried modifying the disconnect event handler as follows

     if(Testfrm.TestClient.Active)then
       TestFrm.TestClient.socket.close;

and this gave no error since "Active" becomes false on socket.close.
But, whether (Testfrm.TestClient.Active) serves the same purpose as
Testfrm.TestClient.socket.connected) at least in this requirement.

Thankyou.

Shehan

 

Re:Socket.Connected not made false after closing socket


Hi,
Sorry for giving the wrong subject line.
It is "Socket.Connected" not made false after closing socket
NOT socket.active.

I am restating my problem here.

In a project, I am using Delphi Winsock component for implementing
socket communication.
TClientSocket object is used to connect to a remote listening socket.
(The scope of the project is limited to the client implementation ,
and not the server. And I have no visibility of the server software).

As per a particular requirement, I need to forcibly close the client
socket. This is triggering the DisconnectEvent and the corresponding
event handler is getting called. But in the Disconnect event handler,
the socket is again closed only if socket.connected is true. The code
segment is as follows:

within a custom routine (where I am forcibly closing the client
socket)
-----------------------------------------------------------------------
TestFrm.TestClient.socket.close;

within Disconnect event handler
-------------------------------
    if(Testfrm.TestClient.socket.connected)then
       TestFrm.TestClient.socket.close;

But this always caused an exception 10038 (WSAENOTSOCK: An operation
was attempted on something that is not a socket), which means I am
trying to close a socket which is already closed. I am sure, this is
happening within the Disconnect event handler. i.e. socket.connected
is not false even after closing the socket from the custom routine.

I tried modifying the disconnect event handler as follows

     if(Testfrm.TestClient.Active)then
       TestFrm.TestClient.socket.close;

and this gave no error since "Active" becomes false on socket.close.
But, whether (Testfrm.TestClient.Active) serves the same purpose as
Testfrm.TestClient.socket.connected) at least in this requirement.

Thankyou.

Shehan

Re:Socket.Connected not made false after closing socket


Quote
> In a project, I am using Delphi Winsock component for implementing
> socket communication.
> TClientSocket object is used to connect to a remote listening socket.
> (The scope of the project is limited to the client implementation ,
> and not the server. And I have no visibility of the server software).

> As per a particular requirement, I need to forcibly close the client
> socket. This is triggering the DisconnectEvent and the corresponding
> event handler is getting called. But in the Disconnect event handler,
> the socket is again closed only if socket.connected is true. The code
> segment is as follows:

This problem is caused because the OnDisconnect event is called before the
socket is fully set to disconnected.

The way I get around this problem is to defer the disconnect event using the
"PostMessage" function from the OnDisconnect event and set up a message
handler in my code. I know it's messy but it allows a little time for all
the clearing up to occur so you should be able to test the state of the
socket more reliably.

for example

const
  CM_Disconnect = WM_USER + 1;// Define a message

type tMyForm = class(Tform);
[foo]
public
   procedure CMDisconnect(var Message : TMessage); message CM_Disconnect;//
message handler
end;

procedure tMyForm .CMDisconnect(var Message: TMessage);
begin

  // Now there has been a short delay after the ondisconnect event and all
the code
  // should have finished clearing up
  Do your OnDisconnect code in here

end;

procedure tMyForm .ClientSocket1DisconnectEvent(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  // Dont do anything in here except post yourself a message so that
handling is deferred for a few milliseconds.
  PostMessage(Self.Handle, CM_Disconnect,0,0);

end;

--
Claire Humphrey
Software Engineer

Re:Socket.Connected not made false after closing socket


Quote
> The way I get around this problem is to defer the disconnect event using
the
> "PostMessage" function from the OnDisconnect event and set up a message
> handler in my code. I know it's messy but it allows a little time for all
> the clearing up to occur so you should be able to test the state of the
> socket more reliably.

why do you need to test the state at all?
Connected and Active don't indicate the actual state of the socket.
What happen is:

1)Active is set to false
2)OnDisconnect is called
3)Connected is set to false.

The socket is closed before that. So, no matter if you test for Active in
the OnDisconnect handler or if you do it your way and check for Connected,
the result will always be the same, false. If you want a real check, use the
socket handle and winsock to get the state.

Andy

Re:Socket.Connected not made false after closing socket


Quote
Andy M. <1...@123.com> wrote in message news:3d88ca60@newsgroups.borland.com...

> > The way I get around this problem is to defer the disconnect event using
> the
> > "PostMessage" function from the OnDisconnect event and set up a message
> > handler in my code. I know it's messy but it allows a little time for all
> > the clearing up to occur so you should be able to test the state of the
> > socket more reliably.

> why do you need to test the state at all?
> Connected and Active don't indicate the actual state of the socket.
> What happen is:

While you're all discussing this, does anyone know how to disconnect a
blocking TclientSocket in a thread?   At the moment it's bodged - I've set a
short read/waitForData timeout & check a 'disconnectNow' boolean in the
thread subclass.  It's safe, it  works, but it's slow - I have to wait for the
timeout before the thread notices it has to disconnect.

I've tried setting active to false, setting connected to false,  freeing the
WinSocketStream, the socket etc. - always get an AV when called from
the main thread..  There must be an easy way.

Rgds,
Martin

Other Threads