Re: closesocket

From: Paul G. Tobey [eMVP] (ptobey)
Date: 02/23/05


Date: Wed, 23 Feb 2005 10:27:37 -0700

No, the Wait is called in the application exit code. Something like this:

BOOL exitFlag = FALSE;
SOCKET s;

ThreadProc()
{
    while ( !exitFlag )
    {
        recv(s, ...);

        // Do something with data, handle error, etc.
    }
}

ExitRoutine()
{
    closesocket( s );

    // It would be a good idea to hide the main window before doing this, so
that the
    // user doesn't see the application any more. The fact that it's still
cleaning up the
    // thread in the background shouldn't bother him.
    WaitForSingleObject( s, <some reasonable period of time, maybe five
seconds> );

    WSACleanup();
}

Paul T.

"tennisman" <schao416@yahoo.com> wrote in message
news:1109174126.611040.234230@f14g2000cwb.googlegroups.com...
> Paul,
>
> Thanks for the quick reply. Yes, I have recv() running in a worker
> thread since my app uses blocking socket. Currently the destructor
> calls closesocket and WSACleanup without waiting for the thread to
> exit. You hit the nail right on the head:) My first line of code in
> the worker thread is recv(), and it will continue to block until I get
> data in the receive buffer. Since WaitForSingleObject is another
> blocking call, I'm not too sure how it would fit into the current
> scheme of thing within the thread. From what I read, AfxExitThread can
> only be called from within the thread. Any tips? Thanks.
>
> Paul G. Tobey [eMVP] wrote:
>> So, to fill in the blanks, the socket is running in a separate
> thread,
>> right, since it's blocking? What happens when that socket is closed
> out
>> from under the calls that are happening in the thread? Does the
> thread do
>> the right thing? In cases where you have a thread for this sort of
> thing,
>> I'd always arrange for the thread to be notified to leave (set a
> flag,
>> maybe, then close the socket, and have the thread, on socket errors,
> check
>> the flag and exit if it's set). Then, before cleaning up WinSock,
> wait for
>> the thread to exit. You can use WaitForSingleObject with the
> thread's
>> handle to do that. Your goal should be for everything to exit as
> cleanly as
>> possible. It wouldn't surprise me if your thread was still
> 'running', as
>> things are now...
>>
>> Paul T.
>>
>> "tennisman" <schao416@yahoo.com> wrote in message
>> news:1109143030.540188.193190@o13g2000cwo.googlegroups.com...
>> > Hello,
>> >
>> > I have created an app with blocking socket and everything is
> working
>> > fine until I shut down the app. The problem is that when I
> shutdown
>> > the app, a small rip of my app's GUI remains on the desktop window.
> It
>> > looks like someone rip a piece off the GUI and paste it on the
> desktop
>> > window. In my destructor, I have inserted code to close the socket
> and
>> > clean up which looks like this:
>> >
>> > CMainDlg::~CMainDlg()
>> > {
>> > closesocket(m_Sock);
>> > WSACleanup();
>> > }
>> >
>> > The weird thing is that if I comment out closesocket and WSACleanup
>> > then the app shuts down w/o leaving residue. Even if I only
> comment
>> > out one w/o the other (doesn't matter which one), it still leaves a
>> > residue. Any tip would be appreciated.
>> >
>



Relevant Pages

  • [9fans] APE and listen(2B)
    ... seems to somehow exit the actual listenproc, ... socket accept: Invalid argument ... int main { ... perror; ...
    (comp.os.plan9)
  • Re: error handling
    ... >> Lisa Pearlson wrote: ... >>> Imagine I have a socket read function.. ... >>> exit 1 ... > exceptions to return data yet allow additional info.. ...
    (comp.lang.tcl)
  • Re: Dangers of intercepting SIGINT?
    ... I used signalto interceptSIGINT. ... it must close it's socket and exit. ... The server process prints various ...
    (comp.os.linux.development.apps)
  • Re: Blocking RECV call & Shutting down Thread
    ... just trying to read from a non-blocking socket. ... thread to wake up and exit. ... a clean thread shutdown is preferable to just terminating one, ... (or some other flag you set, ...
    (microsoft.public.vc.mfc)
  • Re: Problem receiving Multicast traffic transmitted on receiving inter
    ... when using a separate receiver socket, ... I have a service that both sends and receives multicast frames. ... closesocket; ... sizeof ) == SOCKET_ERROR) ...
    (microsoft.public.windowsce.embedded)

Quantcast