# you can put the server code in a computer and the client code on a another computer
# !important: put the server code in another file than the client file
# server code
import socket
SERVER = socket.gethostbyname(socket.gethostname()) # get the ip address of you'r computer
PORT = 5050
ADDR = (SERVER, PORT)
create_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a socket for connection
create_socket.bind(ADDR) # bind the socket
create_socket.listen(1) # listen to one client
connection, addr = create_socket.accept() # accept any connection to this server
print(f"Connection form {addr}")
while True:
msg = input("SERVER: ").encode() # take an input from the server and decode it
connection.send(msg) # send the server message to the client
print("Wait for response...")
recv_message = connection.recv(2048).decode() # recv a message from the client
print(f"CLIENT: {recv_message}")
# client code
import socket
SERVER = socket.gethostbyname(socket.gethostname()) # get the ip address of you'r computer
PORT = 5050
ADDR = (SERVER, PORT)
client_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a socket for connection
client_connection.connect(ADDR) # connect the socket to the server
while True:
print("Wait for response...")
recv_message = client_connection.recv(2048).decode() # recv a message from the server
print(f"SERVER: {recv_message}")
msg = input("CLIENT: ").encode() # take an input from the client and decode it
client_connection.send(msg) # send the client message to the server