Python Forum
multi-threaded tcp server-newbie
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multi-threaded tcp server-newbie
#11
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.
Reply
#12
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/
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#13
You may want to look at that library.

https://zeromq.org/languages/python/
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
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()
Yoriz write Nov-19-2022, 05:08 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#15
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#16
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,520 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Python script multi client server sonra 1 2,465 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  Multi connection socket server help! MuntyScruntfundle 0 2,715 Feb-19-2019, 12:03 PM
Last Post: MuntyScruntfundle
  newbie here - need help with building very basic http server BenSalem 0 2,378 Aug-14-2017, 08:06 AM
Last Post: BenSalem

Forum Jump:

User Panel Messages

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