Python Forum

Full Version: multi-threaded tcp server-newbie
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I simply call it data exchange server. It does not fit into definitions just mentioned above. The server simply accepts data from one client, manipulates it and sends the data to other clients and so on.
So, you're going to design your own protocol for this?

I feel that you need to nail down what your server is in fact going to be doing, in detail (it all seems a little vague to me) before you move on to having multiples of said running, which, on the face of it, seems like less of a task than getting the services and protocol up and running over a tcp connection.

There's a good tutorial on the this Forum site (just in case you've not seen it) https://python-forum.io/thread-5542.html

... which could be a good place to start.

Other resources I can offer include...
https://www.tutorialspoint.com/python/py...orking.htm
https://www.w3schools.in/python/network-programming
https://pythongeeks.org/networking-in-python/
You may want to look at that library.

https://zeromq.org/languages/python/
Here is my code below that estabishes 6 sockets bearing port No. 1000 to 6000. I am able to recieve the data from client connected to port 1000. However for other clients, I am unable to recieve data in server.
#!usr/bin/python
import threading
from threading import *
import socket
import sys

def clientthread(conn):
    buffer=""
    while True:
        data = conn.recv(8192)        
        buffer=data.decode('utf-8')
        print (buffer)
        print("data recieved")
    #conn.sendall(reply)
    conn.close()

def main():
    try:
        host = '10.102.3.16'
        port = 1000
        tot_socket = 6
        list_sock = []
        for i in range(tot_socket):
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
            s.bind((host, (port*(i+1))))
            s.listen(10)
            list_sock.append(s)
            print ("[*] Server listening on %s %d" %(host, (port*(i+1))))

        while 1:
            for j in range(len(list_sock)):
                conn, addr = list_sock[j].accept()
                print ('[*] Connected with ' + addr[0] + ':' + str(addr[1]))
                threading.Thread(target=clientthread,args=(conn,)).start()
                print(threading.active_count())
        s.close()

    except KeyboardInterrupt as msg:
        sys.exit(0)


if __name__ == "__main__":
    main()
(Oct-30-2022, 04:33 PM)gary Wrote: [ -> ]Here is my code below that estabishes 6 sockets bearing port No. 1000 to 6000. I am able to recieve the data from client connected to port 1000. However for other clients, I am unable to recieve data in server.

Hosts typically allocate dynamic port numbers starting at 1024: numbers below 1024 are reserved.

Also (and I'm a little surprised that a Mod has not picked up on this): please post your code, formatted. Not doing so is not only a pain for anyone else trying to assists, but it can also lead to confusion.
I had a similar problem, you want to start a new thread per connection, as a while loop?

That way worked for me, hope it helps you.

    def handleConnected(self):
        self.thread_stop = False
        print(self.address, 'connected')
        if self not in wss:
            wss.append(self)
        for i in wss:
            print("client was added")
            print(wss)

            # Start a thread per connection
            t2 = threading.Thread(target=self.temp_controll, args=(512, 'get')) # (address, value) #, daemon= True)
            t2.start()
Pages: 1 2