Python Forum
How can I make this server stop listening for connections to sockets
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make this server stop listening for connections to sockets
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to make this error stop ? Mawixy 1 3,329 Apr-19-2022, 03:02 PM
Last Post: Mawixy
  How to take the tar backup files form remote server to local server sivareddy 0 1,917 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Listening music from url ebolisa 1 1,677 Nov-25-2020, 07:53 AM
Last Post: ebolisa
  Waiting and listening test 2 2,156 Nov-13-2020, 04:43 PM
Last Post: michael1789
  Python sockets : Client sending to the server nico31780 0 2,350 May-17-2020, 07:56 PM
Last Post: nico31780
  Use Python to start/stop a server service via a webform? oakleaf2001 0 1,762 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  Sockets confusion! MuntyScruntfundle 1 2,221 Oct-16-2018, 07:18 PM
Last Post: Larz60+
  Need small help on dynamic database connections Tirumal 4 3,472 Dec-28-2017, 07:02 PM
Last Post: buran
  listening to USB DPaul 3 5,356 Dec-27-2017, 07:31 AM
Last Post: DPaul
  Array of Listening Sockets abarnes 1 2,625 Sep-28-2017, 07:26 PM
Last Post: abarnes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020