简体   繁体   中英

python connect to server on same network

I have got a server.py and a client.py.

If i run them on the same computer using the same port and host as 127.0.0.1 it works fine.

I got another laptop connected to the same network. I got my local ip address 129.94.209.9 and used that for the server. On the other laptop I tried connecting to the server but I couldn't.

Is this an issue with my code, with the network or I'm just using the wrong ip address?

Server.py

HOST = '129.94.209.9' 
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
sockfd, addr = server_socket.accept()
send and receive messages etc....

Client.py

HOST = '129.94.209.9'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect((HOST, PORT))
except:
    print("Cannot connect to the server")
send and receive messages etc...

Client prints "Cannot connect to the server"

Thanks!!

UPDATES: thanks for the comments

1) I did do sockfd, addr = server_socket.accept() sorry I forgot to add it, it was a few lines further down in the code.

2) The error is: [Errno 11] Resource temporarily unavailable

3) Ping does work

EDIT: It is working now when I plug both computers in by ethernet cable to the same network. Not sure why my the won't work when they are right next to each other connected to wifi. Thanks for all the suggestions! I'll investigate the network issue myself

Using the following code (my IP address rather than yours, of course) I see the expected behaviour.

so8srv.py:

import socket
HOST = '192.168.33.64' 
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
print("Listening")
s = server_socket.accept()
print("Connected")

so8cli.py:

import socket
HOST = '192.168.33.64'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect((HOST, PORT))
except Exception as e:
    print("Cannot connect to the server:", e)
print("Connected")

When I run it, the server prints "Listening" . When I then run the client, both it and the server print "Connected" .

You will notice that, unlike your code, my client doesn't just print hte same diagnostic for any exception that's raised, but instead reports the exception. As a matter of sound practice you should avoid bare except clauses, since your program will then take the same action whether the exception is caused by a programming error or a user action such as KeyboardInterrupt .

也可以尝试使用这种结构来获取 IP 地址:

HOST=socket.gethostbyname(socket.gethostname())

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