0

I want to connect my two different devices using sockets library in Python. Both my devices are connected to the same Internet connection. I tried to use the sockets library to establish the connection. I ran the server script on one device and the client script on another device and tried to establish a connection. But the connection was not established. Is there a problem in my code or there is some other issue?

server.py:-

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((socket.gethostbyname(socket.gethostname()), 5050))

print("Server Starting")
sock.listen()

while True:
    conn, addr = sock.accept()
    print("New connection " + str(addr))

client.py:-

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('Server IP address', 5050))
4
  • Does this answer your question? Python simple socket client/server using asyncio Commented Oct 30, 2021 at 11:12
  • Also what does socket.gethostname() return? It should be the IP you are trying you connect to, or '0.0.0.0' (which it won't be, but you could put it manually there). Commented Oct 30, 2021 at 11:15
  • The 'gethostname' function in socket returns the name of the device. And the 'gethostbyname' returns the ip address of the host name. So, socket.gethostbyname(socket.gethostname()) will return the ip address of my device Commented Oct 30, 2021 at 18:09
  • This link: "stackoverflow.com/questions/48506460/…" did not answer my question. My question was that, is there a problem in my code or there is some other issue that is causing the problem of the connection? Commented Oct 30, 2021 at 18:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.