Community Help: Check the Help Files, then come here to ask!
Programming/Scripts
select() and detection of closed socket
Results 1 to 7 of 7
Thread Tools
Show Printable Version
Email this Page…
Display
Switch to Hybrid Mode
Switch to Threaded Mode
08-02-2004,11:14 PM#1
nickptar
View Profile
View Forum Posts
Registered User
Join Date
Nov 2002
Posts
90
The select_tut(2) man page seems to imply that to check if a socket is closed, you use the exceptfds parameter (which it says is used for out-of-band data), as in this code:
Code:
if (fd1 > 0) if (FD_ISSET (fd1, &er)) { char c; errno = 0; r = recv (fd1, &c, 1, MSG_OOB); if (r < 1) { SHUT_FD1; } else send (fd2, &c, 1, MSG_OOB); }
Is this accurate, or is there some other way? It seems to me that this would be a rather clumsy way to notify the program.
Also, if you're doing straight TCP input and output, do send() and recv() have any advantages over read() and write()?
With TCP, no I don't believe send and recv do anything different from read and write. (Actually I think of UDP as a bit of a performance hack anyway. At least with TCP sockets you can use the same system calls as you'd be able to use with a file, which makes testing a little easier -- just use a file handle instead of a socket handle while testing. Not to mention the number of times I've seen people try to implement connections on top of UDP because "UDP is faster!" Duh... yeah it is, but only because it doesn't do what you need it to do...)
To check whether a socket is closed, just try to read() from it. If read() returns 0, then it's been closed. (However, if you call read() again, it will return -1.) The only reason to use select() is if you have more than one file descriptor that you're waiting on, and from your question, it sounds like you only have one. Let read() do the blocking for you rather than select().
Reply With Quote
08-02-2004,11:41 PM#3
nickptar
View Profile
View Forum Posts
Registered User
Join Date
Nov 2002
Posts
90
Originally posted by bwkaz To check whether a socket is closed, just try to read() from it. If read() returns 0, then it's been closed. (However, if you call read() again, it will return -1.) The only reason to use select() is if you have more than one file descriptor that you're waiting on, and from your question, it sounds like you only have one. Let read() do the blocking for you rather than select().
Except I need to have large numbers of sockets open at once, some waiting to read, some waiting to write. I suppose I could set O_NONBLOCK and loop through, read()ing all of them and seeing if they return 0, but that would not be efficient. Can select() report socket closings?
Originally posted by nickptar Except I need to have large numbers of sockets open at once, some waiting to read, some waiting to write. I suppose I could set O_NONBLOCK and loop through, read()ing all of them and seeing if they return 0, but that would not be efficient. Can select() report socket closings?
With the large number of sockets open at once, use select. Yes, using the O_NONBLOCK will use up way too much cpu. Select will only tell you that there has been activity on the socket(or file descripter). When you go in and read/write and get a 0 like bwkaz said, then you've been closed.
Also, you'll probably want to add a SIGPIPE(believe that's the one) check in there, in case you're connection(s) go to crap.
if (i_forgot && this_is_about_code) language = c++;
Reply With Quote
08-04-2004,11:33 AM#5
nickptar
View Profile
View Forum Posts
Registered User
Join Date
Nov 2002
Posts
90
Originally posted by tecknophreak With the large number of sockets open at once, use select. Yes, using the O_NONBLOCK will use up way too much cpu. Select will only tell you that there has been activity on the socket(or file descripter). When you go in and read/write and get a 0 like bwkaz said, then you've been closed.
Also, you'll probably want to add a SIGPIPE(believe that's the one) check in there, in case you're connection(s) go to crap.
I'm confused. I know that I can check if a socket is dead by seeing if read() returns 0, but if it's dead, select() will never mark it as available for reading, right? So how will the program - blocking in select() and only calling read() or write() when select() says to - know when the socket closes? Is the "exceptids" parameter for this, as well as out-of-bound data?
What function can raise SIGPIPE, and under what circ*mstances?
Can't remember off hand what would cause a SIGPIPE, but I remember that it does happen occationally and it killed the program I was working on.
select will exit when a connection has been broken. The man page tells you that
Those listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a file descriptor is also ready on end-of-file)
When a connection is broken, then that socket will not block on a read or write.
if (i_forgot && this_is_about_code) language = c++;
Didn't you post earlier that you could use exceptfds for checking for a closed connection?
I'm still pretty sure that's the case -- select will return if any FD in readfds is ready to read, any FD in writefds is ready to write, OR any socket FD in exceptfds either has out-of-band data, or has been closed. At least, that's how I read the select (and select_tut) manpage...
Reply With Quote
Quick NavigationProgramming/ScriptsTop
Site Areas
Settings
Private Messages
Subscriptions
Who's Online
Search Forums
Forums Home
Forums
Community Help: Check the Help Files, then come here to ask!
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.
To check whether a socket is closed, just try to read() from it. If read() returns 0, then it's been closed. (However, if you call read() again, it will return -1.)
To resolve this issue, leverage the connection retry period. As soon as the connection is closed, instead of terminating the session, PowerCenter will re-establish a new fresh connection to the database and resume the Database operations.
isConnected() tells you whether you have connected this socket. You have, so it returns true. isClosed() tells you whether you have closed this socket. Until you have, it returns false.
After your extraction, you should develop a blood clot in the socket (hole) that's left behind. It'll look like a dark-colored scab. If you see a blood clot, leave it alone and don't try to remove it. It'll help protect the extraction site.
Socket errors can be caused by various issues including connectivity problems on the network, client or server computers or due to a firewall, antivirus or a proxy server. This error occurs when the socket connection to the remote server is denied.
When an application program indicates that a socket is to linger, it also specifies a duration for the lingering period. If the lingering period expires before the disconnect is completed, the socket layer forcibly shuts down the socket, discarding any data still pending.
One way or another, if you don't close a socket, your program will leak a file descriptor. Programs can usually only open a limited number of file descriptors, so if this happens a lot, it may turn into a problem.
To clean up your sockets, you'll need to run the fg (foreground) command for each socat process that you created. Then you'll use CTRL+C to close each socat. Run fg to bring the datagram-based socat instance to the foreground of your terminal: fg.
You can terminate a specific TCP/IP socket endpoint using the Netstat DRop/-D command. When a DRop command is issued against a socket endpoint, any outstanding or following socket calls that refer to the socket that is being dropped terminate with a negative return code.
A "socket closed" error means that the connection has been terminated, however, no specific reason was provided/returned. The "socket closed" error indicates that the connection was lost outside of the control or awareness of the Driver. There can be a number of reasons for that, for example: network failure.
If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
You can check if a socket is listening on Linux using the "netstat" command or the "ss" command. Both of these commands provide information about active network connections and open sockets. To use "netstat" to check for listening sockets, you can use the following command: netstat -tln.
If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
You can check if a socket is listening on Linux using the "netstat" command or the "ss" command. Both of these commands provide information about active network connections and open sockets. To use "netstat" to check for listening sockets, you can use the following command: netstat -tln.
How do you test if an outlet is live without a multimeter? I'd take a lamp I know works, plug it in elsewhere, turn it ON, confirm it lights, unplug it and then plug it into the outlet in question. If the lamp does not light, I'd go plug it into the first outlet again to see if it still lights there.
Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002
Phone: +813077629322
Job: Real-Estate Executive
Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating
Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.
We notice you're using an ad blocker
Without advertising income, we can't keep making this site awesome for you.