-2

so im trying to establish connection between my friends pc and my pc but it keeps saying the host failed to respond this is the client

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(("192.168.0.177", 1234))

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

and this is the server

import socket
s = socket.socket(socket.AF_INET
, socket.SOCK_STREAM)
s.bind(('192.168.0.177', 1234))
s.listen(5)

while True:
    clientsocket, address = s.accept()
    print(f'conection from {address} has bene establish')
    clientsocket.send(bytes("welcome to the server", "utf-8"))

Update/Edit so i tried my public IP address and it says WinError 10049] The requested address is not valid in its context the error is serverside

2 Answers2

1

In the comments, you say that your friend is on a different LAN as you. The reason that he can not reach your server is because he is trying to reach your server on your LAN by connecting to an address (192.168.0.177) in the 192.168.x.x. space from his LAN.

See https://en.wikipedia.org/wiki/Private_network, where it explains that addresses in the 192.168.0.0 – 192.168.255.255 range are reserved for private networks. As such, IP packets originating from or addressed to a private IP address cannot be routed through the public Internet.

To solve this problem, you should setup a port-forwarding rule your router, to forward incoming connections to port 1234 on the public side of your router to 192.168.0.177:1234 on the private side of your router. Then, your friend should connect to xxx.xxx.xxx.xxx:1234, where xxx.xxx.xxx.xxx is the public IP address of your router. You can find the public ip address of your router by pointing your web browser to www.whatismyip.com from a computer on your LAN. If the port-forwarding is working correctly, your router should forward the incoming connection from your friend to your server at 192.168.0.177:1234 running on your private LAN.

mti2935
  • 11,465
  • 3
  • 29
  • 33
0

The IP range 192.168.0.0 to 192.168.255.255 (192.168.0.0/16) is considered to be the private range. Even if you design your routers to force the packets from these sources to the outside internet world, other routers will be discarding the packets.

You need to have both the machines on the same LAN or you need to set up a network on your own and configure routers (routing tables) in between or a public IP for accepting connections.

m0hithreddy
  • 1,752
  • 1
  • 10
  • 17
  • i setup a protforward thats what teh 192.168.0.177 is – Hyper Strike1212 Jun 28 '20 at 11:56
  • @HyperStrike1212 You are having ``192.168.0.177`` behind a router which has public IP? – m0hithreddy Jun 28 '20 at 11:58
  • no i setup a protforward that i have used in teh past for mc servers etc and thats the ip i gave him – Hyper Strike1212 Jun 28 '20 at 11:58
  • @HyperStrike1212 Ok on which machine you setup port-forward, on machine ``192.168.0.177`` or some other machine which is publicly accessible? – m0hithreddy Jun 28 '20 at 12:01
  • its hosted on my pc – Hyper Strike1212 Jun 28 '20 at 12:26
  • @HyperStrike1212: Your port forward may be correct but the client must connect to the **public IP address** of your router, not 192.168.0.177. – President James K. Polk Jun 28 '20 at 14:56
  • when i put my routers ip it says [WinError 10049] The requested address is not valid in its context – Hyper Strike1212 Jun 28 '20 at 17:45
  • @HyperStrike1212 After a quick Googling, I found this https://stackoverflow.com/questions/23857942/winerror-10049-the-requested-address-is-not-valid-in-its-context Seems like you changed the IP you are binding the server to an external IP. You dont need to change the server code. In the client instead of 192.168.0.177 you need to have router public IP. If all other things are correct, you are good to go :) – m0hithreddy Jun 28 '20 at 17:51
  • @HyperStrike1212 Yes it gives, because you are trying to bind to an external IP in your server. With server code unchanged and in client code changing ``connect(("192.168.0.177"....)`` to ``connect(("router ip"....)`` still gives the error? – m0hithreddy Jun 28 '20 at 17:57
  • @HyperStrike1212 Please edit the question to update your progress and attach the errors you are getting So that community will be interested in solving your problem. Just saying you got the error without greater details is not an easy way of explaining you problems. You need to be specific where you got the error, in client ? in server ? and what error is it? – m0hithreddy Jun 28 '20 at 18:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216838/discussion-between-hyper-strike1212-and-mohith-reddy). – Hyper Strike1212 Jun 28 '20 at 18:08