UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2012-12-12 07:09 AM

fredson
Member
From: London, United Kingdom
Registered: 2012-12-12
Posts: 5
Website

How to Know Active Socket

Hi! I'm still kind a confuse on how would I know if my socket is still active?

Offline

#2 2012-12-12 09:12 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,235

Re: How to Know Active Socket

Depends on what exactly you mean with "active" and what kind of socket
we're talking about. UDP is never active or inactive as such, because there
is no connection. For TCP there is a connection, but if one host silently dies
it can take a long time for the other to discover that it's gone. The only way
to know for sure that a socket is active is to actually use it and receive data.

If a TCP connection gets broken and keep-alive is disabled, you will never
notice until you try to send something and the TCP stack gives up trying to
deliver the data, which can take a very long time. But normally when there is
no hardware crash or NAT routers silently forgetting about your connection,
you will receive an error notification when you try to send or receive.

Offline

#3 2012-12-12 01:23 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: How to Know Active Socket

If a TCP connection gets broken and keep-alive is disabled, you will never
notice until you try to send something and the TCP stack gives up trying to
deliver the data, which can take a very long time.

Yes, for any use where your TCP sockets are going to be sitting idle for possibly long periods of time, you definitely should enable SO_KEEPALIVE probes...  And, if possible, with tweaked settings from the standard defaults, which take way too long to kick in and give up...  (On Linux, you can configure things on a per-socket basis: TCP_KEEPIDLE, TCP_KEEPCNT, and TCP_KEEPINTVL...)  If your protocol is fairly regularly sending data in at least one direction non-stop, then you don't really need it to detect such silently killed connections, but it still can't hurt...  But, basically if your protocol allows arbitrarily long waits for input to arrive, and doesn't require you to periodically send output, then SO_KEEPALIVE is the only thing that will save you from such zombie connections...

Offline

#4 2013-02-28 09:41 AM

fredson
Member
From: London, United Kingdom
Registered: 2012-12-12
Posts: 5
Website

Re: How to Know Active Socket

What I am referring is active socket in ip.

Offline

#5 2013-02-28 01:30 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: How to Know Active Socket

I still don't know what you mean by that...  First of all, it's rather hard to have a plain IP socket, without TCP or UDP being involved as well...  Are you talking about an AF_INET/SOCK_RAW socket or something?

And, I still don't know what you mean by "active", exactly...  Do you just mean is it still usable?  If so, the only case it wouldn't be is the case we previously mentioned, where it's a TCP socket and the connection went dead...  Like we said, in that case, you'd want to use keepalive probes to be able to detect such otherwise silent connection deaths...

Offline

#6 2013-03-01 05:01 AM

fredson
Member
From: London, United Kingdom
Registered: 2012-12-12
Posts: 5
Website

Re: How to Know Active Socket

Sorry in my incomplete word. I'm taking about ip with UDP.

Offline

#7 2013-03-01 01:13 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: How to Know Active Socket

Well, then, like i3839 said, there are no connections involved in UDP, so I have no idea what "active" would even mean in regards to a UDP socket...  As long as you haven't closed it, you should be able to use the UDP socket at any time...

Offline

#8 2013-03-04 02:26 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,235

Re: How to Know Active Socket

In case you want to know that the socket is still alive as a file descriptor
and not closed or something else than your UDP socket, you could call
getsockname() on it to check. But normally you would make sure that
you always know if a socket is still open or not, and remove all references
to it when some part of the code does close it.

The kernel will never close the fd behind your back, it will only close it
when you ask it to, or when the process exits.

Offline

#9 2015-07-21 02:18 PM

CJ
Guest

Re: How to Know Active Socket

In java there is a function for checking wheather the socket is active or not..

#10 2015-07-22 05:13 PM

DougPan
Guest

Re: How to Know Active Socket

I thought I would make a comic that hopefully helps...

Over Simplified TCP Socket           Under Simplifed UDP Socket
                Connection                    Does Not Connect
  Person  Let me know when             Person
                you're wet-v             Can't
                   Connection             Connect
   I am Pouring         ^   v              Doesn't annouce
           Water~        ^   v              Pours Water~
                 \        ^   v                         \
                  |        ^   v                         |
                  v         ^   v                        |
       Funnel  \     /       ^   v            Funnel  \  |  /
                \   /        ^   v                     \ | /
                 \ /         ^   v                      \|/
                 | |         ^   v                      |||
                             ^   v                       |
Timer Bucket   |~~~~~|       ^   v                       |
When Full Tip  |_____|       ^   v                       |
                _____        ^   v                    ~~~v~~~
When Wet wake  /     \       ^   v                   ~/     \~
up and say I'm | 0 0 |       ^   v                  ~ | _ _ | ~
wet...         \  ^  /       ^   v                  ~ \  ^  / ~
              > \_o_/        ^   v                  ~  \_*_/  ~
             ^    |          ^   v                  ~         ~
            ^     v          ^   v                  ~         ~
            ^     Responds   ^   v                  ~         ~
            ^     with...    ^   v                  ~         ~
            ^     Nothing   ^    v                  ~         ~
            ^     Yet ->>>>>^   v                   ~  water  ~
            ^                  v                   ~~~~~~~~~~~~~
             ^<<<<<<<<<<<<<<<v                   ~~~~~~~~~~~~~~~~~
                                                ~~~~~~~~~~~~~~~~~~~
                                              ~~~~~~~~~~~~~~~~~~~~~~~~
 In a TCP senario TCP, wait's and it        In a UDP senario, the software
