Commit 6ee1266b authored by Alan Marchiori's avatar Alan Marchiori

added rcv buffer test

parent 30cab060
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
"""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)-15s %(levelname)-6s: %(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))
"""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)-15s %(levelname)-6s: %(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))
......@@ -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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment