Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D0020E-projekt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Henrik Tjäder
D0020E-projekt
Commits
731a8cc7
Commit
731a8cc7
authored
10 years ago
by
Henrik Tjäder
Browse files
Options
Downloads
Patches
Plain Diff
Fixed the formatting according to PEP8
parent
c5c2689d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
UDP-client-server.py
+56
-41
56 additions, 41 deletions
UDP-client-server.py
with
56 additions
and
41 deletions
UDP-client-server.py
+
56
−
41
View file @
731a8cc7
...
...
@@ -12,10 +12,11 @@ NR_OF_PACKETS=10 #TOTAL NR. OF PACKETS TO SEND
PACKETS_PER_SEC
=
100
# PACKETS PER SECOND
# CLIENT - RECEIVER
UDP_RECEIVE_IP
=
'
::1
'
#IP ADDRESS TO LISTEN FOR INCOMMING PACKET
S (v4 or v6)
UDP_RECEIVE_PORT
=
55555
#IP PORT TO LISTEN FOR INCOM
M
ING PACKETS
UDP_RECEIVE_IP
=
'
::1
'
# LISTENING IP ADDRES
S (v4 or v6)
UDP_RECEIVE_PORT
=
55555
#
IP PORT TO LISTEN FOR INCOMING PACKETS
BUFFER
=
4096
#CLIENT-RECEIVER PART
def
udp_client_receive
(
UDP_RECEIVE_IP
,
UDP_RECEIVE_PORT
):
ADDR
=
(
UDP_RECEIVE_IP
,
UDP_RECEIVE_PORT
)
...
...
@@ -50,8 +51,14 @@ def udp_client_receive(UDP_RECEIVE_IP, UDP_RECEIVE_PORT):
packet_number
=
str
(
splitdata
[
1
].
strip
(
"'
'"
))
packet_number
=
packet_number
.
lstrip
(
'
0
'
)
#WRITE TO FILE AND DO PACKET COUNT
outfile
=
open
(
"
udp_twoway_results.csv
"
,
"
a
"
).
write
(
str
(
time
.
ctime
()
+
'
,
'
+
'
received ,
'
+
packet_number
+
'
,
'
+
str
(
rt_delay
)
+
'
\n
'
))
print
(
time
.
ctime
()
+
'
,
'
+
'
received ,
'
+
packet_number
+
'
,
'
+
str
(
rt_delay
))
#outfile = open("udp_twoway_results.csv", "a").\
open
(
"
udp_twoway_results.csv
"
,
"
a
"
).
\
write
(
str
(
time
.
ctime
()
+
'
,
'
+
'
received ,
'
+
packet_number
+
'
,
'
+
str
(
rt_delay
)
+
'
\n
'
))
print
(
time
.
ctime
()
+
'
,
'
+
'
received ,
'
+
packet_number
+
'
,
'
+
str
(
rt_delay
))
# Store minimum and maximum delay
if
rt_delay
>
max_delay
:
max_delay
=
rt_delay
...
...
@@ -62,8 +69,10 @@ def udp_client_receive(UDP_RECEIVE_IP, UDP_RECEIVE_PORT):
packet_count_rcvd
=
packet_count_rcvd
+
1
cumulative_delay
=
cumulative_delay
+
rt_delay
#CLIENT SERVER SIDE
def
udp_client_send
(
UDP_DEST_IP
,
UDP_DEST_PORT
,
PACKET_SIZE
,
NR_OF_PACKETS
,
PACKETS_PER_SEC
):
def
udp_client_send
(
UDP_DEST_IP
,
UDP_DEST_PORT
,
PACKET_SIZE
,
NR_OF_PACKETS
,
PACKETS_PER_SEC
):
inter_departure_time
=
1.
/
PACKETS_PER_SEC
packet_count_snd
=
0
...
...
@@ -79,10 +88,11 @@ def udp_client_send(UDP_DEST_IP, UDP_DEST_PORT, PACKET_SIZE, NR_OF_PACKETS, P
padding
=
''
for
j
in
range
(
98
,
PACKET_SIZE
):
padding
=
padding
+
str
(
1
)
for
i
in
range
(
1
,
NR_OF_PACKETS
+
1
):
#
SEND SPECIFIED N
UMBE
R OF P
ACKE
TS
for
i
in
range
(
1
,
NR_OF_PACKETS
+
1
):
#
SEND SPECIFIED NR OF P
K
TS
time
.
sleep
(
inter_departure_time
)
snd_sock6
=
socket
.
socket
(
socket
.
AF_INET6
,
socket
.
SOCK_DGRAM
)
snd_sock6
.
sendto
(
str
((
"
%.5f
"
%
time
.
time
(),
str
(
'
%08d
'
%
i
),
padding
)),
(
UDP_DEST_IP
,
UDP_DEST_PORT
)
)
snd_sock6
.
sendto
(
str
((
"
%.5f
"
%
time
.
time
(),
str
(
'
%08d
'
%
i
),
padding
)),
(
UDP_DEST_IP
,
UDP_DEST_PORT
))
packet_count_snd
=
packet_count_snd
+
1
#IF NOT IPv6
...
...
@@ -94,7 +104,8 @@ def udp_client_send(UDP_DEST_IP, UDP_DEST_PORT, PACKET_SIZE, NR_OF_PACKETS, P
for
i
in
range
(
1
,
NR_OF_PACKETS
+
1
):
time
.
sleep
(
inter_departure_time
)
snd_sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
snd_sock
.
sendto
(
str
((
"
%.5f
"
%
time
.
time
(),
str
(
'
%08d
'
%
i
),
padding
)),
(
UDP_DEST_IP
,
UDP_DEST_PORT
)
)
snd_sock
.
sendto
(
str
((
"
%.5f
"
%
time
.
time
(),
str
(
'
%08d
'
%
i
),
padding
)),
(
UDP_DEST_IP
,
UDP_DEST_PORT
))
packet_count_snd
=
packet_count_snd
+
1
#WAIT 5SEC FOR ALL PACKETS TO ARRIVE
...
...
@@ -114,8 +125,12 @@ def udp_client_send(UDP_DEST_IP, UDP_DEST_PORT, PACKET_SIZE, NR_OF_PACKETS, P
#START THE THREADS FOR SENDER AND RECEIVER
if
__name__
==
"
__main__
"
:
receiver_thread
=
Thread
(
target
=
udp_client_receive
,
args
=
(
UDP_RECEIVE_IP
,
UDP_RECEIVE_PORT
))
receiver_thread
=
Thread
(
target
=
udp_client_receive
,
args
=
(
UDP_RECEIVE_IP
,
UDP_RECEIVE_PORT
))
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
()
sender_thread
=
Thread
(
target
=
udp_client_send
,
args
=
(
UDP_DEST_IP
,
UDP_DEST_PORT
,
PACKET_SIZE
,
NR_OF_PACKETS
,
PACKETS_PER_SEC
)).
start
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment