6d109dbb98de26990d9df2781ae31c14810f7adb
[tt2015.git] / a2 / code / client / main.py
1 from sender import Sender
2 import sys
3
4 # before running this program, be sure to run the TCPServer
5 # also make sure to run the below command to prevent the OS from canceling our connection
6 # sudo iptables -A OUTPUT -p tcp --tcp-flags PSH PSH -j ACCEPT
7 # sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP
8
9 # for running tests, you may need to store information on the received sequence number for the server
10 # you can do that by defining new fields within the sender class/global variables to store such info
11
12 # for non local communication, you can comment the raw sockets setting in sender.py (line 132)
13
14 # run using sudo (!!!) from a command line terminal
15 # and don't mind/ignore eclipse's red markings.
16
17 if __name__ == "__main__":
18 serverPort = 10000
19 if len(sys.argv) > 1:
20 serverPort = int(sys.argv[1])
21 sender = Sender(serverIP="127.0.0.1", networkInterface="lo", isLocal=True, serverPort=serverPort, waitTime=1, isVerbose=1)
22 # isLocal is True if the interface is a local one
23 response = sender.sendInput("S", 100, 100)
24
25 # triggers the response SA _ 101 if the server is listening on the specified port
26 # if the server isn't listening, there are no responses
27 print sender.lastAckReceived
28 print sender.isTimeout
29
30 # an example for the echo handling server
31 if sender.isTimeout == False: # in case something was received
32 sender.sendInput("A", 101, sender.lastSeqReceived + 1) # connection is established
33 sender.sendInput("A", 101, sender.lastSeqReceived + 1, data = "C") # send some data
34 sender.sendInput("A", 102, sender.lastSeqReceived + 1, data = "C") # send some data
35 sender.sendInput("FA", 103, sender.lastSeqReceived + 1) # close connection (the echo also closes)
36 sender.sendInput("RP", 104, 0) # reset connection
37
38 sender.sendReset() # switch sender port