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


Messages In This Thread
Make sockest keep connection - by LavaCreeperKing - May-16-2019, 08:49 PM
RE: Make sockest keep connection - by heiner55 - May-20-2019, 03:30 AM
RE: Make sockest keep connection - by heiner55 - May-21-2019, 05:05 AM

Forum Jump:

User Panel Messages

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