title: Stop server from client's thread / Modify server's variable from client's thread
body: <p>I would like to write an application that could stop the server based on client's input. The server is multi-threaded and I do not understand how can I do this. </p>
  
  <p>Basically, I described my problem here: <a href="https://stackoverflow.com/questions/43757999/modify-servers-variable-from-clients-thread-threading-python">Modify server&#39;s variable from client&#39;s thread (threading, python)</a>.</p>
  
  <p>However, this is the Python solution, not the general solution I could implement in Java, C, C++, etc.</p>
  
  <p>I need to close other clients, when one of them guesses the number, but the server should be still alive, ready for the new game. </p>
  
  <p>Can I ask for some advices, explanations?</p>
  
  <p>I tried this (still do not know how to port it to C or Java), but it lets the clients send the numbers even if one of them just guesses it. It seems to me that <code>kill_em_all</code> does not do it's job, it does not close all the connections and does not disconnect the other clients as it should. How to improve this?</p>
  
  <pre><code>#!/usr/bin/env python
  
  from random import randint
  import socket, select
  from time import gmtime, strftime
  import threading
  import sys
  
  class Handler(threading.Thread):
      def __init__(self, connection, randomnumber, server):
          threading.Thread.__init__(self)
          self.connection = connection
          self.randomnumber = randomnumber
          self.server = server
  
      def run(self):
          while True:
              try:
                  data = self.connection.recv(1024)
  
                  if data:
  
                      print(data)
  
                      try:
                          num = int(data)
  
                          if self.server.guess(num) :
                              print 'someone guessed!'
                              self.server.kill_em_all()
                              break
                          else :
                              msg = "Try again!"
                              self.connection.sendall(msg.encode())
  
  
                      except ValueError as e:
                          msg = "%s" % e
                          self.connection.sendall(msg.encode())
                  else:
                      msg = "error"
                      self.connection.send(msg.encode())
  
              except socket.error:
                  break
          self.connection.close()
  
      def send(self, msg):
          self.connection.sendall(msg)
  
      def close(self):
          self.connection.close()
  
  
  class Server:
      randnum = randint(1,100)
      def __init__(self, ip, port):
          self.ip = ip
          self.port = port
          self.address = (self.ip, self.port)
          self.server_socket = None
  
      def guess(self, no):
          if self.randnum == no:
              self.randnum = randint(1, 100)
              print("New number is ", self.randnum )
              result = True
          else:
              result = False
          return result
  
      def kill_em_all(self):
          for c in self.clients:
              c.send("BYE!")
              c.close()
  
      def run(self):
          try:
              self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              self.server_socket.bind((self.ip, self.port))
              self.server_socket.listen(10)
              self.clients = []
  
              print('Num is %s' % self.randnum)
  
              while True:
                  connection, (ip, port) = self.server_socket.accept()
  
                  c = Handler(connection, self.randnum, self)
                  c.start()
                  self.clients.append(c)
  
          except socket.error as  e:
              if self.server_socket:
                  self.server_socket.close()
              sys.exit(1)
  
  
  if __name__ == '__main__':
      s = Server('127.0.0.1', 7777)
      s.run()
  </code></pre>
  
  <p>Client code:</p>
  
  <pre><code>import socket
  import sys
  
  port = 7777
  s = None
  try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      host = socket.gethostname()
      s.connect(('127.0.0.1', port))
  except socket.error, (value, message):
      if s:
          s.close()
      print "Could not open socket: " + message
      sys.exit(1)
  while True:
      data = raw_input('&gt; ')
      s.sendall(data)
      data = s.recv(1024)
      if data:
          if data == "BYE!":
              break
          else:
              print "Server sent: %s " % data
  s.close()
  </code></pre>