Networking: Server Python Panda3D and Java Client

Hi
I´m developing a application with network connectivity.The server side, a graphic simulator, will be write with Panda3D. The client is a Java application with raw sockets that send string messages to server to control the behavior of simulator.
I will appreciate if anybody could show a simple code that implement a server in Panda3D that could communicate with the socket application write on another language like java via raw socket.
Thanks

A “server” is pretty nonspecific. There are many different kinds of servers, and there are many ways to write code that functions as a server. But if you’re only looking for inspiration, have you looked at the ServerRepository.py file in direct/distributed?

David

the server module should receive a string messagem form client via raw socket and use to interact with a 3d animation.

I have done this, is actually pretty complicated, the code was made to communicate with C with sockets, you add raw data, so I don’t know if this works with java, in this implementation message always have an unsigned int that is its code (for example, code 1 was login message).

Again, I don’t know if this code will serve you, but will give some information about what modules you should look at.
To send data you need to use self.sock.send().

Another important point is that this module uses the threading module which might not work properly with panda3d tasks (my server doesn’t use panda tasks - since they consume a lot of CPU even when doing nothing, - so it works fine), if you are using panda tasks you need to import the panda copy of threads, look at the manual for the exact name.

import socket, struct, ctypes, sys
import threading

class Message:
	def __init__(self, code):
		self.formatString = ""
		self.bufferSize = []
		self.valueList = []
		self.addUnsignedInt(code)
		
	def finishMessage(self):
		totalBufferSize = 0
		for i in self.bufferSize:
			totalBufferSize += i
		buff =  ctypes.create_string_buffer(totalBufferSize)
		offset = 0
		j = 0
		for i in self.valueList:
			if (self.formatString[j] == 's'):
				w = 0
				for k in i:
					struct.pack_into(self.formatString[j], buff, offset+w, k)
					w+=1
			else:
				struct.pack_into(self.formatString[j], buff, offset, i)
			offset += self.bufferSize[j]
			j+=1
		return buff
		
	def addUnsignedShortInt(self, value):
		self.formatString += "H"
		self.bufferSize.append(2)
		self.valueList.append(value)
		
	def addChar(self, value):
		self.formatString += "c"
		self.bufferSize.append(1)
		self.valueList.append(value)

	def addShortInt (self, value): 
		self.formatString += "h"
		self.bufferSize.append(2)
		self.valueList.append(value)

	def addInt(self, value):
		self.formatString += "i"
		self.bufferSize.append(4)
		self.valueList.append(value)
	
	def addUnsignedInt(self, value):
		self.formatString += "I"
		self.bufferSize.append(4)
		self.valueList.append(value)
	
	def addLongInt(self, value):
		self.formatString += "l"
		self.bufferSize.append(4)
		self.valueList.append(value)
	
	def addUnsignedLongInt(self, value):
		self.formatString += "L"
		self.bufferSize.append(4)
		self.valueList.append(value)
	
	def addLongLongInt(self, value):
		self.formatString += "q"
		self.bufferSize.append(8)
		self.valueList.append(value)
	
	def addUnsignedLongLongInt(self, value):
		self.formatString += "Q"
		self.bufferSize.append(8)
		self.valueList.append(value)
	
	def addFloat(self, value):
		self.formatString += "f"
		self.bufferSize.append(4)
		self.valueList.append(value)
	
	def addDouble(self, value):
		self.formatString += "d"
		self.bufferSize.append(8)
		self.valueList.append(value)
	
	def addString(self, value):
		self.formatString += "I"
		self.bufferSize.append(4)
		self.valueList.append(len(value)+1)
		self.formatString += "s"
		self.bufferSize.append(len(value)+1)
		self.valueList.append(value)
class ConnectionHandler:
    def __init__(self, host, port):
        self.sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
        try:
            self.sock.connect ( ( host, port) )
        except:
            print("Can't connect to server.")
        self.bufferSize = 256
        self.buffer = ctypes.create_string_buffer(self.bufferSize)
        self.receiveList = []
        self.receiveThread = threading.Thread(target=self.receiveData)
        self.receiveThread.setDaemon(True)
        self.receiveThread.start()
    
    def receiveData(self):
        while True:
            try:
                totalSize = self.sock.recv_into(self.buffer, self.bufferSize)
                if (totalSize > 0):
                    self.receiveList.append((totalSize, self.buffer))
            except socket.error:
                    print ("Error receiving data.")
                    pass

[/quote]