rolgordijn
Posts: 8
Joined: Sun Mar 26, 2017 2:06 pm

keep TCP socket server running?

Tue Feb 12, 2019 7:49 am

The example TCP socket server on
https://wiki.python.org/moin/TcpCommunication works just fine for me, except if the client stops sending data to the server, the server socket script stops running, so you can't restart the client without restarting the server.

I added a second while loop so that the server waits on "s.accept" to accept a new connection but that doesn't work. How kan i keep the server alive? (One connection at a time is sufficient, no need for multiple connections using threads)...

Code: Select all


import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20  # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
while 1:
   try:
       conn, addr = s.accept()
       while 1:
           data = conn.recv(BUFFER_SIZE)
           conn.send(data)  # echo
   except:
           conn.close()

User avatar
PeterO
Posts: 4464
Joined: Sun Jul 22, 2012 4:14 pm

Re: keep TCP socket server running?

Tue Feb 12, 2019 9:28 am

Look at the examples in the python documentation, they do what you want ....
https://docs.python.org/3/library/socket.html
Examples are that the bottom of the page.

PeterO

Code: Select all

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data: break
            conn.sendall(data)
            
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Aydan
Posts: 647
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: keep TCP socket server running?

Wed Feb 13, 2019 2:08 pm

@rolgordijn:

once a socket has been closed, youcan't reopen it.
So move these three lines into the while loop and you should be good:

Code: Select all

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
Regards
Aydan

User avatar
PeterO
Posts: 4464
Joined: Sun Jul 22, 2012 4:14 pm

Re: keep TCP socket server running?

Wed Feb 13, 2019 2:12 pm

Aydan wrote:
Wed Feb 13, 2019 2:08 pm
@rolgordijn:

once a socket has been closed, youcan't reopen it.
So move these three lines into the while loop and you should be good:

Code: Select all

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
Regards
Aydan
NO that's not right,. The listening socket shouldn't be closed and reopened, it will probably fail due to timeouts associated with the tcp state.
(ISTR SO_LINGER has something to do with this).

The accept call is all that needs to be put within the loop.

Code: Select all

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(1)
    while True:
        print("Waiting for connection")
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr)
            while True:
                data = conn.recv(1024)
                if not data: break
                conn.sendall(data)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Aydan
Posts: 647
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: keep TCP socket server running?

Wed Feb 13, 2019 2:41 pm


Heater
Posts: 11399
Joined: Tue Jul 17, 2012 3:02 pm

Re: keep TCP socket server running?

Wed Feb 13, 2019 3:36 pm

Aydan,
once a socket has been closed, you can't reopen it.
So move these three lines into the while loop and you should be good:
No. That is not correct. Don't do that.

You are missing the point that:

Code: Select all

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
Creates a socket and get's it listening on an address/port.

The missing part is that:

Code: Select all

conn, addr = s.accept()
Creates a new socket when an incoming connection is established.

It is that second socket that is closed when either the server or the client has done it's thing. Then one can loop around and call accept again.

In fact normally one should be prepared to accept multiple incoming connections, each on their own new socket, at the same time. But that gets us into talk of spinning up a new thread to handle each new connection or using some kind of event based system.

However, in simple one to one connections that complexity is not required.

Return to “Python”