1

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

asked Apr 13, 2017 at 7:39
10
  • At first you should remove the try / except or re-raise the exceptin to see the actual error message. Commented Apr 13, 2017 at 7:43
  • Have you tried changing your except: to something like except Exception as ex:. This will allow you to see a better description of the exception with print(ex) and may provide a more information Commented Apr 13, 2017 at 7:45
  • Does ping from one machine to another works ? Commented Apr 13, 2017 at 7:47
  • Yep I tried those, updated the question Commented Apr 13, 2017 at 7:51
  • what happens if you use HOST = '0.0.0.0' in the server.py? Commented Apr 13, 2017 at 7:53

2 Answers 2

1

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.

answered Apr 13, 2017 at 7:56
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Looks like our code was all good but my wireless access point or router must have been doing some bad things. It's working now when I connect the laptops using ethernet cable rather than WiFi
0

Also try using this construction to obtain the IP address:

HOST=socket.gethostbyname(socket.gethostname())
answered Apr 13, 2017 at 7:58

3 Comments

That can often end up returning the localhost address
Pay attention with this. On one machine you should have gethostbyname() and on another "0.0.0.0". Otherwise the connection between the two will fail
That's not true, and sounds like cargo cult information. '0.0.0.0' is used on server sockets to listen on all available interfaces. It's perfectly valid to only listen on one interface, nominated by its IP address. There's absolutely no need to use hostnames at all - they are merely a convenience for human users.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.