Simple Server

While were working with networking in panda3d I came up to this example of trivial server that prints received data.

I needed to connect other programs with panda without datagrams.

You can connect to this server through telnet, for example, and send some data by typing it in.

telnet localhost 1234

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
from direct.task import Task

PORT = 1234


class MyApp(ShowBase):

    def __init__(self):

        self.lastConnection = None
        self.cManager = QueuedConnectionManager()
        self.cListener = QueuedConnectionListener(self.cManager, 0)
        self.cReader = QueuedConnectionReader(self.cManager, 0)
        # set raw mode - no datagrams requierd
        self.cWriter = ConnectionWriter(self.cManager,0)
        self.tcpSocket = self.cManager.openTCPServerRendezvous(PORT, 1)
        taskMgr.add(self.listenTask, "serverListenTask",-40)
        taskMgr.add(self.readTask, "serverReadTask", -39)
    def listenTask(self, task):
        if self.cListener.newConnectionAvailable():
            rendezvous = PointerToConnection()
            netAddress = NetAddress()
            newConnection = PointerToConnection()

            if self.cListener.getNewConnection(rendezvous,netAddress,newConnection):
                newConnection = newConnection.p()
                # tell the Reader that there's a new connection to read from
                CLIENTS[newConnection] = netAddress.getIpString()
                self.lastConnection = newConnection
                print "Got a connection!"
                print "getNewConnection returned false"
        return Task.cont

    def readTask(self, task):
        if self.cReader.dataAvailable():
            datagram = NetDatagram()
            if self.cReader.getData(datagram):
                # print received message
                msg = datagram.getMessage()
                print msg
        return Task.cont

app = MyApp()

nice snip - thankyou for share it