I want to install a program onto 4 computers that just listens for a command by 1 main server.
How would I go about doing that?
Should I install 4 servers on each PC and my main client program speaks to all of them?
Or
Should I create a client where its just listening for calls from the server (seems more logical). If so, how would I go about doing this?
What I have so far:
client.py
Server.py
How would I go about doing that?
Should I install 4 servers on each PC and my main client program speaks to all of them?
Or
Should I create a client where its just listening for calls from the server (seems more logical). If so, how would I go about doing this?
What I have so far:
client.py
Code
# a simple client socket
import socket
# define socket address
TCP_IP = '127.0.0.1' # ip of the server we want to connect to
TCP_PORT = 5000 # port used for communicating with the server
BUFFER_SIZE = 1024 # buffer size used when receiving data
# create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket created successfully.")
# connect to server
s.connect((TCP_IP, TCP_PORT))
print ("Established connection with the server.")
message = b"I've been sent from the client!"
# send message to the server
s.send(message)
print ("Message sent to server.")
# receive data from server
data = s.recv(BUFFER_SIZE)
print ("Here's the message received from the server:")
print (data)Server.py
Code
# a simple server socket
import socket
# define socket address
TCP_IP = '0.0.0.0' # consider all possible incoming IPs
TCP_PORT = 5000 # port used for communicating with the client
BUFFER_SIZE = 1024 # buffer size used when receiving data
# create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket created successfully.")
# bind socket
s.bind((TCP_IP, TCP_PORT))
# start listening
s.listen(20)
print ("Waiting for a connection...")
# wait for connection
while True:
# accept connection
conn, addr = s.accept()
print ("Connected with " + addr[0] + " " + str(addr[1]))
# get message from client
message = conn.recv(BUFFER_SIZE)
# check that there is a message
if not message:
break
# put some logic here, if needed
server_message = b"I'm the message from the server..."
#server_message = input("Enter message")
#server_message = b(server_message)
# send message to client
conn.send(server_message)
s.close()

