Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Socket that won't close
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Alex Taylor  
View profile  
 More options May 17, 3:41 am
Newsgroups: comp.os.os2.programmer.misc, comp.os.os2.networking.tcp-ip
From: "Alex Taylor" <mail...@reply.to.address>
Date: 17 May 2009 02:41:01 -0500
Local: Sun, May 17 2009 3:41 am
Subject: Socket that won't close
I'm writing a REXX program that runs a TCP/IP daemon thread using RXSOCK.

The socket is created as follows (error handling omitted):

    socket = SockSocket('AF_INET', 'SOCK_STREAM', 0 )
    CALL SockIoctl socket, 'FIONBIO', 1
    rc = SockSetSockOpt( socket, 'SOL_SOCKET', 'SO_LINGER', '1 2')
    address.!family = 'AF_INET'
    address.!port   = 2780
    address.!addr   = 'INADDR_ANY'
    rc = SockBind( socket, 'address.!')
    rc = SockListen( socket, 5 )

The daemon thread is then started, which basically runs a SockAccept() loop
until a quit event is posted, at which point it calls SockClose( socket )
and then signals the main program to exit.

This works fine about 80% of the time.  Sometimes, however, I discover
once the program has exited that the socket is still open, stalled in
TIME_WAIT status.  It usually stays that way for about 20 seconds or so,
during which time of course I can't restart the daemon successfully.

I've tried with SO_LINGER both enabled and disabled.  It makes no apparent
difference.  

How can I make sure this wretched socket gets closed when the program
does?

--
Alex Taylor
Fukushima, Japan
http://www.socis.ca/~ataylo00

Please take off hat when replying.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marty  
View profile  
 More options May 17, 12:46 am
Newsgroups: comp.os.os2.programmer.misc, comp.os.os2.networking.tcp-ip
From: Marty <n...@comcast.martyamodeo>
Date: Sun, 17 May 2009 00:46:35 -0400
Local: Sun, May 17 2009 12:46 am
Subject: Re: Socket that won't close

Do you do anything to unblock the receiving thread before exiting?  Or
is the thread killed "harshly" when the main thread exits?

--
[Reverse the parts of the e-mail address to reply.]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Taylor  
View profile  
 More options May 17, 4:23 am
Newsgroups: comp.os.os2.networking.tcp-ip, comp.os.os2.programmer.misc
From: "Alex Taylor" <mail...@reply.to.address>
Date: 17 May 2009 03:23:01 -0500
Local: Sun, May 17 2009 4:23 am
Subject: Re: Socket that won't close

On Sun, 17 May 2009 04:46:35 UTC, Marty <n...@comcast.martyamodeo> wrote:
> > The daemon thread is then started, which basically runs a SockAccept()
> > loop until a quit event is posted, at which point it calls SockClose(
> > socket ) and then signals the main program to exit.

> > This works fine about 80% of the time.  Sometimes, however, I discover
> > once the program has exited that the socket is still open, stalled in
> > TIME_WAIT status.  It usually stays that way for about 20 seconds or so,
> > during which time of course I can't restart the daemon successfully.

> Do you do anything to unblock the receiving thread before exiting?  Or
> is the thread killed "harshly" when the main thread exits?

As I say, on program exit, I post an event to the daemon thread which tells
it to break out of its loop.  At that point, it calls SockClose, then posts
an event back to the main program tell it that it's safe to exit.

When the user goes to exit the program, I post this event to the daemon
thread:

    IF listener.!thread > 0 THEN DO
        rc = VRMethod('Application', 'PostQueue',,
                       listener.!thread, 0, 'finished = 1')
    END

(I also post termination messages to any client socket threads that may be
active, but that part seems to be working as it should.)

