Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Make sockest keep connection
#1
I am trying to set up a network server. Here are some bits of the code that make the connection for the client and server. This is all just for a test.

Server:
port = 50008
host = 'localhost'

class ClientThread(QtCore.QThread):#manages connection to a client.
    def __init__(self,name,connection):
        super(ClientThread, self).__init__()#are QThreads better than built in python threads?
        self.active = True
        self.data = 'None'
        self.conn = connection
    def run(self):
        while self.active:
            self.conn.send(self.data.encode())
            data = self.conn.recv(1024)
            if self.conn.close:
                print('connection terminated')
                self.active = False

sock = socket(AF_INET, SOCK_STREAM)
sock.bind(('',port))
sock.listen(1)
while True:
    conn, addr = sock.accept()
    ct = ClientThread('dummy',conn)
    ct.start()
Client:
port = 50008
host = 'localhost'

class ServerThread(QtCore.QThread):# manages connection to the server.
    def __init__(self,name,connection):
        super(ServerThread, self).__init__()#are there any advantages to using QThreads?
        self.active = True
        self.data = 'none'
        self.sock = connection
    def run(self):
        while self.active:
            data = self.sock.recv(1024)
            self.sock.send(self.data.encode())
            if self.sock.close:
                print('connection terminated')
                self.active = False


sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host,port))
st = ServerThread('dummy',sock)
st.start()
So the point is I connect to the server, but the connection only last until data is sent or received by the client or the server. In this setup the client and server both send and receive data, but after that happens the connection is closed. How do I keep the connection open for continuous transmission? For example, if I make a two player game(one person on one computer one on another) how do I keep the connections open so every time someone does something, that data gets sent to the server, processed, and sent to the other clients so they can update? I don't want someone to connect, make a move, get disconnected, then have someone else connect and take the place of who ever got disconnected. So how can I get the connection to remain active until the user closes it? Also, how would I prevent other connections to the server once I have the max amount of players?

EDIT: I just tried to run my code for a test, and it looks like I get a connection, and then it just closed
Quote
#2
See see for a similar problem:
https://python-forum.io/Thread-File-Transfer
Quote
#3
Never mind, I found out what is going on. It is not similar to that other question. What is going on is for some reason in order to get a loop to process the socket I need to pass the socket argument directly to the function that creates the loop. So by passing the socket to the Thread __init__ function and making it an attribute of the thread was not working. Instead I have to call the run function directly and pass the socket to it. But doing that wont make the thread work properly so I can't set up a thread like that. Instead I have to use _thread and call _thread.start_new_thread to call a function that creates a loop to manage the connection and pass the socket to that function.
Quote
#4
Great.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 247 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  Multi connection socket server help! MuntyScruntfundle 0 327 Feb-19-2019, 12:03 PM
Last Post: MuntyScruntfundle
  No connection could be made becouse the target machine actively refused it. GottaAimHigherPal 1 580 Oct-18-2018, 01:59 PM
Last Post: Larz60+
  Need Help in ssh connection hareesh 3 759 Oct-04-2018, 07:59 AM
Last Post: hareesh
  tcp server/client port connection issue valshev 2 1,335 Sep-01-2018, 02:50 PM
Last Post: valshev
  WebRTC connection from the server Borisko 1 1,439 May-20-2018, 10:15 AM
Last Post: Borisko
  Python // C # - SOCKET connection is constantly interrupted raspberryPiBRA 0 810 Feb-01-2018, 09:53 AM
Last Post: raspberryPiBRA
  Decode data of udp connection deividnastri 2 1,843 Oct-24-2017, 05:12 PM
Last Post: deividnastri
  scapy ssh connection my network sniffing from my router Boss 1 2,537 Feb-13-2017, 07:04 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)