Client receives messages even after discoonected?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I set up an client to asynchronously receive data from it's connection.
When i'm finished, i close the client using

try
{
theClient.SetSocketOptionSocketOptionLevel.Socket,SocketOptionName.NoDelay,1);
theClient.Shutdown(SocketShutdown.Both);
theClient.Close();
theClient=null;
}

However, somehow the server seems to be able to send me messages !?
Does anyone have any idea how this is happening?
Thanks,
Shaun
 
Are you running it on a different thread? If you are then the client will
not Garbage collect until all it associated objects are marked to be
disposed. The client is still holding some reference that is not cleaned
up..

VJ
 
Yes i am running it on a separate thread to the main app.
I thought that marking it as null would cause the grabage collection.
... clearly not.
How should i go about ensuring it is removed immediately?

What state is the socket in at the end of the function ?- I would have
thought that the closing of the socket would do exactly that - but the new
data is still getting through !!??

Thanks,
Shaun
 
Where are you creating the socket? Main or in the secondary thread?, You
cant create the client in secondary thread, and clean up in main thread.
Well the code might work if you have it public and such, but Garbage
collection cannot happen...

it is going to be very hard for me to suggest a clean up idea, without
looking at the code. Can you give me a sketch of what you are doing in the
Main thread and in the secondary thread.. that will help us to see if we can
get some place.

VJ
 
VJ,
Thanks for you thoughts.
I have the following class structure

ClientManager
-AsynchronousClientSocket
--Socket (Mirosoft class)

following your thoughts i have changed the AsynchronousClientSocket.Stop()
class to be as follows:

theClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.NoDelay,1);
theClient.SetSocketOption(SocketOptionLevel.Socket,
System.Net.Sockets.SocketOptionName.DontLinger, 1); // added 160307
theClient.Shutdown(SocketShutdown.Both);
theClient.Close();
theClient=null;
state.workSocket=null; // added 160307
state=null;// added 160307

GC.Collect();
GC.WaitForPendingFinalizers();

Using a memory profiler, i can now see that the only link to the socket that
is still in existance is one from
OverlappedAsyncResult
that is referrenced from
AsyncCallback
that is referrenced from
Socket.BeginReceive

Does this help you to help me?
Thanks,
Shaun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top