I used UDP instead of TCP to solve my problem. Here is how I did it in python: There were some requirements about how the nodes in the ring are organized and when the token is passed and how the token number is changed and when all the processes should terminate. I just added the code to show the logic of the UDP that I used.
address = socket.gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
has_token = False
token = 1
if id == 1:
has_token = True
token = random.randrange(1,11,1)
time.sleep( 5 )
s.bind((address, port))
while token < max_token:
while not has_token:
data_token, addr = s.recvfrom(8192)
if data_token:
has_token = True
token = int(data_token)
if (token % 7) != id:
token = increment_token(token)
s.sendto(str(token),(next_address,next_port))
has_token = False
else:
token = set_random_token(token)
s.sendto(str(token),(next_address,next_port))
has_token = False