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
#5
Hi There,
Is Socket library supports python 3.9 or vice-a-versa?
I have installed python 3.9 and while installing socket it giving error "No matching distribution found".
Reply
#6
(Apr-20-2021, 07:20 AM)sankarachari Wrote: Hi There,
Is Socket library supports python 3.9 or vice-a-versa?
I have installed python 3.9 and while installing socket it giving error "No matching distribution found".

Yes, it is supported.. did you do import socket before using any socket functionality?
Reply
#7
The socket library should come with Python download iirc, so you shouldn't have to use pip to install it. Also, you should make a new thread next time rather than using one on a separate question.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,274 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,421 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,360 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
Question Trouble with Client/Server reverse Shell! Gilush 0 2,756 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,191 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,311 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,335 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 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 3,826 Apr-13-2020, 10:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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