Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Source Link

python socket:send and receive multi objects data

i working on a multi player game project its strategy so i need to sending data for more that one object in the same time i created a client and server and that's worked for one object in the server and one object in the client but after i added the second object to the server i get a proplem not error just 3d objects position proplem and the controller for server objects get swapped for example the server have two objects that's i can get access to control it ["Cube"] and ["Server"] and the server have only one object that's called ["Client"] the proplem is when i run server then i controll my objects the engine swaping the objects in the client screen for example whe i click up arrow to move the ["Server"] object to up the ["Cube"] object who moves in the Client screen and when i click w key to move ["Cube"] object to up the ["Server"] object who moves in the client screen

that's my client code the ["client"] object are connected to it in the game engine

#------------Setup--------------#
from pickle import dumps, loads
from mathutils import Vector
from GameLogic import * 
import GameLogic as GameLogic
import bge
from socket import *
import socket
import pickle


cont = GameLogic.getCurrentController()
owner = cont.owner
#obj = cont.owner

if not owner['OneTime']:
  ServerIP = GameLogic.IP
  Serverport = 2052
  Clientname = ''
  ClientPort = 2053
  GameLogic.sClient = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

  GameLogic.sClient.bind((Clientname,ClientPort))
  GameLogic.host = (ServerIP,Serverport)    
  GameLogic.sClient.setblocking(0)
  owner['OneTime'] = 1

PosYou = list(owner.worldPosition)

scene = getCurrentScene()
Cube = scene.objects["Cube"]
Server = scene.objects["Server"]


PosServer = [0,0,0]
PosCube = [0,0,0]
#------------RECEIVE/SEND--------------#    

Data = pickle.dumps((PosYou))

GameLogic.sClient.sendto(Data,GameLogic.host)

try:
    #thefucking proplem
    Data, SRIP = GameLogic.sClient.recvfrom(1024)



    UPData = pickle.loads(Data)
    PosServer = [UPData[0],UPData[1],UPData[2]]
    Server.worldPosition = PosServer 
    Data2, SRIP2 = GameLogic.sClient.recvfrom(1024)
    UPData2 = pickle.loads(Data2)

    PosCube = [UPData2[0],UPData2[1],UPData2[2]]
    Cube.worldPosition = PosCube 




except OSError:
    pass            
#---------------THE-END----------------#

that's my server code the ["Server"] object are connected to it in the game engine

#------------Setup--------------#
from GameLogic import * 
import GameLogic as GameLogic
import bge
from socket import *
import socket
import pickle

from pickle import *
cont = GameLogic.getCurrentController()
owner = cont.owner
#obj = cont.owner

if not owner['OneTime']:
  Host = "localhost"
  ServerPort = 2052
  GameLogic.sServer = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  GameLogic.sServer.bind((Host,ServerPort)) 
  GameLogic.sServer.setblocking(0)
  owner['OneTime'] = True

PosYou = list(owner.worldPosition)

scene = getCurrentScene()
Client = scene.objects["Client"]
Cube = scene.objects["Cube"]
PosClient = [0,0,0]
PosCube = [0,0,0]
#------------RECEIVE/SEND--------------#

try:
    Data, CLIP = GameLogic.sServer.recvfrom(1024)
    UPData = pickle.loads(Data)
    PosClient = [UPData[0],UPData[1],UPData[2]]
    Client.worldPosition = PosClient 
    Data = pickle.dumps((PosYou))
    GameLogic.sServer.sendto(Data,CLIP)  


except OSError:
    pass
#---------------THE-END----------------#

the second object code the ["Cube"] object and that's connected two the object in the game engine and i get this error when i run it

File "secondob.py", line 18, in AttributeError: 'module' object has no attribute 'sServer'
#------------Setup--------------#
from GameLogic import * 
import GameLogic as GameLogic
import bge
from socket import *
import socket
import pickle

from pickle import *
cont2 = GameLogic.getCurrentController()
owner2 = cont2.owner
#obj = cont.owner





GameLogic.sServer.setblocking(0)
PosYou2 = list(owner2.worldPosition)


scene = getCurrentScene()
Client = scene.objects["Client"]
PosClient2 = [0,0,0]
#------------RECEIVE/SEND--------------#

try:

    Data2, CLIP2 = GameLogic.sServer.recvfrom(1024)


    UPData2 = pickle.loads(Data2)
    PosClient2 = [UPData2[0],UPData2[1],UPData2[2]]

    Client.worldPosition = PosClient2     
    Data2 = pickle.dumps((PosYou2))
    GameLogic.sServer.sendto(Data2,CLIP2)  
except OSError:
    pass
#---------------THE-END----------------#