user3103359
user3103359

Reputation: 11

How can I know the number of clients connected to the server and return the number of the connected clients to the user?

I want to know these for I am getting crazy with this:

How can I do these:

1-If the server terminates the clients should terminate also. Your server should allow the administrator to close all connections (i.e. the server must wait for the user to terminate the program preferably through a menu interface)

2-In order to know the number of clients connected you will need to identify each client uniquely – this can be accomplished by using the pid that is uniquely assigned for each client connection (store these in a global list). These connections can change dynamically (i.e. clients can disconnect and reconnect) so you must maintain this list on the server. This is my server side code:

Thanks in advance

import _thread
import socket
import sys
from datetime import datetime



def serveclient(c):
    global v, nclient, vlock, nclientlock
    while(True):
        k=(c.recv(1)).decode('utf-8')
        if(k==''):
            break
        if(k=='D'):
            today = str(datetime.now().strftime('%Y-%m-%d'))
            c.send(today.encode('utf-8'))
        if(k=='T'):
            tme = str(datetime.now().strftime('%H:%M:%S'))
            c.send(tme.encode('utf-8'))
        if(k=='X'):
                   <<<< # Here I should put the number of clients connected and echo   back the number like the above code
        vlock.acquire()
        v+=k
        vlock.release()

        #Echo back
        c.send(v.encode('utf-8'))
    c.close()   #End connection
    nclientlock.acquire()
    nclient-=1
    nclientlock.release()

#Main driver code
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = int(sys.argv[1])
listener.bind(('',port))
listener.listen(5)

#Initialize global data
v=''
vlock=_thread.allocate_lock()
nclient=10       #Max number of clients
nclientlock=_thread.allocate_lock()

#accept calls from clients
for i in range(nclient):
    (client, ap) = listener.accept()
    _thread.start_new_thread(serveclient, (client,))

listener.close()
while nclient >0:
    pass    #do nothing, just wait for client count to drop to zero

print('The final string is: ', v)

<<<<<<<<<<<<<<<<<<<<<<<<<This the Client Code>>>>>>>>>>>>>>>>>>>>>>>

#Client Program. Sends a single char at a time to the server until the client
    #sends a '', this will terminate the client.
    #
    #usage: python server port

    import socket
    import sys

    #create the socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    host = sys.argv[1]      #Server info from cmd line
    port = int(sys.argv[2]) #Port from cmd line

    #Conncet to server
    s.connect((host, port))

    while(True):
        #get letter
        k = input('enter a letter: ')
        s.send(k.encode('utf-8'))
        if(k==''):
            break
        v=s.recv(1024)  #receive upto 1024 bytes
        print(v.decode('utf-8'))

    s.close()

Upvotes: 1

Views: 5465

Answers (1)

user207421
user207421

Reputation: 310913

Just increment a count every time you accept a socket, and decrement it every time you close an accepted socket.

Upvotes: 1

Related Questions