Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103510

tcp networking question (CLOSE_WAIT)

X-Received by 10.66.182.225 with SMTP id eh1mr42235331pac.24.1456422950186; Thu, 25 Feb 2016 09:55:50 -0800 (PST)
X-Received by 10.50.2.36 with SMTP id 4mr106746igr.2.1456422950151; Thu, 25 Feb 2016 09:55:50 -0800 (PST)
Path csiph.com!feeder.erje.net!2.us.feeder.erje.net!news.glorb.com!hb3no8985315igb.0!news-out.google.com!pn7ni2172igb.0!nntp.google.com!hb3no8985312igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Thu, 25 Feb 2016 09:55:49 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=67.82.52.205; posting-account=lOEuBgoAAAD6Qw4dpoqeBAJHKoD58tcK
NNTP-Posting-Host 67.82.52.205
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <7ff15c67-102e-4b02-a2c8-87b24f1b8351@googlegroups.com> (permalink)
Subject tcp networking question (CLOSE_WAIT)
From Ray <rui.vapps@gmail.com>
Injection-Date Thu, 25 Feb 2016 17:55:50 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:103510

Show key headers only | View raw


hi,

I'm new to python networking. I am waiting TCP server/client app by using python built-in SocketServer. My problem is if client get killed, then the tcp port will never get released, in CLOSE_WAIT

maybe I didn't do the handler right? or anyway I can catch the client get killed?

I wrote following simple code to explain the issue I have, server listen on 127.0.0.1:1234
client need to send multi-messages (I am sending 2 in this test) if client get killed (I just do kill client_pid) then if I check lsof, I will see the new allocated tcp do not get released 

########server.py########

#!/usr/bin/python
import threading
import SocketServer
import time

class TCPServerRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        while True:
            data=self.request.recv(1024)
            if not data:
                time.sleep(0.1)
                continue
            print data
            self.request.send(data)
            if data=='end':
                break

def main():
    server = TCPServer(('127.0.0.1', 1234), TCPServerRequestHandler)
    ip, port = server.server_address
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    time.sleep(600)
    server.shutdown()
    server.server_close()
if __name__=='__main__':
    main()

########client.py########
import socket
import time

def client():
    sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('127.0.0.1', 1234))
    sock.sendall('hello')
    response=sock.recv(1024)
    print "Received:", response
    time.sleep(60)
    sock.sendall('end')
    response=sock.recv(1024)
    print "Received:", response
    sock.close()

if __name__=='__main__':
    client()


when I run it, client has 60 seconds sleep, so I just kill the process from OS
kill 93948

then I'm running lsof to find my TCP/UDP 
lsof -n -p $(ps -ef|grep serve[r]|awk '{print $2}')|egrep '(TCP|UDP)'|awk '{print $NF}'

TCP 127.0.0.1:search-agent (LISTEN)
TCP 127.0.0.1:search-agent->127.0.0.1:59411 (CLOSE_WAIT)

I will see the CLOSE_WAIT, this is where the client get killed.


any help would be great!

Thanks a lot!
 

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 09:55 -0800
  Re: tcp networking question (CLOSE_WAIT) "Martin A. Brown" <martin@linux-ip.net> - 2016-02-25 10:17 -0800
    Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:25 -0800
      Re: tcp networking question (CLOSE_WAIT) "Martin A. Brown" <martin@linux-ip.net> - 2016-02-25 10:56 -0800
        Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:59 -0800
  Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:44 -0800

csiph-web