include tcp.repy

MSS = 2

if callfunc == 'initialize':
  IP = '127.0.0.1' #  getmyip()
  PORT = 12345
  PORT_TWO = 12346
  MESSAGE = "hi hows it going foo bar baz good bye??"
  MAXLEN = 1000000 # all of it

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

  retries = 100
  mess = ""
  while retries:
    mess += tcup.recv(MAXLEN)
    if mess == MESSAGE:
      break
    else:
      retries -= 1
      sleep(.01)

  if mess != MESSAGE:
    print "%s != " % mess
    print "%s" % MESSAGE

  tcup.disconnect()

def client():
  socket = Connection()
  socket.bind(IP, PORT)
  socket.connect(IP, PORT_TWO)
  
  bytes = socket.send(MESSAGE)
  if bytes == 0:
    print "Expected some bytes"

  socket.disconnect()

def main():
  settimer(0, server, ())
  sleep(.01)
  client()

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