Deny "steal" the port by another application

I have a small daemon application that listens on a specific port, and it happens that another application uses the same port, somehow removing it from my application.

Before starting this other application, everything works fine, as soon as I start it, I no longer receive the connection, but closing it, the daemon continues to work again. I did not know that you can use the port when another application is listening on it, and would like to prevent this, if possible.

It is also interesting that when the daemon restarts, even if an exception occurs, it already works, it can create, bind and listen on this port, it just does not work as it should.

Here will be the main loop code:

if __name__ == '__main__':
    print "Daemon PID:", os.getpid()
    server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    server.bind(('localhost', local_port))
    server.listen(10)
    print "Server listening on:", server.getsockname()

    while True:
        thread.start_new_thread(handle_client, server.accept())

(local_port handle_client , - Debian GNU/Linux 7, Python - 2.7.3)


:

:

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN     25422/python

VLC

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      25422/python    
tcp6       0      0 :::9000                 :::*                    LISTEN      25447/vlc       
tcp6       0      0 ::1:9000                ::1:46156               ESTABLISHED 25447/vlc       
tcp6       0      0 ::1:46157               ::1:9000                ESTABLISHED -               
tcp6       0      0 ::1:46156               ::1:9000                ESTABLISHED -               
tcp6       0      0 ::1:9000                ::1:46157               ESTABLISHED 25447/vlc

, TCP TCP6. , VLC, , , VLC TCP-.

+4
1

, - IPv4, VLC IPv6. , , IPv4 ; , IPv6 IPv4 . , IPv6 ( ) IPv4 AF_INET6 'ip6-localhost'.

+2

Source: https://habr.com/ru/post/1539063/


All Articles