# sample tcp server in python that uses binary comm - match with clientskl.py from socket import * from struct import * # for packing/unpacking import sys listenport = int(sys.argv[1]) # bind port # create server socket, or "listening" socket: sfd = socket(AF_INET,SOCK_STREAM) # bind socket to host and port sfd.bind(('0.0.0.0', listenport)) sfd.listen(16) # set length of connection queue # server loop: connects = 0 # simple counter keeps track of while 1: # open communications socket (cfd,addr) = sfd.accept() print "connection from ip",addr[0],"port ",addr[1] x = 10 # number of bytes to send y = htonl(x) # convert to network byte ordering s = pack("I",y) # pack into string format cfd.send(s) # send over socket buf = "abcdefghij" cfd.send(buf) # send buffer (10 bytes) r = cfd.recv(8) # read 8 bytes from socket print "received echo: ",r cfd.close() # close socket, get ready for next connection # server loop