Can Sockets Be Used To Connect Multiple Computers On Different Networks In Python?
Solution 1:
Sockets should work, but there are some caveats:
- If the server (assuming you are using TCP sockets) is behind a firewall or NAT (typically if you have a router) you will need it to be configured to redirect the port on the public interface (visible from the Internet) to the local port.
- You will need to know your friend's public IP or hostname to connect to them.
If your networks look like this:
+---------------+ +-----------+ +--------+ +---------------+ +------------------+
| Your computer |-->|Your router|-->|Internet|-->|Friend's router|-->|Friends's computer|
+---------------+ +-----------+ +--------+ +---------------+ +------------------+
192.168.0.5 host.isp.com friend.isp.com 192.168.2.55
And your friend is running the server on his local network, on port 9000 (for example), then you'd connect to friend.isp.com:9000. If their router is configured to redirect traffic on port 9000 on the friend.isp.com interface (internet) to 192.168.2.55 (local machine) then a connection should be established correctly.
Solution 2:
It is possible but it will require some configuration on the router of your friend. The reason is that nowadays everybody has a public IP address that is provided by your service provider. This IP address is not fixed per se, but it is easy to lookup what is the current IP@ https://www.whatismyip.com/
Now sending is not going to be a problem, but receiving is. Some service providers like here in Belgium Telenet don't allow just any port to be used and you have to find out which ports are allowed. The well known ports are most likely not allowed. That for instance makes it impossible to host a webserver without first contacting your ISP. Port 10000 for instance will work. Contact your ISP to find out these kind of limitations.
Next problem is that your both have a router and a private network with private IP@ behind a NAT/PAT enabled router. For instance if I do ipconfig on my pc I get following IP@ 192.168.1.99. This IP@ is unique behind my router, but it is not unique on the whole internet so these IP@ cannot be used directly when communicating over the internet. So the router is going to use NAT/PAT and some lookup table that is filled based on the outgoing packets. You send something, the nat/pat table is build, your private IP@ is replaced with the public one and an allocated port. When a reply comes to you the port is used to change it back to the IP@ of the original request. For this reason sending is not a problem but receiving is.
To solve this problem your friend has to setup port forwarding or put a pc in a demiliterized zone, it depends on the router in question. Port forwarding is like manually filling the NAT/PAT table with an entry. If something arrives on this port, send it to that private IP@ using that port.
Next problem on both sending and receiving machines is the firewall. The firewall has to be switched off or configured to allow outgoing and incoming traffic on the ports that you are planning on using.
If you understand all that then you can make it work. But if this is all Chineze to you, then you will have a lot of difficulties to make it work. So conclusion is that it is possible but there are quite some caveats to tackle. There are many questions of people that are trying to do exactly what you describe here and have a lot of trouble making it work. If you are really a novice I would try to find somebody with experience or you will lose your hair trying.(by pulling it out yourself from frustration)
The fact that you are using python or any other programming language is not relevant in this discussion. It is pure a networking question.
More about NAT/PAT : http://www.webopedia.com/DidYouKnow/Computer_Science/NAT_and_PAT.asp
Solution 3:
The major (abstract) idea is simple. You have two node (computers) in a network (this time internet), so you should be able to connect both of them together.
You can establish a tcp connection between them.
Sockets are just fine for your case, all you need is a public IP for each one of them and a free port in each node. You can select your ports from this range.
Python provides a socket module , check it, it is very simple and easy. You may have a look at some examples as well.
However practically speaking, you would need the open these ports in the firewall, and also do some configuration depending on your operating system to allow this kind of communication.
Solution 4:
Yes it is possible.
All you need is the hostname or IP address and port number of the computer that you want to connect to. As long as a route can be established to the remote machine, a connection can be made.
Look at the socket
module. Here's a simple example:
importsockets= socket.socket()
s.connect(('123.123.123.123', 10000))
This will attempt to establish a TCP connection to the remote machine with IP address 123.123.123.123 on port 10000. The remote machine will need to have port 10000 open, i.e. a service listening for connection on that port, as well as port 10000 being open in any firewall device.
Instead of the IP address, you can also use a host name:
s.connect(('remote.host.com', 10000))
Post a Comment for "Can Sockets Be Used To Connect Multiple Computers On Different Networks In Python?"