-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop-wait-protocol.py
39 lines (29 loc) · 956 Bytes
/
stop-wait-protocol.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
import socket
# Define the IP address and the port number that the socket will listen on
HOST = 'localhost'
PORT = 12345
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the IP address and the port number
server_socket.bind((HOST, PORT))
# Listen for incoming connections
server_socket.listen()
# Accept a connection
print('Waiting for a connection...')
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address}")
# Define the data to be sent
data = "Hey Selcia"
# Send the data
client_socket.send(data.encode())
# Receive the acknowledgement
ack = client_socket.recv(1024)
# Print the acknowledgement
if not ack:
print("Error: Empty acknowledgement message received")
else:
# Print the acknowledgement
print(f"Acknowledgement received: {ack.decode()}")
# Close the connection
client_socket.close()
server_socket.close()