mathmaniage
mathmaniage

Reputation: 319

Communicating via IP addresses

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)

Upvotes: 0

Views: 71

Answers (1)

George
George

Reputation: 2512

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.

Upvotes: 1

Related Questions