简体   繁体   中英

Python Sockets: how to connect between two computers on the same wifi

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?

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()


 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM