Socket.BeginAccept() problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scott

    Socket.BeginAccept() problem

    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.BeginAcc ept()
    function. This is done in its own thread using the following code.



    while (ServerState)

    {

    serverSock.Begi nAccept(new AsyncCallback(C onectionRecived CallBack),
    serverSock);

    System.Threadin g.Thread.Sleep( 100);

    }



    It passes in the server socket.

    The function which is called from BeginAccept is called AcceptNewConnec tion
    which takes in the System.IAsyncRe sult.

    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)asyncRe sult.AsyncState ;
    clntSock = servSock.EndAcc ept(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.Sock ets.AcceptAsync Result.
    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.






  • scott

    #2
    Re: Socket.BeginAcc ept() problem

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




    "scott" <scottamillard@ hotmail.com> wrote in message
    news:d9mntk$jec $1@newsg3.svr.p ol.co.uk...[color=blue]
    > 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[/color]
    Socket.BeginAcc ept()[color=blue]
    > function. This is done in its own thread using the following code.
    >
    >
    >
    > while (ServerState)
    >
    > {
    >
    > serverSock.Begi nAccept(new AsyncCallback(C onectionRecived CallBack),
    > serverSock);
    >
    > System.Threadin g.Thread.Sleep( 100);
    >
    > }
    >
    >
    >
    > It passes in the server socket.
    >
    > The function which is called from BeginAccept is called[/color]
    AcceptNewConnec tion[color=blue]
    > which takes in the System.IAsyncRe sult.
    >
    > 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)asyncRe sult.AsyncState ;
    > clntSock = servSock.EndAcc ept(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.Sock ets.AcceptAsync Result.
    > According to .net memory profiler loads of these were being created and[/color]
    not[color=blue]
    > freed up.
    >
    >
    >
    > I came to the conclusion that this was because for ever BeginAccept that[/color]
    was[color=blue]
    > being called there was not a EndAccept for all of them. Therefore I[/color]
    decide[color=blue]
    > to place a EndAccept call instead the while loop just after the[/color]
    BeginAccept.[color=blue]
    >
    >
    >
    > I then ran .net memory profiler again and the memory leek was gone.[/color]
    However[color=blue]
    > 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.
    >
    >
    >
    >
    >
    >[/color]


    Comment

    Working...