Skip to content

Instantly share code, notes, and snippets.

@bave
Created August 19, 2013 14:41
Show Gist options
  • Save bave/6269883 to your computer and use it in GitHub Desktop.
Save bave/6269883 to your computer and use it in GitHub Desktop.
A test program of TCP Fast Open. (supported by greater than or equal to linux 3.7 kernel )
#!/usr/bin/env python
# you must preset following command.
# sudo sysctl -w net.ipv4.tcp_fastopen=3
import sys
import socket
PORT = 10443
TCP_FASTOPEN = 23
MSG_FASTOPEN = 0x20000000
def server():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_TCP, TCP_FASTOPEN, 5)
s.bind(("", PORT))
s.listen(socket.SOMAXCONN)
while True:
conn, addr = s.accept()
while True:
ret = conn.recv(1024)
if len(ret) <= 0:
break
print ret;
conn.sendall(ret)
conn.close()
s.close()
def client():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.sendto("1. Using TCP fast open!!!", MSG_FASTOPEN, (sys.argv[2], PORT))
ret = s.recv(1024)
print ret
s.send("2. continued data sending")
ret = s.recv(1024)
print ret
s.close()
def usage():
print 'Usage:'
print ' python tfo.py [option]'
print ''
print ' -s'
print ' server mode'
print ''
print ' -c [servername]'
print ' clinet mode'
print ''
def main():
if '-s' in sys.argv:
server()
elif '-c' in sys.argv:
client()
else:
usage()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment