5

Looking for a simple python solution for this:

Assume Machine A's local IP is 192.168.1.5 and public IP is 111.11.111.11, Machine B's local IP is 10.0.0.5 and public IP is 222.22.222.22, now I need to communicate between these two machines, how do you do that?

I've seen lots of socket programming examples with both server and client on the same network, but how do you connect machines behind different networks without changing router configurations(port forwarding etc.)?

EDIT:

What if Machine A's behind LAN: local IP 192.168.1.5, public IP 111.11.111.11; Machine B's an HTTP server with a public IP of 222.22.222.22; now you can easily reach B from A through HTTP requests, but what's the best solution to reach machine A from machine B?

Shane
  • 4,875
  • 12
  • 49
  • 87
  • Don't know python.. But I can tell you this.. A typical solution in this situation would be based on HTTP, and would probably involve an intermediary service to act as a Gateway / Relay. – Vikas Gupta Dec 31 '14 at 02:36
  • use 3rd public machine accessible from both e.g., http://localtunnel.me/ – jfs Dec 31 '14 at 02:40
  • @VikasGupta: I was thinking to build a simple HTTP server on each side so that they can send HTTP requests to each other to communicate, but without admin rights on routers on each side, you cannot really reach to either side... – Shane Dec 31 '14 at 02:40
  • @Shane true, which is why I suggested that you'd need an intermediary.. to act as Gateway / Relay, which will have to be publically accessible, and act as server, where as the two machines behind the firewall will act as HTTP clients (not servers). – Vikas Gupta Dec 31 '14 at 02:43
  • related: [UDP Hole Punching Algorithm](http://stackoverflow.com/q/8892142/4279) – jfs Dec 31 '14 at 02:43
  • @VikasGupta: yeah you can do that, but it's not real-time, so still not a good solution... – Shane Dec 31 '14 at 02:53
  • 1
    *"it's not real-time"* -- what do you mean by that? (port-forwarder works in "real-time") Are there are other requirements? (provide more context, to make the question more specific otherwise it is too broad). If A->B works then maintain a long-lived connection to be able to send message from B to A at any time. – jfs Dec 31 '14 at 03:21
  • @J.F.Sebastian: Sorry for the misleading context, what I'm trying to say is, B is actually an HTTP server, and although A->B works, I cannot afford a long-lived connection, and do not want to send frequent requests from A->B either, by real-time I mean if at a certain moment when B want to reach A, what should I do to initiate such a connection(since A is behind LAN)? – Shane Dec 31 '14 at 03:47
  • 1
    Any device with a 192.168. or 10. IP address will be behind a NAT router and inaccessible directly. You *need* to rely on the router for a connection. Either you need to configure the router for forwarding, or you need to let the device behind the router initiate the connection. There's no other way. In the case where they're *both* behind different routers without forwarding, you need a third party. – Mark Ransom Dec 31 '14 at 03:48
  • @Shane: don't put it in the comments, [edit] your question instead. There is not enough information still. *"B is actually an HTTP server"* does it mean you can't install anything on B? You can't make `B -> A` connection without `A -> ` connection first. Follow the link I've provided above e.g., read how ICE protocol works. – jfs Dec 31 '14 at 04:00

2 Answers2

2

Hole Punching is one way to achieve this. Hole punching is a NAT traversal technique, which allows direct communication between devices behind NATs. Not all NATs support hole punching. But a good percentage of NATs allow.

http://www.brynosaurus.com/pub/net/p2pnat/

This link explains it in detail. They have given the statistics also.

Seema Kadavan
  • 2,538
  • 1
  • 16
  • 31
0

For the first case, when both the devices are behind NAT, you need to either do hole punching or use a UPnP python library to forward ports if you don't want to forward them manually.

For the second case, the program on machine A must initialize the connection. The NAT on this end will create a translation entry in it's table. Basically, any packets sent from machine B on your network's public IP address will now be forwarded to machine As local IP address.

Dodu
  • 224
  • 1
  • 6