-2

Note : This problem has been completely solved, as was am running client.py before server.py

Just got started with socket programming, I have created the below code and expecting to print some byte message, but it isn't doing that.

I just want to make the message available for any person on any machine. But it's refusing by the machine to do that.

Here is my code:

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 12048

s.bind((socket.gethostname(), port))

s.listen()

while True:
    c, addr = s.accept()
    print("Got connection from", addr)

    c.send(bytes("Thank you", "utf-8"))

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 12048

s.connect(('192.168.0.1', port))

msg = s.recv(1024)
print(msg.decode("utf-8"))

Some images to better explain my errors:

server.py

client.py

Any help would be appreciated!!!

  • Are you sure that `socket.gethostname()` returns the hostname for the IP-address `192.168.0.1`? And are you sure about that `192.168.0.1` address? It's a common address for gateways but not common for hosts. – Some programmer dude Dec 09 '19 at 11:31
  • So what should i do? If i type another host name/ip address, it says firewall had blocked it. –  Dec 09 '19 at 11:32
  • Where are you running the server? Where are you running the client? Are they on the same system (machine, host)? Then use `127.0.0.1` for both the `bind` and `connect` calls. Otherwise you need to find out the actual public address of the server host, and use it for the `connect` call. I also recommend you use the wildcard address `''` for the `bind` call, so the server will listen for connections on all available interfaces. – Some programmer dude Dec 09 '19 at 11:33
  • Bud to 0.0.0.0 unless you have a good reason not to. – user207421 Jan 02 '20 at 04:41

2 Answers2

0

It looks like in the server.py script you use s.bind((socket.gethostname(), port)) where socket.gethostname() is a hostname, but in the client.py script you use s.connect(('192.168.0.1', port)) where '192.168.0.1' is the hostname you are trying to connect.

I think there you have socket.gethostname() != '192.168.0.1' and that's the problem.

Also, you can bind to all available IP addresses on the host using this solution Python socket bind to any IP?

Let's use listen_ip = socket.gethostbyname(socket.gethostname()) for connection since socket.gethostname() may return hostname instead of IP and it will not be solved by python dns resolver in local network if it was local name, not DNS.

and use it later as s.bind((listen_ip, port)) and s.connect((listen_ip, port))

After some debugging I've got a working solution for you

There are the scripts required.

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 12_048

s.bind((host, port))

s.listen()
print("Server listening @ {}:{}".format(host, port))

while True:
    c, addr = s.accept()
    print("Got connection from", addr)

    c.send(bytes("Thank you", "utf-8"))

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '192.168.1.162'  # The IP printed by the server must be set here

# or we can set it using env variable named SERVER_IP
if 'SERVER_IP' in os.environ:
    host = os.environ['SERVER_IP']

port = 12048

print("Connecting to {}:{}".format(host, port))
s.connect((host, port))

msg = s.recv(1024)
print(msg.decode("utf-8"))
Alexandr Shurigin
  • 3,921
  • 1
  • 13
  • 25
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/203930/discussion-on-answer-by-alexandr-shurigin-my-socket-program-in-python-isnt-prin). – Samuel Liew Dec 09 '19 at 18:49
-1

In a chat conversation we concluded that the hardcoded IP in the question is not the correct one. This solution does have the IP he needed but it will be different in each case. Remeber that server.py needs to be launched first, and when you see the printed Server listening @ IP:12048, write that IP in client.py and launch it. Client does need to be launched after seeing that line even if you already know the IP, as the server needs some time to be ready and the client will crash if it tries to connect to the server while it is not ready.

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 12048

s.bind((host, port))

s.listen()
print("Server listening @ {}:{}".format(host, port))

while True:
    c, addr = s.accept()
    print("Got connection from", addr)

    c.send(bytes("Thank you", "utf-8"))

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '192.168.1.162'  # The IP printed by the server must be set here
port = 12048

print("Connecting to {}:{}".format(host, port))
s.connect((host, port))

msg = s.recv(1024)
print(msg.decode("utf-8"))
Adirio
  • 5,040
  • 1
  • 14
  • 26
  • This code is totally correct. But not working on my machine. Now i believe it's machine's fault and not the code, from the beginning. Can you suggest me how to resolve this problem. –  Dec 09 '19 at 13:39
  • Corrected code how. What changed? Mere code is not sufficient. You have to explain. – user207421 Jan 02 '20 at 04:43
  • @user207421 There were 2 errors that were debugged in a chat conversation and are present in this scripts. Both are explained above. The first one was that he had a hardcoded IP in the client that did not match the server IP. By printing the `Server listening @ IP:12048` message as explained above you can get the server IP. And then use it in the client script as the comment suggests. The second error was that he was launching both script at the same time and the server needs a bit of time to be ready before the client can try to connect. This is also explained in the answer. – Adirio Jan 02 '20 at 07:57