Home > Mobile >  python socket / how do you detect disconnection from clients and detect number of connections per us
python socket / how do you detect disconnection from clients and detect number of connections per us

Time:10-03

this is for server authentication for my application. (im working on login function so dont mind about it) what i wanna do is to make server receive heartbeat from client and close client socket if its doesnt respond in a few min also i want to detect number of connections per user.

for receiving heartbeat, i can make the client send heartbeat constantly but how do you make the server decect it? i know time measurement is needed but if i put time.perf_counter() right before 'client_socket.recv(1024)' the counter function wont be executed because its waiting on receiving. so how would i solve this?

and im also trying to make it detect number of connections per user. (5 maximum connections per user) for detection, i give username 1 when a user is connected and give -1 when the user disconnects but im not sure if the method im doing is correct or a good way to do so.

i'd be appreciated if you could help me out

------------------------server----------------------------    

import socket 
from _thread import *
import sys
import time


username = ['test123', 'hongengi']
userconnect= 0


def threaded(client_socket, addr): 
    print('Connected by :', addr[0], ':', addr[1]) 


    while True: 

        try:

            data = client_socket.recv(1024)
            print (data.decode())
            print('Received from '   addr[0],':',addr[1] , data.decode())

            if data.decode() == ".": # heartbeat
                heartbeat = time.perf_counter()
                print ("heartbeat")

            if data.decode() == "test123":
                print ("login success")
                userconnect == userconnect   1    
                
            
            if not data:
                print ("no data / disconnect ")
                print('Disconnected by '   addr[0],':',addr[1])
                userconnect == userconnect - 1 
                break

            client_socket.send(data) 

        except (ConnectionResetError, socket.error) as e:
            print ("error occurs")

            print('Disconnected by '   addr[0],':',addr[1])
            userconnect == userconnect - 1 
            break
             
    client_socket.close() 


HOST = '127.0.0.1'
PORT = 5000

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT)) 
server_socket.listen() 

print('server start')


while True: 

    print('wait')


    client_socket, addr = server_socket.accept() 
    start_new_thread(threaded, (client_socket, addr)) 

server_socket.close()





------------------------client---------------------------- 

import socket

SERVER_IP = 'localhost'
SERVER_PORT = 5000
SIZE = 100
SERVER_ADDR = (SERVER_IP, SERVER_PORT)
heartbeat = "."
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(SERVER_ADDR)
#username = "test123"
#userpass = "123123"

while True:

    client_socket.send(heartbeat.encode())
    msg = client_socket.recv(SIZE)
    print (msg.decode())

CodePudding user response:

One end of a socket is never "notified" when the other socket closes. There is no direct connection, so the only way to tell this is to time out. You can use socket.timeout to establish a timeout time. Your recv will then return with 0 bytes, and that's an indication that your timeout expired.

How to set timeout on python's socket recv method?

  • Related