-
Notifications
You must be signed in to change notification settings - Fork 2
/
tcp_send_multiple_messages_open_close.py
50 lines (37 loc) · 1.51 KB
/
tcp_send_multiple_messages_open_close.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import socket
# IP address and port of where we'll send the message.
server_ip_address = '127.0.0.1'
server_ip_port = 10000
def send_data(total_bytes, message_size_in_bytes):
"""
Send a bunch of messages that sum "total_bytes" of data. Break each
message into chunks of "message_size_in_bytes". (Try to anyway.)
"""
# Total number of messages to send.
messages_to_send = total_bytes // message_size_in_bytes
# Message as a byte array. (Hence the b at the front.)
# Put a Y at the end of each chunk, a \n at end-of-message.
my_message = b"X" * (message_size_in_bytes - 1) + b"Y"
# Repeat, and send our message over and over.
for i in range(messages_to_send):
# Open a socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
# Connect to this server, and this port.
my_socket.connect((server_ip_address, server_ip_port))
my_socket.sendall(my_message)
if(i == messages_to_send - 1):
# Send a message signaling we are done sending packets.
my_socket.sendall(b"\n")
# Close the socket
my_socket.close()
def main():
""" Main program. """
# How many bytes to send
total_bytes = 5000
# How big each message will be
message_size_in_bytes = 100
print(f"Sending {total_bytes:,} bytes in {message_size_in_bytes} byte chunks.")
send_data(total_bytes, message_size_in_bytes)
print("Done")
main()