0

I am trying to create a listener, connect clients and after sometime force closing the socket and kill the connection. I am not able to close the connection even by trying to close the socket itself. I am not sure what i am doing wrong. My code has a server side code and a client side code (2 different terminals)

the server

from multiprocessing.connection import Listener
import threading, socket


def start_thread(name,  target,  args=(),  kwargs={},  daemon=True):
    thread = threading.Thread(name=name,  target=target, args=args,  kwargs=kwargs)
    thread.daemon = daemon
    thread.start()
    return thread


# client
received = []
def child(conn):
    while True:
        msg = conn.recv()
        received.append(msg)
        # this just echos the value back, replace with your custom logic
        #import time
        #time.sleep(5)
        conn.send(msg)

# server
serv = Listener(('', 5000), authkey='mypassword')
def mother(serv):
    while True:
        client = serv.accept()
        print(client, type(client))
        t = start_thread('child', child, kwargs={'conn':client})
        print(t)

start_thread('mother',mother, (serv,))

The client

from multiprocessing.connection import Client

c = Client(('localhost', 5000), authkey='mypassword')

c.send('hello');print( c.recv())

c.send({'a': 123}); print('i am back', c.recv())

Now on the server side, without closing the terminal or exiting python, i would like to stop the listener and close the socket. by trying all the following (separately in different runtimes) nothing worked and the connection was still open

1 - serv.close()
2 - serv._listener.close()
3 - serv._listener._socket.close()

# Shutdown the socket always returns an error
4 - socket.socket.shutdown(serv._listener._socket, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 174, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor


5 - serv._listener._socket.shutdown(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 174, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
Cobry
  • 4,348
  • 8
  • 33
  • 49

1 Answers1

0

It is a socket relative problem.

In short, you cannot shutdown after close because close will destroy a socket (file descriptor).

Details in this and this.

Hao Li
  • 1,593
  • 15
  • 27
  • thank you for your answer. Actually I didn't call shutdown after call. Those are all the methods i've tried separately on different executions. I will rephrase my question for less confusions – Cobry Oct 29 '18 at 18:31