network examples / reference?

Looking for a little help on networking. I see in the manual it says to ask here, so here I am. I already tried a search and basically just came up with a bunch of broken links - lots of references to “Network Example” (along with Rubiks example, and one other - all references made from one page), but when I try to go there, I get a 404.

I don’t think what I’m trying to do should be all that difficult. Just want a socket to listen for messages while still having an interactive window. Messages will be to change colors of objects - if I can get a string in, I think I can figure out what to do with it from there…

Any help appreciated!
–Blake

Try https://discourse.panda3d.org/viewtopic.php?t=940.

Just wondering if anyone had some different input on this… I’ve been going through the Panda API and cross-referencing the DocumentedServer.py - looks neat, but I’m not sure it’s quite what I was hoping for. From what I can tell, this is depending on Python at both ends (packaging up a datagram object, sending it over, then unpackaging it). I was hoping for something that could just use a Socket so I can use a different language to communicate with it. I guess I’m looking for a way to open a socket and create a task that will check for data.

Any takers?

Thanks!

If all you want is a low-level socket, try Python’s “socket” module.

http://www.python.org/doc/2.4.2/lib/module-socket.html

David

I understand you dont want to bother with the packing and unpacking of datagrams, but you could just send a string instead… Is that what you’re looking for?

Yes, I think that would work. I’m just having a hard time figuring out how to have a TCP socket listen and handle events while still allowing Panda to do it’s thing. I’m looking into the Twisted framework, but so far it seems that both Twisted and Panda have some sort of internal event loop that pretty much blocks anything else from happening.

Sorry - I’m kind of new to Python… this is definitely a learning experience for me, so if you can give me any pointers, I’d appreciate it!

This is based on the reference code here:

python.org/doc/lib/socket-example.html


#Let's start with a simple echo server embedded in code
def server():
        import socket
        HOST=''
        PORT=50007
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.bind((HOST,PORT))
        s.listen(1)
        conn, addr = s.accept()
        print 'connected by', addr
        while(1):
                data = conn.recv(1024)
                if not data: break
                conn.send(data)
        conn.close()

#Now, if you run this with panda, it will cause panda 
#to stop processing. You need to have this running all 
#the time, but you also need panda running all the time

#One of the ways to get around this is to use threading
import thread
thread.start_new_thread(server,())

#this basically says "call this function inside of a 
#thread, and let it run in parallel with my process"


#finally, here's the host code to test it with.
import socket
HOST='127.0.0.1'
PORT=50007

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))
s.send('Hello, world')
data=s.recv(1024)
s.close()
print 'Recieved', repr(data)

Panda and Twisted both have their own event loops that want to be in control of program flow, and need to be able to run independantly of each other. In the code I’m working on I was able to solve this problem by creating two program threads, one for Panda and one for Twisted, and using two Queue structures to pass messages between the two threads.

In Panda3D I have a task which runs once per frame, which checks the incoming Queue for messages from the server and updates world objects in Panda as needed. Player inputs call event handlers which place messages on the outgoing queue. In Twisted I have a loop which periodically checks the outgoing queue for messages, and sends them to the server as needed. Messages from the server are places on the incoming queue and sent to the Panda task as they happen.

This may not be the most optimal way to do it and still needs a lot of development, but it does solve the problem of running Twisted and Panda simultaneously.

Another, simpler option is not to call run() at all. Panda doesn’t really need to own the main loop.

Instead, simply call Task.step() from time to time, which will run through one iteration of Panda’s loop. In fact, run() is basically just an infinite loop that calls Task.step() repeatedly.

Of course, you have to ensure that Task.step() is called frequently enough to keep your frame rate up.

David

Hi :slight_smile: zpawlov, I tried your example above, but I get constantly the following error message:

 s.connect((HOST,PORT))
  File "<string>", line 1, in connect
socket.error: (111, 'Connection refused')

What goes wrong there? (Using Python 2.3.5)

He says that he can’t connect to the socket, that I see as well, but I wonder why he can’t connect… however, when running the server part alone, it opens the port and waits for a connection.

But when starting the client part, I get the message mentioned above :frowning:

Where is the error? At my place may be?

Regards, Bigfoot29

Small change to drwr’s post:


Task.step()

doesn’t work. It has to be


taskMgr.step()

Regards, Bigfoot29

That’s what I get for typing in code suggestions off the top of my head! :blush:

Hehe… the “cloaky” Mr Cloak gave me that advice in IRC, just wanted to provide the results here too :smiley:

He seem to know you, David wonders :wink:

Regards, Bigfoot29

im sure this is a really bad way to do it but…

def runEverySecond():
        taskMgr.step()    
w = World()
l = task.LoopingCall(runEverySecond)
l.start(0)
pykeClient = pykeFactory(w)
reactor.connectTCP("localhost", 8007, pykeClient)#(ipaddress,port,factory)
reactor.run() 

then in your factory…

class pykeFactory(ClientFactory):
    def __init__(self,wint):
        self.wor = wint

the client factory doesn’t have an init so don’t call it heh
so this way you can access stuff in your world object… like uh a queue:P

Edit: As for a server you could just write that all in twisted:)