diff --git a/UDP-client-server.py b/UDP-client-server.py index 1fa06c33d1bffde873d33969ced20f1f44fefeec..2a71c8676f3e6b99acb69dc6219f11d34f804478 100755 --- a/UDP-client-server.py +++ b/UDP-client-server.py @@ -1,7 +1,8 @@ import socket import time import sys -from threading import Thread +import threading +from threading import Thread, Timer import numpy as np from array import * @@ -66,8 +67,10 @@ def udp_client_receive(UDP_RECEIVE_IP, UDP_RECEIVE_PORT): rt_delay_array.append(rt_delay) - print((time.ctime() + ',' + 'received , ' + - packet_number + ' , ' + str(rt_delay))) +# Comment this line to disable per packet print + + #print((time.ctime() + ',' + 'received , ' + + #packet_number + ' , ' + str(rt_delay))) # Store minimum and maximum delay if rt_delay > max_delay: @@ -127,7 +130,8 @@ def udp_client_send(UDP_DEST_IP, UDP_DEST_PORT, PLR = 100 - ((packet_count_rcvd * 100.) / packet_count_snd) - print('\n', packet_count_snd, 'packets sent') + print('\n') + print(packet_count_snd, 'packets sent') print(packet_count_rcvd, 'packets received') print('packet loss ratio = ', round(PLR, 3), '%') @@ -147,8 +151,10 @@ def udp_client_send(UDP_DEST_IP, UDP_DEST_PORT, else: avg_packet_delay = cumulative_delay / packet_count_rcvd #CALCULATE STABILITY - network_stability = int(((packet_count_rcvd/packet_count_snd)*(avg_packet_delay/packet_wait_time)*100)+0.5) + network_stability = int(((packet_count_rcvd / packet_count_snd)\ + * (avg_packet_delay / packet_wait_time) * 100) + 0.5) print('avg.rtt = ', avg_packet_delay) + print('stability = ', network_stability) #START THE THREADS FOR SENDER AND RECEIVER @@ -158,7 +164,17 @@ if __name__ == "__main__": receiver_thread.daemon = True receiver_thread.start() time.sleep(1) - sender_thread = Thread(target=udp_client_send, - args=(UDP_DEST_IP, UDP_DEST_PORT, - PACKET_SIZE, NR_OF_PACKETS, - PACKETS_PER_SEC)).start() + while threading.activeCount() <= 2: + + # Set the count of the received packets to 0, + # since the listener thread runs continously + packet_count_rcvd = 0 + packet_count_snd = 0 + + sender_thread = Thread(target=udp_client_send, + args=(UDP_DEST_IP, UDP_DEST_PORT, + PACKET_SIZE, NR_OF_PACKETS, + PACKETS_PER_SEC)) + sender_thread.start() + while sender_thread.is_alive(): + time.sleep(1)