May-16-2019, 08:49 PM
(This post was last modified: May-16-2019, 08:49 PM by LavaCreeperKing.)
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:
EDIT: I just tried to run my code for a test, and it looks like I get a connection, and then it just closed
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