include tcp.repy

if callfunc == 'initialize':
  IP = '127.0.0.1' #  getmyip()
  PORT = 12345
  PORT_TWO = 12346
  fn = "seattle.txt"
  fnout = "junk_test.out" # write if error
  fobj = open(fn, "r")
  MESSAGE = fobj.read()
  fobj.close()
  MAXLEN = 1000000 # all of it

def server():
  socket = Connection()
  socket.bind(IP, PORT_TWO)
  socket.listen()
  tcup, addr = socket.accept()

  sleep(10)
  mycontext['mess'] += tcup.recv(MAXLEN)

  tcup.disconnect()
  if mycontext['mess'] != MESSAGE:
    print "%s != " % mycontext['mess']
    print MESSAGE
    fobj = open(fnout, "w")
    fobj.write(mycontext['mess'])
    fobj.close()

def client():
  socket = Connection()
  socket.timeout = 5 # seconds
  socket.bind(IP, PORT)
  socket.connect(IP, PORT_TWO)

  bytes = socket.send(MESSAGE)
  if bytes == 0:
    print "Expected some bytes"

  socket.disconnect()

def main():
  mycontext['mess'] = ""
  settimer(0, server, ())
  sleep(.01)
  client()

if callfunc == 'initialize':
  main()
  exitall()
