# Py - a TCP/IP single client echo server, also prints received data
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-10-29  initial release

# Linux   client: $ nc  localhost 5000
# Windows client: Putty | Host name = 192.168.0.16 | Port = 5000 |
#				  Connection type = other | Raw | Open
# Android client: TCP Client from Sollae Systems

import socket

# ============================ main ==============================

HOST	= '0.0.0.0'			# all interfaces
PORT	= 5000           	# listening port

def main():
    print('server running ...')
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as ss:	# IPv4 & stream
        ss.bind((HOST, PORT))										# bind unbound address									
        ss.listen(0)												# enable server, allowed unaccepted connections
        cs, addr = ss.accept()										# client socket object
        with cs:
            print('Connected by', addr)
            while True:
                data = cs.recv(1024)
                if not data: break
                cs.sendall(data)
                print('Rx:', data)
    print('connection lost')

main()

# ============================ end ===============================