also responsds based on timing logic.    you write needs to respond, not UDP


DougPan

#11 2015-07-22 05:17 PM

JerryLetter
Guest

Re: How to Know Active Socket

Now that's the best explanation I've ever heard... I mean seen.

Jerry

#12 2015-07-22 05:40 PM

DougPan
Member
From: Canada
Registered: 2015-07-22
Posts: 3
Website

Re: How to Know Active Socket

Now I'm registered with developerweb.net as DougPan

DougPan wrote:
I thought I would make a comic that hopefully helps...

Over Simplified TCP Socket           Under Simplifed UDP Socket
                Connection                    Does Not Connect
. . .

DougPan

Offline

#13 2015-07-22 05:43 PM

DougPan
Member
From: Canada
Registered: 2015-07-22
Posts: 3
Website

Re: How to Know Active Socket

JerryLetter wrote:

Now that's the best explanation I've ever heard... I mean seen.

Jerry

Thanks Jerry

Offline

#14 2015-07-22 08:38 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,235

Re: How to Know Active Socket

DougPan wrote:

Now I'm registered with developerweb.net as DougPan

Welcome! I hope you're aware that you just put one foot into the grave though. ;-)

It's good to know that people are still reading this forum.

Offline

#15 2015-07-22 09:19 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: How to Know Active Socket

Indeed...  New non-spammer users are a rare treat these days...  Hell, I'd even welcome the typical old post we used to get from new school students wanting us to do their homework for them!  (I'd still just ridicule them and make them do it themselves, of course, but any non-spam content would be a nice change of pace these days...)

Offline

#16 2015-07-23 02:12 PM

DougPan
Member
From: Canada
Registered: 2015-07-22
Posts: 3
Website

Re: How to Know Active Socket

i3839 wrote:
DougPan wrote:

Now I'm registered with developerweb.net as DougPan

Welcome! I hope you're aware that you just put one foot into the grave though. ;-)

It's good to know that people are still reading this forum.

Wew, i3839, in what sense have I put one foot into the grave?  Is it the spam as mentioned by RobSeace at  04:19 PM, or is it something else?
If it's spam I guess I could just turn off this forum from forwarding to my email.
If it's some matter of incorrectness, please feel free to correct me.

Thanks for welcoming me.

Offline

#17 2015-07-23 09:57 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: How to Know Active Socket

I think i3839 just meant that the forum is dying (or is already the walking dead)...  We used to be a lot more active in the past, but in recent years we hardly get any real programming discussion anymore...

Offline

#18 2015-07-24 10:07 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,235

Re: How to Know Active Socket

Yeah, what Rob said. It's not just this forum, but forums in general seem to be a getting quieter. At least my impression is that the new generation doesn't do forums.

I would like to know if you receive any spam via your subscription. I never did, but maybe they're smart enough to exclude admins. All those dormant bot accounst must be there for some reason... I disabled user PMing for this reason.

I miss those interesting problem questions, I loved debugging them. :-/

Offline

#19 2016-08-23 07:35 AM

Lagger
Member
Registered: 2016-08-22
Posts: 1

Re: How to Know Active Socket

DougPan wrote:
I thought I would make a comic that hopefully helps...

Over Simplified TCP Socket           Under Simplifed UDP Socket
                Connection                    Does Not Connect
  Person  Let me know when             Person
                you're wet-v             Can't
                   Connection             Connect
   I am Pouring         ^   v              Doesn't annouce
           Water~        ^   v              Pours Water~
                 \        ^   v                         \
                  |        ^   v                         |
                  v         ^   v                        |
       Funnel  \     /       ^   v            Funnel  \  |  /
                \   /        ^   v                     \ | /
                 \ /         ^   v                      \|/
                 | |         ^   v                      |||
                             ^   v                       |
Timer Bucket   |~~~~~|       ^   v                       |
When Full Tip  |_____|       ^   v                       |
                _____        ^   v                    ~~~v~~~
When Wet wake  /     \       ^   v                   ~/     \~
up and say I'm | 0 0 |       ^   v                  ~ | _ _ | ~
wet...         \  ^  /       ^   v                  ~ \  ^  / ~
              > \_o_/        ^   v                  ~  \_*_/  ~
             ^    |          ^   v                  ~         ~
            ^     v          ^   v                  ~         ~
            ^     Responds   ^   v                  ~         ~
            ^     with...    ^   v                  ~         ~
            ^     Nothing   ^    v                  ~         ~
            ^     Yet ->>>>>^   v                   ~  water  ~
            ^                  v                   ~~~~~~~~~~~~~
             ^<<<<<<<<<<<<<<<v                   ~~~~~~~~~~~~~~~~~
                                                ~~~~~~~~~~~~~~~~~~~
                                              ~~~~~~~~~~~~~~~~~~~~~~~~
 In a TCP senario TCP, wait's and it        In a UDP senario, the software
also responsds based on timing logic.    you write needs to respond, not UDP


DougPan [url=http://gold.com/]stripped[/url]

Brilliant post Doug, I love posts like these because they give me a new way of thinking about things in the future. It's great to learn.

Last edited by Lagger (2017-04-13 11:27 AM)

Offline

Board footer

Powered by FluxBB