Jun-03-2020, 02:28 PM
(This post was last modified: Jun-03-2020, 02:30 PM by Emekadavid.)
I was using this tutorial online which explains a simple client-server script for python networking. The server side ran well but when I closed the client connection, I expected the server side to also close its own connection because it was running on my windows machine. How do I do this? Here is the server code.
import socket
#create a socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#get local machine name
host = socket.gethostname()
port = 9999
#bind to the port
serversocket.bind((host, port))
#queue up to 5 requests
serversocket.listen(5)
while True :
#establish a connection
clientsocket, addr = serversocket.accept()
print('Got a connection from %s' % str(addr))
msg = 'Thank you for connecting'+ '\r\n'
clientsocket.send(msg.encode('ascii'))
clientsocket.close() #it closed the connection here and I want
#the server to stop listening so it shuts down its own socket
