-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcp_receive_blocking.py
67 lines (52 loc) · 2.36 KB
/
tcp_receive_blocking.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import socket
# This is your buffer size.
# You can't receive anything more than the buffer size at one time.
BUFFER_SIZE = 65535
# This should be the IP address of the computer you run this
# code on (the server). It should be the SAME IP address that
# the client hooks up to.
# Note: If you use '127.0.0.1' you can only receive connections
# from the same computer. Outside computers cannot connect to a
# computer listening to 127.0.0.1.
my_ip_address = '127.0.0.1'
my_ip_port = 10000
# Create a socket for sending/receiving data
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Tell the socket it will be listening on my_ip_address, and my_port.
# Note that Python expects ip and port as a list
listen_to = (my_ip_address, my_ip_port)
my_socket.bind(listen_to)
# We are going to be listening as a server, not connecting as a client.
# So we call the 'listen' method of our socket.
# The "1" specifies the size of the backlog of connections we allow before
# refusing connections. If we specify 1, then once we pick up a connection
# we won't accept any others until we close the current connection.
my_socket.listen(1)
while True:
connection = None
try:
# Get a connection, and the address that hooked up to us.
# The 'client address' is an array that has the IP and the port.
connection, client_address = my_socket.accept()
# Read in the data, up to the number of characters in BUFFER_SIZE
data = connection.recv(BUFFER_SIZE)
# Print what we read in, and from where
client_ip = client_address[0]
client_port = client_address[1]
# Decode the byte string to a normal string
data_string = data.decode("UTF-8")
print(f"Data from {client_ip}:{client_port} --> '{data_string}'")
except Exception as e:
# Whoa Nelly! Was there an error?
print("Unable to receive the message.", e)
finally:
# Close the socket. No socket operations can happen after this.
# If there was more data to send, you would not want to do this.
# You would want a loop of 'recv' and keep the 'accept' and 'close'
# out of that loop.
if connection:
connection.close()
# Because we have a 'while True' loop we'll never get here. But this
# is the proper code to run when you want to shut down the socket properly.
socket.close()
print("Done")