3

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?

2 Answers 2

2

You need to tell the client the address and port of your server on your network. You also need to tell the server which address and port to be listening on.

For convenience lets bind on all IP addresses for the server. To do this set the host in the Server Code to "0.0.0.0"

For the Client config, you must put the address of the server. To do this I would put a line the server code to display the hostname of that machine.

#SERVER
import socket

def Main():
    host = "0.0.0.0"
    port = 5000

    print socket.gethostname()

    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 = #put hostname here
        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()


 
Sign up to request clarification or add additional context in comments.

3 Comments

it works on one computer, but does not work when the server and client are on separate computers returning a value of mySocket.connect((host,port)) socket.gaierror: [Errno -2] Name or service not found.
I figured it out, given the above, you can use the ip found in ifconfig(on linux) as the host I think its ipconfig on Windows.
How if we could not know the host's IP address without manually input them? Is it possible?
1

Try temporarily disabling the firewall on both computers. If that fixes it, then create rules to allow the traffic. Port 5000 is not common, so it is likely being dropped by your firewall.

EDIT: Make sure you turn your firewall back on. Disabling the firewall is strictly for debugging.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.