In this tutorial we will create the server for our game. The server is responsible for handling all of the different clients that are playing our game. It will store and send information to each of clients appropriately.
Finding The Server IP Address
For this tutorial we will be creating a server on our local network. This means that people outside our network will not be able to connect.
To find the address that you should use in the server variable simply open cmd > type "ipconfig" > copy the IPV4 Address. Do this on whatever machine will be running the server script.
importsocketfrom_threadimport*importsysserver="IPV4 ADDRESS HERE"port=5555s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)try:s.bind((server,port))exceptsocket.errorase:str(e)s.listen(2)print("Waiting for a connection, Server Started")defthreaded_client(conn):conn.send(str.encode("Connected"))reply=""whileTrue:try:data=conn.recv(2048)reply=data.decode("utf-8")ifnotdata:print("Disconnected")breakelse:print("Received: ",reply)print("Sending : ",reply)conn.sendall(str.encode(reply))except:breakprint("Lost connection")conn.close()whileTrue:conn,addr=s.accept()print("Connected to:",addr)start_new_thread(threaded_client,(conn,))