Multi-threaded TCP Socket Server
suggest changeWhen run with no arguments, this program starts a TCP socket server that listens for connections to 127.0.0.1 on port 5000. The server handles each connection in a separate thread.
When run with the -c argument, this program connects to the server, reads the client list, and prints it out. The client list is transferred as a JSON string. The client name may be specified by passing the -n argument. By passing different names, the effect on the client list may be observed.
Server Output
$ python client_list.py
Starting server...
Client Output
$ python client_list.py -c -n name1
{
"name1": {
"address": "127.0.0.1",
"port": 62210,
"name": "name1"
}
}
The receive buffers are limited to 1024 bytes. If the JSON string representation of the client list exceeds this size, it will be truncated. json.loads then raises json.JSONDecodeError, which is a ValueError subclass. One truncated string produced:
Unterminated string starting at: line 1 column 1023 (char 1022)
The column numbers depend on where the text was cut. The client_list.py source is not on this page, and this transcript was not re-run.