Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3946576
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:08:59+00:00 2026-05-20T01:08:59+00:00

When I close the socket on one end of a connection, the other end

  • 0

When I close the socket on one end of a connection, the other end gets an error the second time it sends data, but not the first time:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 12345))
server.listen(1)

client = socket.create_connection(("localhost",12345))
sock, addr = server.accept()
sock.close()

client.sendall("Hello World!")    # no error
client.sendall("Goodbye World!")  # error happens here

I’ve tried setting TCP_NODELAY, using send instead of sendall, checking the fileno(), I can’t find any way to get the first send to throw an error or even to detect afterwards that it failed. EDIT: calling sock.shutdown before sock.close doesn’t help. EDIT #2: even adding a time.sleep after closing and before writing doesn’t matter. EDIT #3: checking the byte count returned by send doesn’t help, since it always returns the number of bytes in the message.

So the only solution I can come up with if I want to detect errors is to follow each sendall with a client.sendall("") which will raise an error. But this seems hackish. I’m on a Linux 2.6.x so even if a solution only worked for that OS I’d be happy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:08:59+00:00Added an answer on May 20, 2026 at 1:08 am

    This is expected, and how the TCP/IP APIs are implemented (so it’s similar in pretty much all languages and on all operating systems)

    The short story is, you cannot do anything to guarantee that a send() call returns an error directly if that send() call somehow cannot deliver data to the other end. send/write calls just delivers the data to the TCP stack, and it’s up to the TCP stack to deliver it when it can.

    TCP is also just a transport protocol, if you need to know if your application “messages” have reached the other end, you need to implement that yourself(some form of ACK), as part of your application protocol – there’s no other free lunch.

    However – if you read() from a socket, you can get notified immediatly when an error occurs, or when the other end closed the socket – you usually need to do this in some form of multiplexing event loop (that is, using select/poll or some other IO multiplexing facility).

    Just note that you cannot read() from a socket to learn whether the most recent send/write succeded, Here’s a few cases as of why (but it’s the cases one doesn’t think about that always get you)

    • several write() calls got buffered up due to network congestion, or because the tcp window was closed (perhaps a slow reader) and then the other end closes the socket or a hard network error occurs, thus you can’t tell if if was the last write that didn’t get through, or a write you did 30 seconds ago.
    • Network error, or firewall silently drops your packets (no ICMP replys are generated), You will have to wait until TCP times out the connection to get an error which can be many seconds, usually several minutes.
    • TCP is busy doing retransmission as you call send – maybe those retransmissions generate an error.(really the same as the first case)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to only close one end of a TCP socket to
I have a simple file transfer socket program where one socket sends file data
Why does this port/socket close once a connection has been made by a client?
In any socket API the close() method closes a connection with a FIN. If
I'm using a socket connection in PHP to post data to an Apache webserver.
I want to close a socket so I can reopen one on the same
Could I close tcp::socket in different thread from the sync-reading thread? It looks like:
How do you detect if Socket#close() has been called on a socket on the
If I open and close a socket by calling for instance Socket s =
How can I know when to close the socket of an HTTP client (I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.