select() and detection of closed socket (2024)


  • Register
  • Help
  • Forum
    • New Posts
    • FAQ
    • Calendar
    • Forum Actions
      • Mark Forums Read
    • Quick Links
      • Today's Posts
      • View Site Leaders
  • What's New?
  • Advanced Search
  • Forum
  • 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
  1. 08-02-2004,11:14 PM #1

    nickptar

    • View Profile
    • View Forum Posts

    select() and detection of closed socket (4)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()?

    Reply With Quote

  2. 08-02-2004,11:33 PM #2

    bwkaz

    select() and detection of closed socket (9)Registered User

    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    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

  3. 08-02-2004,11:41 PM #3

    nickptar

    • View Profile
    • View Forum Posts

    select() and detection of closed socket (15)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?

    Reply With Quote

  4. 08-04-2004,11:14 AM #4

    tecknophreak

    select() and detection of closed socket (20)Bad Speller ^^

    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228

    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

  5. 08-04-2004,11:33 AM #5

    nickptar

    • View Profile
    • View Forum Posts

    select() and detection of closed socket (26)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?

    Reply With Quote

  6. 08-04-2004,12:45 PM #6

    tecknophreak

    select() and detection of closed socket (31)Bad Speller ^^

    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228
    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++;

    Reply With Quote

  7. 08-04-2004,06:26 PM #7

    bwkaz

    select() and detection of closed socket (36)Registered User

    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    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!
    1. Newbies Corner
    2. Hardware
    3. Software
    4. Installation
    5. Window/Desktop Managers
    6. Kernels and Modules
    7. Networking
    8. Web/Security
    9. Mobile Computing
    10. Games
    11. Programming/Scripts
    12. Technical
    13. How I Did It!
    14. Linux PPC
    15. Other UNIX-Based
    16. Hardware Trading
  • JustLinux
    1. Help File Library
      1. Audio Hardware
      2. Configuration
      3. Hardware
      4. Installation
      5. Networks
      6. Programming
      7. Software
      8. X Window
    2. Events
    3. JL Talkback
    4. JL Ideas
  • Feedback
    1. Forum Suggestions
    2. Post Your Forum Bugs Here
  • Off Topic
    1. /dev/random
    2. .iso Confused - Which Distro?

«Previous Thread|Next Thread»

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

  • Contact Us
  • JustLinux Forums
  • Top

All times are GMT -4. The time now is 01:24 AM.


select() and detection of closed socket (42)

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.

select() and detection of closed socket (2024)

FAQs

How to check if a socket is closed? ›

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.)

How to fix socket closed error? ›

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.

What does socket close () do? ›

Close() Closes the Socket connection and releases all associated resources.

How to know if client closed socket in Java? ›

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.

What does a closed socket look like? ›

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.

How do you check if a socket is working? ›

Here's how to test sockets for power and wiring faults:
  1. Switch off the socket.
  2. Remove any other plugs or devices from the mains socket.
  3. Insert your socket tester.
  4. Switch the power back on and await the results.
Jan 30, 2023

What would cause a socket error? ›

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.

What causes a socket to close? ›

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.

What happens if I don't close a socket? ›

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.

How do you clean up sockets in Linux? ›

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.

How to close a socket in netstat? ›

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.

What does socket closed mean? ›

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.

How to check if a socket is open in Java? ›

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.

How do I check my socket status? ›

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 I know if my socket is disconnected? ›

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.

How do you check what sockets are open? ›

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 tell if a socket is on or off? ›

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.

Top Articles
The Five Critical Components of a Financial Plan for Retirees
Complete Guide for Trading Pump and Dump Stocks | TradingSim
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6337

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

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.