Python Forum
Make sockest keep connection
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
Reply
#2
See see for a similar problem:
https://python-forum.io/Thread-File-Transfer
Reply
#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.
Reply
#4
Great.
Reply


Forum Jump:

User Panel Messages

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