Send
import socket
import time
HOST = '' # Space
PORT = 7778 # Port number (non-privileged ports are > 1023),Same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
time.sleep(0.1) # Sleep for 0.1 seconds
conn.sendall(b'1') # Send the number '1'
Receive
import socket
import datetime
HOST = '192.168.0.111' # Send IP address
PORT = 7788 # Same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
data = s.recv(1024) # Receive data
if data:
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = f"{now} - Received: {data.decode()}\n"
print(log_message, end='') # Print the received message
with open(r'D:\log.txt', 'a') as file:
file.write(log_message) # Write to log file