Python <-> Java TCP Sockets
03-01-05
server.py
Client.java
These 2 files, server.py , and
Client.java demonstrate how to
communicate between java and python over sockets. The Java
client has a receive thread that just waits for something
to show up on the socket. The Python server has a seperate
thread to process receives from all clients that connect to it.
To demonstrate this, I have created a basic protocol to share COVE information.
The messages implemented are SendSpeed, SendBearing, SendTurningRate, and
SendUpdate. The format for the protocol is just a simple string. !! is the
start of a new message and ##\n is the end. All arguments in the message
string are identified by their argument name, followed by '=', followed by
the value, followed by ','.
SERVER TO CLIENT
-
SendUpdate:
type, count (number of entities in this update), [bearing, speed, xcoord, ycoord, isalive, id] repeat this portion count times.
An example of a message for 2 entities:
!!type=update,count=2,bearing=23.5,speed=53.3,xcoord=43,ycoord=32,isalive=1,id=francis,bearing=54.2,speed=98.3,xcoord=23,ycoord=876,isalive=1,id=fletch##\n
CLIENT TO SERVER
-
SendSpeed:
type, speed
Example:
!!type=speedupdate,speed=53.3##\n
-
SendBearing:
type, bearing
Example:
!!type=bearingupdate,bearing=33.3##\n
-
SendTurningRate:
type, turningRate
Example:
!!type=turningrateupdate,turningrate=38.1##\n