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 7690559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:25:30+00:00 2026-05-31T20:25:30+00:00

im doing some basic socket programming in python. But im having some trouble figuring

  • 0

im doing some basic socket programming in python. But im having some trouble figuring out how my echoserver could notice that the echoclient has closed it’s connection, without a constant ping/pong or timeout.

echoclient.py

#echoclient
import sys
import socket
import os
import datetime

def log(s):
    return datetime.datetime.now().strftime('%H:%M:%S') + ' - ' + s

s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)

s.connect(('127.0.0.1', 9000))

while 1:
    try:
        foo = raw_input('what do you want to send?: ')
        if len(foo) > 0:
            s.send(foo)
    except KeyboardInterrupt:
        print 'shutting down connection'
        s.close()
        break
    data = s.recv(1024)
    if data:
        print log('Recieved'), repr(data)

echoserver.py

#echoserver
import sys
import socket
import os
import datetime

def log(s):
    return datetime.datetime.now().strftime('%H:%M:%S') + ' - ' + s

port = 9000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', port))
s.listen(5)

while 1:
    conn, addr = s.accept()
    conn.settimeout(10)
    print 'Connected by', addr
    while 1:
        try:
            data = conn.recv(1024)
            if data:
                print log(repr(data))
                conn.send(data)
        except KeyboardInterrupt:
            print 'Closing connection ', addr
            conn.close()
            sys.exit(1)
        except:
            print 'Exception caught! closing connection ', addr
            conn.close()
            break           

Now the only way the server knows that the client has died is by the conn.settimeout(10)

  1. How can i get a exception for my server when the client disconnects preferably without a constat ping/pong message between them or settimeout.

  2. My next step is to create a echoproxy between the echoclient and echoserver, will i need to use threads/multiprocessing to achieve this? Any tips or pointers?

  3. Anything else you have to comment about bad/good practices is welcome

Thanks!

  • 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-31T20:25:32+00:00Added an answer on May 31, 2026 at 8:25 pm

    It was my understanding the socket library already gets a message when clients disconnect.

    Strictly speaking, you’re supposed to use shutdown on a socket before you close it. The shutdown is an advisory to the socket at the other end. Depending on the argument you pass it, it can mean “I’m not going to send anymore, but I’ll still listen”, or “I’m not listening, good riddance!”. Most socket libraries, however, are so used to programmers neglecting to use this piece of etiquette that normally a close is the same as shutdown(); close(). So in most situations, an explicit shutdown is not needed.

    The server can detect “EOF” by a receive of 0 bytes. It can assume it has the complete request. The server sends a reply. If the send completes successfully then, indeed, the client was still receiving.

    When the other ends disconnects abruptly (like a network fault or crash) that’s when the timeout comes into play.

    In your server code above you should be able to immediately detect client disconnects using:

    if data:
       # process data
    else:
       # client disconnected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a little trouble with some basic trig. I'm doing some math homework,
I am doing some basic Socket messaging. I have a routine that works well
I was doing some basic audio programming in C# using the NAudio package and
I seem to be having problems doing some basic parsing of an XML document
I must be missing something here, but I seem to be having some trouble
I'm new to programming, I have some basic python programming from college, I am
I'm doing some work for a department that has a pretty basic table structure
I'm now doing some work with decoding MP3 files, but just have some basic
I've been doing some very basic SNMP4J programming. All I want to do is
While doing some random experimentation with a factorial program in C, Python and Scheme.

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.