Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cs363
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Courses
cs363
Commits
6ee1266b
Commit
6ee1266b
authored
Feb 14, 2019
by
Alan Marchiori
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added rcv buffer test
parent
30cab060
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
157 additions
and
2 deletions
+157
-2
README
rcv_buffer/README
+10
-0
randsender.py
rcv_buffer/randsender.py
+93
-0
zerorecv.py
rcv_buffer/zerorecv.py
+52
-0
udp_client.py
socket_examples/udp_client.py
+2
-2
No files found.
rcv_buffer/README
0 → 100644
View file @
6ee1266b
The goal of this is to create a connected
socket and NOT read data from it. The
server will just send random bits into
the socket to fill the receive buffer.
We can then observe the receive buffer
shrinking until it reaches zero. Then we
will see how the server side continues.
Alan Marchiori
2019
rcv_buffer/randsender.py
0 → 100644
View file @
6ee1266b
"""Ranodm data sender
This server accepts TCP connections and
send random data forever.
Alan Marchiori
2019
"""
import
logging
import
socket
import
time
import
threading
# to get thread name
import
argparse
import
os
def
handle_client
(
clientskt
,
clientaddr
,
log
,
bytelen
=
128
,
rate
=
10
):
"handle a single client"
totalbytes
=
0
log
.
info
(
"Connection from {}"
.
format
(
clientaddr
))
try
:
while
True
:
try
:
sentbytes
=
clientskt
.
send
(
os
.
urandom
(
bytelen
))
log
.
info
(
"Sending... Sent {} of {} bytes to {}, total bytes = {}"
.
format
(
sentbytes
,
bytelen
,
clientaddr
,
totalbytes
))
totalbytes
+=
sentbytes
time
.
sleep
(
1
/
rate
)
except
(
BrokenPipeError
,
ConnectionResetError
):
log
.
info
(
"Client {} closed connection"
.
format
(
clientaddr
))
clientskt
.
close
()
break
except
socket
.
error
as
x
:
log
.
info
(
"Got {}, done with client."
,
x
)
break
finally
:
try
:
clientskt
.
close
()
except
:
pass
def
main
(
addr
,
port
):
log
=
logging
.
getLogger
()
server_port
=
(
addr
,
port
)
log
.
info
(
"[{}] Starting threading server on {}"
.
format
(
threading
.
current_thread
()
.
name
,
server_port
))
threads
=
[]
try
:
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
skt
:
skt
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
skt
.
bind
(
server_port
)
# bind socket to the UDP port
skt
.
listen
()
while
True
:
clientskt
,
clientaddr
=
skt
.
accept
()
t
=
threading
.
Thread
(
target
=
handle_client
,
args
=
(
clientskt
,
clientaddr
,
log
))
t
.
start
()
threads
.
append
((
t
,
clientskt
))
except
KeyboardInterrupt
:
for
t
,
cskt
in
threads
:
if
t
.
is_alive
():
cskt
.
close
()
if
__name__
==
"__main__"
:
FORMAT
=
'
%(asctime)-15
s
%(levelname)-6
s:
%(message)
s'
logging
.
basicConfig
(
format
=
FORMAT
,
level
=
logging
.
DEBUG
)
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
parser
.
add_argument
(
'-a'
,
'--addr'
,
type
=
str
,
help
=
'ip address of the server'
,
required
=
False
,
default
=
"127.0.0.1"
)
parser
.
add_argument
(
'-p'
,
'--port'
,
type
=
int
,
help
=
'port the server listens on'
,
required
=
False
,
default
=
8888
)
args
=
parser
.
parse_args
()
main
(
**
vars
(
args
))
rcv_buffer/zerorecv.py
0 → 100644
View file @
6ee1266b
"""Ranodm data sender
This server accepts TCP connections and
send random data forever.
Alan Marchiori
2019
"""
import
logging
import
socket
import
time
import
argparse
import
os
def
main
(
addr
,
port
):
log
=
logging
.
getLogger
()
server_port
=
(
addr
,
port
)
log
.
info
(
"Starting client, connect to {}"
.
format
(
server_port
))
try
:
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
skt
:
skt
.
connect
(
server_port
)
log
.
info
(
"Connected to {}. Doing nothing!"
.
format
(
server_port
))
while
True
:
time
.
sleep
(
0.1
)
except
KeyboardInterrupt
:
log
.
info
(
"ctrl-c, quit!"
)
if
__name__
==
"__main__"
:
FORMAT
=
'
%(asctime)-15
s
%(levelname)-6
s:
%(message)
s'
logging
.
basicConfig
(
format
=
FORMAT
,
level
=
logging
.
DEBUG
)
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
parser
.
add_argument
(
'-a'
,
'--addr'
,
type
=
str
,
help
=
'ip address of the server'
,
required
=
False
,
default
=
"127.0.0.1"
)
parser
.
add_argument
(
'-p'
,
'--port'
,
type
=
int
,
help
=
'port the server listens on'
,
required
=
False
,
default
=
8888
)
args
=
parser
.
parse_args
()
main
(
**
vars
(
args
))
socket_examples/udp_client.py
View file @
6ee1266b
...
...
@@ -22,9 +22,9 @@ print ("message:", MESSAGE)
with
socket
.
socket
(
socket
.
AF_INET
,
# Internet
socket
.
SOCK_DGRAM
)
as
sock
:
# UDP
#
sock.bind((UDP_IP, 8889))
sock
.
bind
((
UDP_IP
,
8889
))
print
(
"Socket:"
,
sock
)
sock
.
sendto
(
MESSAGE
,
(
UDP_IP
,
UDP_PORT
))
#
print("Socket:", sock)
print
(
"Socket:"
,
sock
)
#sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
#print("Socket:", sock)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment