A client server implementation in python for the number guess problem. The server chooses a random number. The clients connect and send numbers to server. The server returns to each client a status message:

·         ‘H’ – send a larger number

·         ‘S’ – send a lower number

·         ‘G’ – you guessed my number

·         ‘L’ – another client guessed the number. You are a looser !

 

The implementation bellow is a concurrent multithreaded - didactic approach to the problem – not checking for every possible error. Also pay attention to thread synchronization mechanisms and the way a simple solution becomes complicated when threads need to be synchronized

Client
 
Server 
 

__author__ = 'dadi'
import socket, struct, random,sys, time


if __name__ == '__main__':
   
try:
        s = socket.create_connection( (
'localhost',1234))
   
except socket.error as msg:
       
print("Error: ",msg.strerror)
       
exit(-1)

    finished=
False
   
sr = 1; er=2**17-1
   
random.seed()

    data=s.recv(
1024)
   
print(data.decode('ascii'))
    step_count=
0
   
while not finished:
        my_num=random.randint(sr,er)
       
try:
            s.sendall( struct.pack(
'!I',my_num) )
            answer=s.recv(
1)
       
except socket.error as msg:
           
print('Error: ',msg.strerror)
            s.close()
           
exit(-2)
        step_count+=
1
       
print('Sent ',my_num,' Answer ',answer.decode('ascii'))
       
if answer==b'H':
            sr = my_num
       
if answer==b'S':
            er = my_num
       
if answer==b'G' or answer==b'L':
            finished=
True
       
time.sleep(0.25)

    s.close()
   
if answer==b'G':
       
print("I am the winner with",my_num,"in", step_count,"steps")
   
else:
       
print("I lost !!!")
#    input("Press Enter")

 

 

__author__ = 'dadi'

import socket
import threading
import random
import struct
import sys
import time

random.seed()
start=
1; stop=2**17-1
my_num= random.randint(start,stop)
print('Server number: ',my_num)
mylock = threading.Lock()
client_guessed=
False
winner_thread=0
e = threading.Event()
e.clear()
threads = []
client_count=
0

def worker(cs):
   
global mylock, client_guessed, my_num, winner_thread, client_count,e

    my_idcount=client_count
   
print('client #',client_count,'from: ',cs.getpeername(), cs )
    message=
'Hello client #'+str(client_count)+' ! You are entering the number guess competion now !'
   
cs.sendall(bytes(message,'ascii'))

   
while not client_guessed:
       
try:
            cnumber=cs.recv(
4)
            cnumber=struct.unpack(
'!I',cnumber)[0]
           
if cnumber > my_num:
                cs.sendall(
b'S')
           
if cnumber < my_num:
                cs.sendall(
b'H')
           
if cnumber== my_num:
                mylock.acquire()
                client_guessed=
True
                
winner_thread=threading.get_ident()
                mylock.release()

       
except socket.error as msg:
           
print('Error:',msg.strerror)
           
break

    if
client_guessed:
       
if threading.get_ident() == winner_thread:
            cs.sendall(
b'G')
           
print('We have a winner', cs.getpeername())
           
print("Thread ",my_idcount," winner")
            e.set()
       
else:
            cs.sendall(
b'L')
           
print("Thread ",my_idcount," looser")
    time.sleep(
1)
    cs.close()
   
print("Worker Thread ",my_idcount, " end")


def resetSrv():
   
global mylock, client_guessed, winner_thread, my_num, threads,e, client_count
   
while True:
        e.wait()
       
for t in threads:
            t.join()
       
print("all threads are finished now")
        e.clear()
        mylock.acquire()
        threads = []
        client_guessed =
False
       
winner_thread=-1
       
client_count = 0
       
my_num = random.randint(start,stop)
       
print('Server number: ',my_num)
        mylock.release()


if __name__=='__main__':
   
try:
        rs=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        rs.bind( (
'0.0.0.0',1234) )
        rs.listen(
5)
   
except socket.error as msg:
       
print(msg.strerror)
        
exit(-1)
    t=threading.Thread(
target=resetSrv, daemon=True)
    t.start()
   
while True:
        client_socket,addrc = rs.accept()
        t = threading.Thread(
target=worker, args=(client_socket,) )
        threads.append(t)
        client_count+=
1
        
t.start()