I guess Python
automatically send a close sign when shuts down(ctr+c) the client.
python tcp server
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 12345 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
#if not data:break
conn.sendall(data)
print(data)
python tcp client
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 12345 # The port used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', data)
#s.close()
When executing this code,
the server continues to receive null data (maybe close sign) by the client that ended without closing.
Connected by ('127.0.0.1', 55305)
b'Hello, world'
b''
b''
b''
b''
b''
...
This error occurs when run node.js
client and shutdown(ctr+c) without close sign.
The python tcp server occurs error but not print null.
node.js tcp client
const Net = require('net');
const client = new Net.Socket();
client.setEncoding('utf8');
client.connect({ port: 12345, host: '127.0.0.1' })
client.write('Hello, world');
/*client.end()*/
Connected by ('127.0.0.1', 56685)
b'Hello, world'
Traceback (most recent call last):
File "C:/.../server.py", line 13, in <module>
data = conn.recv(1024)
ConnectionResetError: [WinError 10054]An existing connection was forcibly closed by the remote host
I know that the server and the client need to send and receive the exit sign,
but I wonder why the Python client automatically sends the close sign when it is closed.
Where can I get information about this?
question from:
https://stackoverflow.com/questions/65878403/why-python-tcp-client-send-close-sign-automatically 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…