123456789101112131415161718192021222324252627282930313233343536373839 |
- import socket
- import sys
- # save values needed to talk to server
- saddr = sys.argv[1]
- uport = int(sys.argv[2])
- rcode = sys.argv[3]
- msg = sys.argv[4]
- # start udp socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- # send msg to server to check rcode
- sock.sendto(rcode.encode(),(saddr, uport))
- # get reply from server, save the port, then check it
- confirmation, newaddr = sock.recvfrom(1024)
- rport = confirmation.decode()
- sock.sendto(rport.encode(), newaddr)
- confirm, neweraddr = sock.recvfrom(1024)
- if (confirm.decode() == "correct"):
- # got right port, close UDP connection
- sock.close
-
- # make TCP connection, send msg
- tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- tsock.connect((saddr, int(rport)))
- tsock.send(msg.encode())
-
- # get reply, print it, exit
- reply = tsock.recv(1024).decode()
- print("CLIENT_RCV_MSG='", reply, "'", sep="")
- tsock.close()
- sys.exit(0)
- else:
- print("Did not confirm the port, giving up.")
- sys.exit(1)
|