Socket.BeginAccept() problem

S

scott

Hi all hope some one can help me with this prob because it is really
annoying me and I can't seem to solve it.

Just like to say thx to any one that can offer any help.

Ok the prob.

I have a server which accepts new connections using the Socket.BeginAccept()
function. This is done in its own thread using the following code.



while (ServerState)

{

serverSock.BeginAccept(new AsyncCallback(ConectionRecivedCallBack),
serverSock);

System.Threading.Thread.Sleep(100);

}



It passes in the server socket.

The function which is called from BeginAccept is called AcceptNewConnection
which takes in the System.IAsyncResult.

Within this function I obtain the server socket from the IASyncResult and
then use that socket to call EndAccept() to obtain a socket to the client.



Socket servSock = (Socket)asyncResult.AsyncState;
clntSock = servSock.EndAccept(asyncResult);



When running the code all is fine and works. However I started to see the
memory of my process starting to slowly creep up. I downloaded a demo
version of .net memory profiler to see if I could trace back the problem.



I tracked down the memory leek to System.Net.Sockets.AcceptAsyncResult.
According to .net memory profiler loads of these were being created and not
freed up.



I came to the conclusion that this was because for ever BeginAccept that was
being called there was not a EndAccept for all of them. Therefore I decide
to place a EndAccept call instead the while loop just after the BeginAccept.



I then ran .net memory profiler again and the memory leek was gone. However
I now had a problem where a user could not always connect to the socket.
Sometimes they could and sometimes they could not.



I guess what im asking is am I doing something wrong ?

Is there a correct way to do this ?

Does any one know what's going on ?



I hope you can understand my problem, I realise it is a bit wordy and im
sorry for that.



Thx for any help



Scott.
 
S

scott

Just to let you know iv solved the prob. I see what i was doing wrong now
and have solved it using a
System.Threading.ManualResetEvent
 

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

Top