I have a setup of two laptops next to each other and would like to send messages between them, I found code that works when both the client and the server is on the same computer, but it won't work when they are separated.
#SERVER
import socket
def Main():
host = "localhost"
port = 5000
mySocket = socket.socket()
mySocket.bind((host,port))
mySocket.listen(1)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
while True:
data = conn.recv(1024).decode()
if not data:
break
print ("from connected user: " + str(data))
data = str(data).upper()
print ("sending: " + str(data))
conn.send(data.encode())
conn.close()
if __name__ == '__main__':
Main()
#CLIENT
import socket
def Main():
host = '0.0.0.0'#127.0.0.1
port = 5000
mySocket = socket.socket()
mySocket.connect((host,port))
message = input(" -> ")
while message != 'q':
mySocket.send(message.encode())
data = mySocket.recv(1024).decode()
print ('Received from server: ' + data)
message = input(" -> ")
mySocket.close()
if __name__ == '__main__':
Main()
I have tried many different addresses, including, 0.0.0.0, localhost, 192.168... <- ip from ifconfig. What is the problem? and what should I look into for the best solution?