The daemon thread's loop looks like this:

    finished = 0
    CALL VRInit
    DO WHILE finished == 0
        csock = SockAccept( socket )
        IF csock > 0 THEN DO
            CALL VRMethod 'Application', 'PostQueue', 0, 1,,
                          'CALL Event_Incoming', 'ClientSocket', csock
        END
        _VREEvent = VREvent('N')
        INTERPRET _VREEvent
    END
    IF socket > 0 THEN DO
        rc = SockClose( socket )
        IF rc \= 0 THEN CALL SockPSock_ErrNo()
    END
    CALL VRMethod 'Application', 'PostQueue', 0, 1, 'CALL Quit'
    CALL VRFini

At which point the "Quit" method in the main program simply destroys the
window and exits, as usual.

--
Alex Taylor
Fukushima, Japan
http://www.socis.ca/~ataylo00

Please take off hat when replying.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Ratcliffe  
View profile  
 More options May 17, 5:07 am
Newsgroups: comp.os.os2.programmer.misc, comp.os.os2.networking.tcp-ip
From: Paul Ratcliffe <ab...@orac12.clara34.co56.uk78>
Date: Sun, 17 May 2009 09:07:07 GMT
Local: Sun, May 17 2009 5:07 am
Subject: Re: Socket that won't close
On 17 May 2009 02:41:01 -0500, Alex Taylor <mail...@reply.to.address> wrote:

> The daemon thread is then started, which basically runs a SockAccept() loop
> until a quit event is posted, at which point it calls SockClose( socket )
> and then signals the main program to exit.

> This works fine about 80% of the time.  Sometimes, however, I discover
> once the program has exited that the socket is still open, stalled in
> TIME_WAIT status.  It usually stays that way for about 20 seconds or so,
> during which time of course I can't restart the daemon successfully.

> I've tried with SO_LINGER both enabled and disabled.  It makes no apparent
> difference.  

> How can I make sure this wretched socket gets closed when the program
> does?

It's all very annoying. I've never got to the bottom of it. Have you
tried putting in a SockShutDown() to see if that helps?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Taylor  
View profile  
 More options May 17, 6:45 am
Newsgroups: comp.os.os2.networking.tcp-ip, comp.os.os2.programmer.misc
From: "Alex Taylor" <mail...@reply.to.address>
Date: 17 May 2009 05:45:01 -0500
Local: Sun, May 17 2009 6:45 am
Subject: Re: Socket that won't close
On Sun, 17 May 2009 09:07:07 UTC, Paul Ratcliffe

<ab...@orac12.clara34.co56.uk78> wrote:
> > How can I make sure this wretched socket gets closed when the program
> > does?

> It's all very annoying. I've never got to the bottom of it.

Ah, so it's not just me?  Not sure if that's a relief or a worry.  :)

Does it happen only with RXSOCK, or with sockets in general?

> Have you tried putting in a SockShutDown() to see if that helps?

Well, now I have.  And no, it doesn't help.  :(

--
Alex Taylor
Fukushima, Japan
http://www.socis.ca/~ataylo00

Please take off hat when replying.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Ratcliffe  
View profile  
 More options May 17, 7:27 am
Newsgroups: comp.os.os2.networking.tcp-ip, comp.os.os2.programmer.misc
From: Paul Ratcliffe <ab...@orac12.clara34.co56.uk78>
Date: Sun, 17 May 2009 11:27:05 GMT
Local: Sun, May 17 2009 7:27 am
Subject: Re: Socket that won't close
On 17 May 2009 05:45:01 -0500, Alex Taylor <mail...@reply.to.address> wrote:

>> > How can I make sure this wretched socket gets closed when the program
>> > does?

>> It's all very annoying. I've never got to the bottom of it.

> Ah, so it's not just me?  Not sure if that's a relief or a worry.  :)

> Does it happen only with RXSOCK, or with sockets in general?

In general I think.

>> Have you tried putting in a SockShutDown() to see if that helps?

> Well, now I have.  And no, it doesn't help.  :(

Oh well, worth a shot. I think you'll just have to learn to live with it.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Ratcliffe  
View profile  
 More options May 17, 9:20 am
