0

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)

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Apr 16, 2019 at 15:49
9
  • 2
    It'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. Commented 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? Commented Apr 16, 2019 at 16:08
  • 1
    No, 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. Commented Apr 16, 2019 at 16:16
  • 1
    Not 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. Commented Apr 16, 2019 at 16:28
  • 1
    You 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. Commented Apr 16, 2019 at 21:59

1 Answer 1

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.

answered Apr 16, 2019 at 21:26
Sign up to request clarification or add additional context in comments.

Comments

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.