I'm just starting to learn an introductory book on networking, and have come across creating a simple client server program using TCP. The code for server is:
import socket as soc
serverport = 12000
server_socket=soc.socket(soc.AF_INET,soc.SOCK_STREAM)
server_socket.bind(('', serverport))
server_socket.listen(2)
max_bytes = 2048
connection_socket, addr = server_socket.accept()
x = connection_socket.recv(max_bytes)
print("Your sender has sent you: ", x.decode())
connection_socket.send('I received your msg'.encode())
connection_socket.close()
and the code for client is:
import socket as soc
servername = 'xx.xx.xx.xxx' <- In this I put someone's public IP address
serverport = 12000
client_socket=soc.socket(soc.AF_INET,soc.SOCK_STREAM)
client_socket.connect((servername, serverport))
sentence='This is mathmaniage'
client_socket.send(sentence.encode())
#waits
modified_sentence=client_socket.recv(2048)
print(modified_sentence)
client_socket.close()
If IP identifies hosts and the code above is supposed to establish TCP connection and it works on my local host, why doesn't it work on two different computers on two different networks? (Like my friend's PC and my, so instead of 'xx.xx.xx.xxx' I write my friend's IP address)
-
2It's not that simple anymore on the pubic internet. Various firewalls and quirks of network topography will prevent random incoming connections on unknown ports. Try on on your local network.Blorgbeard– Blorgbeard2019年04月16日 15:55:21 +00:00Commented Apr 16, 2019 at 15:55
-
@Blorgbeard, works on my local network on ubuntu. What else can be there blocking the connection? But, does that mean the msg does get to the IP address? And it's only not working because something at the end host is discarding it?mathmaniage– mathmaniage2019年04月16日 16:08:19 +00:00Commented Apr 16, 2019 at 16:08
-
1No, in all likelihood your friend's ISP is blocking the connection. If it made it past that, your friend's router would probably also block it (with firewall and NAT). Your friend's ISP may also be using some form of NAT.Blorgbeard– Blorgbeard2019年04月16日 16:16:43 +00:00Commented Apr 16, 2019 at 16:16
-
1Not necessarily - "IP identifies hosts" - not always. See: NAT. Honestly, I'm not a networking guy, and I don't know the details of you and your friends' networks. I suspect the easiest way to get this working is to rethink your architecture. If you wrote a server that both you and your friend connected to with clients, you could host the server on (e.g.) an Amazon EC2 instance (free tier is available). You can give that a public IP and allow connections on port 12000.Blorgbeard– Blorgbeard2019年04月16日 16:28:00 +00:00Commented Apr 16, 2019 at 16:28
-
1You have run into the giant kludge called NAPT that is the bane of the modern Internet because we have run out of IPv4 addresses. IP is designed to be an end-to-end protocol where every endpoint has a unique address, but the address space in IPv4 is so limited that we need workarounds until IPv6 is ubiquitous. NAPT breaks the IP paradigm. See the answer to this question.Ron Maupin– Ron Maupin2019年04月16日 21:59:56 +00:00Commented Apr 16, 2019 at 21:59
1 Answer 1
Its a bit more complex than you think , your friend need to have Static IP address and Port forwarding .
Read more here https://en.wikipedia.org/wiki/Port_forwarding
And you have to check if the ISP is blocking your request .
a simple solution is VPN , you can use some free software to set up a vpn fast and simple with your friend.