Newsgroups: comp.os.os2.networking.tcp-ip, comp.os.os2.programmer.misc
From: Paul Ratcliffe <ab...@orac12.clara34.co56.uk78>
Date: Sun, 17 May 2009 13:20:08 GMT
Local: Sun, May 17 2009 9:20 am
Subject: Re: Socket that won't close
On Sun, 17 May 2009 11:27:05 GMT, Paul Ratcliffe

Actually, having pursued this a bit more, I find that my server only
fails to bind with an "Address in use" error if it had connected clients
at the time I closed it, despite disconnecting them properly.
If it had no clients it restarted fine every time.
"netstat -s" never showed any lingering sockets either way.

Subsequent to this, I added a SO_REUSEADDR setsocketopt() to the socket
before binding and it now works properly all the time.
I can't remember why I didn't do this before...

This is on a machine with a 16 bit TCP/IP stack though. Perhaps I should
try it on a 32 bit stack as well.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marty  
View profile  
 More options May 19, 9:19 am
Newsgroups: comp.os.os2.networking.tcp-ip, comp.os.os2.programmer.misc
From: Marty <n...@comcast.martyamodeo>
Date: Tue, 19 May 2009 09:19:14 -0400
Local: Tues, May 19 2009 9:19 am
Subject: Re: Socket that won't close

Is there some chance that the main application can post its message to
terminate the thread, but not give up the rest of its timeslice and exit
before the thread gets the message?  In C-land, I would have some kind
of DosWaitThread type of thing in there.

--
[Reverse the parts of the e-mail address to reply.]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Taylor  
View profile  
 More options May 20, 1:08 am
Newsgroups: comp.os.os2.programmer.misc, comp.os.os2.networking.tcp-ip
From: "Alex Taylor" <mail...@reply.to.address>
Date: 20 May 2009 00:08:01 -0500
Local: Wed, May 20 2009 1:08 am
Subject: Re: Socket that won't close

On Tue, 19 May 2009 13:19:14 UTC, Marty <n...@comcast.martyamodeo> wrote:
> > As I say, on program exit, I post an event to the daemon thread which
> > tells it to break out of its loop.  At that point, it calls SockClose,
> > then posts an event back to the main program tell it that it's safe to
> > exit.

> Is there some chance that the main application can post its message to
> terminate the thread, but not give up the rest of its timeslice and exit
> before the thread gets the message?  In C-land, I would have some kind
> of DosWaitThread type of thing in there.

I don't see how.  If the daemon thread is active, then the main program
can only quit once it receives the 'done' message from the listener.

But I have no way of knowing what the VX-REXX runtime is doing internally,
of course...

--
Alex Taylor
Fukushima, Japan
http://www.socis.ca/~ataylo00

Please take off hat when replying.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Taylor  
View profile  
 More options May 26, 7:17 am
Newsgroups: comp.os.os2.programmer.misc, comp.os.os2.networking.tcp-ip
From: "Alex Taylor" <mail...@reply.to.address>
Date: 26 May 2009 06:17:02 -0500
Local: Tues, May 26 2009 7:17 am
Subject: Re: Socket that won't close
On Sun, 17 May 2009 13:20:08 UTC, Paul Ratcliffe

<ab...@orac12.clara34.co56.uk78> wrote:
> Actually, having pursued this a bit more, I find that my server only
> fails to bind with an "Address in use" error if it had connected clients
> at the time I closed it, despite disconnecting them properly.
> If it had no clients it restarted fine every time.
> "netstat -s" never showed any lingering sockets either way.

> Subsequent to this, I added a SO_REUSEADDR setsocketopt() to the socket
> before binding and it now works properly all the time.
> I can't remember why I didn't do this before...

> This is on a machine with a 16 bit TCP/IP stack though. Perhaps I should
> try it on a 32 bit stack as well.

Well, so far it seems to work here (crossing fingers).  MPTS WR08708
(eCS 2.0rc4).

Thanks for the hint!
--
Alex Taylor
Fukushima, Japan
http://www.socis.ca/~ataylo00

Please take off hat when replying.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google