#!/usr/bin/python
import socket
import sys
#Try to get the nickname from the execution arguments
try:
nick = sys.argv[1]
#If it is not specified, make it "Guest"
except IndexError:
nick = "Guest"
#Try to get the port from the execution arguments
try:
port = int(sys.argv[2])
#If it is not specified make it 9999
except IndexError:
port = 9999
#Create a socket and connect it with (IP, port)
#In this case "the IP" is localhost
s = socket.socket()
s.connect(("localhost", port))
print "\\t\\t.::Client-Server Mergesort::."
print "\\t\\t1- Please use the syntax: X-X-X-X-X"
print "\\t\\t(Where the X\'s are the numbers to order)"
print "\\t\\t2- Type exit to finish the program"
#Main loop
while True:
#Get the list as an input and send it to the server
list_str = raw_input("%s -> List to Sort: "%nick)
s.send(list_str)
#If the string is exit, finish the client program
if list_str == "exit":
break
#Wait until the server sorts the list
sorted_list = s.recv(1024)
#If the result of the sorted list is false
#the input had invalid characters so show a message
if sorted_list == "False":
print "Invalid Input. Try Again"
#If the input was correct, print the results
else:
print "\\tSorted List: %s"%(sorted_list)
print "Exit"
#Close the socket
s.close()