I'm creating a socket program. The server is always listening to client request and send a response. When user click button in GUI, the client will send a request to server.
My problem is I want to notify the client that server has been closed when I click close button in GUI in server. I have come up with an idea that create a new connection to client always listen to server, but I think it is wasting resources.
server.py
while True:
try:
request = client.recv(BUFSIZE).decode('utf-8')
except ConnectionResetError:
logging.info(str(client.getsockname()) + ' has exited')
break
except Exception as e:
continue
if request:
try:
request = json.loads(request)
response = self.__handle_request(request, client).encode()
client.send(response)
client.py
# when user client button
sock.send(request)
response = sock.recv().decode()
render(response)
The client socket IS notified when the server socket closes. Your client's sock.recv
call will return a zero-length packet, the only instance in which it does so.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.