Server that sends messages

Hi,

I’ve got a python server that can receive messages and send pre-coded messages from/to the client, however; is it possible to write messages to the client while the server is running??

Thanks,

Lewis

Yes, it’s the same thing in reverse, after all.

You’ll need to be more specific in what you’d like to achieve, and how that differs from what you’re currently doing, if you want more useful answers :slight_smile:

Well after you said that the problem was with my server, I’ve written another one/worked on the one you gave me so that it accepts multiple clients, essentially I want to pass a movement command from a client to blender in order to move my character

I still don’t understand why that is a problem - you already send data from the server to the client and back.

Do you know how to get the client to receive messages and print them after it has sent the position to the server?

I keep getting the error:


import socketimport time
import bpy
from math import pi
from bge import logic, render
from commands import Commands
from clientThread import clientThread 






class NetworkPeer:




    def __init__(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = socket.gethostname()
        self.port = 12345
        
        self.socket.connect((self.host, self.port))
        self.set_id()
       
    def set_id(self):
        msg = Commands.SETID + ' SimR '
        print ('C: sending message:', (msg ))#
        
        sock = self.socket
        sock.sendall(msg.encode('utf-8'))
        #identitylen = len( Commands.ACK )
        #data = Commands.ACK
        
        try:
            while True:
                char = sock.recv( 1 )
                if char == '
':
                    break
 
                else:
                    data+= ch




        except:
            pass
#
#
#        
        data, addr = sock.recvfrom(1024)
        print("Message recieved: ", data)
        
    
        
        data, addr = sock.recvfrom(1024)
        print("Message recieved: ", data)
       
def init(cont):
   
    own = cont.owner


    logic.network = NetworkPeer()
    render.showMouse(1)
    #print('logic.network.socket')




def update(cont):
    
    cont = bge.logic.getCurrentController
    own = cont.owner
    x, y = own.worldPosition.xy
    ori = own.worldOrientation.to_euler()[2] * (180/pi)
    if ori < 0:
        ori = 360 + ori
        own['Heading'] = ori
    message = Commands.POSE + " SIMR {} {} {}
".format(int(x), int(y), int(ori))
    print ('C: sending message: ', ( message ))
    logic.network.socket.send(message.encode('utf-8'))
    print(logic.network.socket)
    data, addr = sock.recvfrom(1024)
    print("Message recieved: ", data)

Read the error and your code… You use a variable called sock that you never define

I know that but when I try and define it as sock=socket.self I get the error that there’s no attribute ‘socket’

You are trying to access variables that belong to the class, but accessing them from outside of it. Rather than do that, write functions for the class to do what you want, e.g. sending/recieving messages. Then, call the functions to get what you want.

Why would you want to define it as socket.self?

The variable must exist in order to be used. socket.self doesn’t exist, and if you look above that line, a socket is used to send data - logic.network.socket

The point of this reply is to suggest that you reason through error messages and notice patterns in your code :slight_smile:

Whilst this is true, in Python it’s not poor design to access member variables of objects, provided that they’re not indicated to be private (_foo). It wouldn’t really make sense in this case to rewrite the entire socket API when it’s already provided by the socket object, unless more advanced features are added :slight_smile:

While I agree that it’s useful to prompt people to read the errors, in this situation I believe the problem is the understanding how a class works.

The problem with bandaid fixing the individual errors as they come up is it leaves lewis without a good understanding on what’s actually happening. Instructions how to reference the internal socket variable is only going to end up with lots of repeat code all over the place and makes it only a matter of time before we see a thread asking why some objects can recieve the right messages and others don’t.

Lewis, I’d suggest taking a step back and learn how to use classes properly, then use one to consolodate all of the network code.