I am currently working on a simple school project on game server. I have one conceptual problem for dealing with the modified data in the TCPserver.
I have created a simple program for the issues. The clients will first type a number and the number will send to the server. This number will be processed in the server by adding 1 and the server will send the modified number back to clients. In addition, the modified number will also save to the totalNum
, which store all the sum of clients modified number.
I expect the server will "store" the clients number. For instance, when client A send number 5, the totalNum
would be 6. After that, when client B send number 8, client A data should still maintain in the sever, I expect the totalNum
to be 6 9 = 15.
Server.py
#!/usr/bin/python3
import socket
import threading
class ServerThread(threading.Thread):
def __init__(self, client):
threading.Thread.__init__(self)
self.client = client
def run(self):
connectionSocket, addr = self.client
totalNum = 0
number = connectionSocket.recv(1024)
newNumber = number.decode()
newNumber = int(newNumber)
editedNumber = newNumber 1
#add the edited number to the totalNum in the server
totalNum = editedNumber totalNum
print (totalNum)
#change back to str for sending
editedNumber = str(editedNumber)
connectionSocket.send(editedNumber.encode())
connectionSocket.close()
class ServerMain:
def server_run(self):
serverPort = 12000
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind( ("", serverPort) )
serverSocket.listen(5)
print("The server is ready to receive")
while True:
client = serverSocket.accept()
t = ServerThread(client)
t.start()
if __name__ == '__main__':
server = ServerMain()
server.server_run()
Client.py
#!/usr/bin/python3
import socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverName = "localhost"
serverPort = 12000
clientSocket.connect( (serverName, serverPort) )
# Get input for sending
number = input("Input a number:")
clientSocket.send(number.encode())
modifiednumber = clientSocket.recv(1024)
print("From Server:", modifiednumber.decode())
clientSocket.close()
updated server code(changing the totalNum
to class variable):
#!/usr/bin/python3
import socket
import threading
class ServerThread(threading.Thread):
totalNum = 0
def __init__(self, client):
threading.Thread.__init__(self)
self.client = client
def run(self):
connectionSocket, addr = self.client
number = connectionSocket.recv(1024)
newNumber = number.decode()
newNumber = int(newNumber)
editedNumber = newNumber 1
#add the edited number to the totalNum in the server
self.totalNum = editedNumber self.totalNum
print (self.totalNum)
#change back to str for sending
editedNumber = str(editedNumber)
connectionSocket.send(editedNumber.encode())
connectionSocket.close()
class ServerMain:
def server_run(self):
serverPort = 12000
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind( ("", serverPort) )
serverSocket.listen(5)
print("The server is ready to receive")
while True:
client = serverSocket.accept()
t = ServerThread(client)
t.start()
if __name__ == '__main__':
server = ServerMain()
server.server_run()
CodePudding user response:
Youre pretty close. move totalNum = 0
to be a class level variable, i.e.
class ServerThread(threading.Thread):
totalNum = 0
def __init__(self, client):
and anywhere you use totalNum
use it as ServerThread.totalNum
The problem was that you had totalNum as a scoped variable so it would get lost every time you finished run()
. the issue arises if we made totalNum
an instance variable. when run()
ends the thread ends and the value gets lost again! by making it a class variable its always there and always shared. you can also access it from outside the class with ServerThread.totalNum