Skip to content

Instantly share code, notes, and snippets.

@xslim
Created March 16, 2011 22:05
Show Gist options
  • Save xslim/873415 to your computer and use it in GitHub Desktop.
Save xslim/873415 to your computer and use it in GitHub Desktop.
qCar Server test
#!/usr/bin/env python
import socket
import sys
import random
import struct
import time
# Server example
# Establish a TCP/IP socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# create the UDP socket
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Bind to TCP port No. 17642 ...
s.bind(("",8787))
# ... and listen for anyone to contact you
# queueing up to five requests if you get a backlog
s.listen(5)
packet = lambda *args: struct.pack('!HQffffffff', 0xbaba, *args)
seq = 1
# Servers are "infinite" loops handling requests
while True:
# Wait for a connection
connect, address = s.accept()
# Typically fork at this point
# Receive up to 1024 bytes
#resp = (connect.recv(1024)).strip()
# And if the user has sent a "SHUTDOWN"
# instruction, do so (ouch! just a demo)
#if resp == "SHUTDOWN": break
print "\ndone",address
# Send an answer
connect.send("\x0AOK\n")
# And there could be a lot more here!
while True:
# And loop for / wait for another client
val_egt = random.randint(0,1000)
val_v2 = random.randint(1, 255)
val_boost = random.uniform(-1,2)
val_v4 = random.randint(1, 255)
val_v5 = random.randint(1, 255)
val_v6 = random.randint(1, 255)
val_v7 = random.randint(1, 255)
val_v8 = random.randint(1, 255)
#print "%f" % (val_boost)
#sys.stdout.write(val_boost)
p = packet(seq, val_egt,val_v2,val_boost,val_v4,val_v5,val_v6,val_v7,val_v8)
#p = packet(seq, *[random.randint(1, 255) for i in xrange(4)])
#sys.stdout.write(p)
#sys.stdout.flush()
time.sleep(.01)
seq += 1
UDPSock.sendto(p,(address[0], 8787))
# When done with a connection close it
connect.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment