Python Forum
Client server Multithreading
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Client server Multithreading
#1
Hello,

I have been working on creating a simple 3-client 1-server model to check and update the user-supplied text file. The server is doing this by comparing it with his own list of words.

All clients have been successfully connected and have got the updated file.

Now further I wanted to allow the server to keep on polling the clients after some 60 seconds to check if there are any new words in their queue and retrieve if any.

Could someone help as to what functionality the server needs to use to do this? I have been trying to use select but could not really properly make use of it. I know there should be some locking mechanism as well so that it does not affect the multi-threaded environment. But how exactly it can be accomplished?

NOTE: I am using python 3.7
Reply
#2
Firstly, I assume you're using sockets since I did not see any mention of a specific library. I would create a Server class and within it two main functions. The first receives and handles signals from the clients. The second one, which could be run within a thread, will every 60 seconds, ping each client to send information of the current status of their words, which the server can compare with it's own. Below is a little example of my idea (don't know if I made any typos in the code, but it's just for a general idea).

import sockets
import select
import threading
import time


class Server:
    def __init__(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.bind((IP, PORT))

        self.sockets = [self.socket]
        self.clients = []
        self.listening = False

        self.codes = {
            0: self.compare_with_client,
        }

    def run_recv(self):
        self.socket.listen()
        self.listening = True
        while self.listening:
            ready_socks, _, error_socks = select.select(self.sockets, [], self.sockets)
            for sock in ready_socks:
                if sock == self.socket:
                    client, address = self.s.accept()
                    self.socks.append(client)
                    self.clients.append(client)
                else:
                    # Receive info from socket
                    # Figure out code (meaning of message)
                    # Execute function based on code given by client
                    self.codes[code](message_data)


    def run_ping_client(self):
        while self.listening:
            time.sleep(60)
            if not self.listening:
                return
            for client in self.clients:
                # Ping client with code 0
                # Client will ping back under the same code

    def run(self):
        thread = threading.Thread(target=self.run_ping_client)
        thread.start()
        self.run_recv()

    def compare_with_client(self, data):
        # Do whatever with the data from the client
Reply
#3
Thank you so much for your help. I'll try to implement this idea in my code.
And yes, I am using sockets to connect my clients with the server.
I was thinking to store the elements in the client queue. THen convert the queue to a list and send it over to the socket.

Then the server will send a message after some time asking for some info. If the client has some words with it, it will respond.

Timer I am still confused, since:- If we use time.sleep(60), it just hangs the complete program.

Will it not affect the functionality?



Also, 1 more query... Can I make use of JSON to convert the entire list to bytes. if yes, then how can I send the JSON converted bytes over the socket?
send.encode() command does not work for it. It just gives the socket timeout error.
Reply
#4
run_ping_client is a thread, so time.sleep will only hold up the code running within that thread. Also yes, you can use json to easily send lists over the socket. json.dumps will take in a list and return a string which you can encode and send across the socket. json.loads will take in a string and turn it back into a list.
Anan likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Trouble with Client/Server reverse Shell! Gilush 0 269 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 493 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 1,556 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 969 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 857 Jun-02-2020, 01:08 PM
Last Post: nuffink
  how can i send a list of tuples from the server to the client using sockets? dafdaf 1 665 Apr-13-2020, 10:51 PM
Last Post: Larz60+
  Python script multi client server sonra 1 706 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  simple udp server/client cardmaker 2 1,297 Nov-26-2019, 12:36 AM
Last Post: micseydel
  Slow websockets server -> client pattagghiu 0 851 Sep-17-2019, 09:28 AM
Last Post: pattagghiu
  Chat (Client-Server) andresdrr 3 1,559 Aug-26-2019, 02:01 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

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