Acting as a server outside LAN - c++

I'm currently writing a peer-to-peer chat application to chat around the world, but my problem is that I can't connect() to another computer outside my LAN because although I have the public IP address of my friends and if necessary I could get their private IP address, how can I connect to their PC since the public IP address is the router's address, not the computer's one and I can only specify one address (either the public or the private). So how can I send the message to the computer in specific and not to the router without having to setup it?. I know it's possible because otherwise if you requested a web page all the computers in your lan can get your private information (in a wired scheme, since in wi-fi it's clearly possible).
Any help would be appreciated, sorry for my poor English :D.

There are two possibilities:
1 Read around here: http://en.wikipedia.org/wiki/STUN
2 Go for IPv6! ;-)

Actually the private IP address of computers on the other side of the router will not help you. In these situations, if they initiate the connection to you, then you are fine as you can respond on that connection.
If you want to initiate the connection to them, you must to it to their public IP address and they must have their router configured to map the inbound request (on whatever port) to the correct IP address in their network.

First, you need to be sure that the computer you are trying to connect is listening to a specific port (the same one that you are trying to connect to).
You will also need to configure your friends routers so that they forward all the request on that port to your friends' computers. If you don't have access to the routers, it is probably best to use a computer that is not behind any router as the chat server.

The ideal solution is to use uPNP. This allows your application to talk to the router(s) on the current network and ask them to forward the appropriate messages to you. This then makes a hole in the firewall which allows your app (on another PC) to connect.
Assuming both apps do this, either end can initiate the connection. If only one end has the app, then the OTHER end must initiate connection.
Imagine what would happen if everyone on the internet could just send messages to anyone - with no software pre-installed...

Related

Can I still do port forwarding in order to make my local website public if I have two routers between me and the internet?

I am trying to host a website from my house. (It does not have to be very reliable and consistent, hence I'm hosting from my house from a Raspberry Pi).
My website is obviously working fine locally. I tried to configure port forwarding on my router. It did not work. I then realized that my IP address on https://www.whatismyip.com/ and my router IP address is not the same. After reading up I am suspecting that I am behind another router. That would make sense since I'm living in an estate/housing complex and one ISP provides internet to all units. I can't access the next router (probably due to security reasons from my ISP).
My question is, is there any way around this? Would an ISP in general perhaps grant me a port if I ask them?
P.s. I am using phpmyadmin with my website and I am using Java on a cron-job to execute a program every 2 minutes which changes the DB. So I am not sure website hosting on a third party site would be trivial in my case.
it works now. I emailed my ISP to ask about the situation. Without further questions they assigned me a public IP address. I configured port forwarding and it works. Seems like you only get a public IP if you ask for one.

Connection from external computer to computer in local network

Currently in my chat p2p app, I need to open the port for other computers can connect to, but static ip is not allowed by the admin to open the port. Then I found a network programming exercise that seemed like a solution to this problem. The requirements are as follows:
"Write a program to test the UPnP protocol to
ADSL modem controller opens NAT gateway automatically.
In case you can not control the modem, find out and install a NAT Traversal technique to connect two clients in two NAT networks.
internet (use an intermediary server for primers
connect)."
Can anyone tell me what is an intermediate server for connection primitives?
Check https://www.noip.com/ :P. Maybe this can solve some of your problems ^^
You can simple setup DynDNS services. You will have one external domain name with any ip address.
But best way to setup SoftEther VPN solution. That can pass thought any NAT. You can keep your application server at the NAT subnetwork too. And that server will registered on common EtherVPN registry that allow connects from anywhere.
If you want smart solution embedded in your application. Please check similar solutions for VoIP communications. Like ICE, STUN, TURN. But that will not simple to implement.

C++ sockets: communication between PCs over internet

I'm writing a program on Windows using winsocks that can send messages to another computer. The client connects with the server in the other computer and begin exchanging data.
It works fine on my local network using local addresses(192.168.1.*), but I can't communicate with public addresses (216.185.45.129); not even my own. I can successfully connect to a website on port 80, but not to my laptop at home using its public IP address, regardless of what ports I use (unreserved ports).
So I did research online and the only solution that seems to work is port forwarding.
-But is there absolutely no other way to achieve this?
-How do other programs like Teamviewer connect to other computers on the network then?
-Is there an already open but typically unused port that I can use?
-At the very least, can I forward the ports on my router but not have the client do anything? Or maybe have my program forward the ports automatically.
The main problem is, that every router is using NAT to distinguish different computer in your lokal network against the WAN. He need to do this, because you got only one IP in the internet, but several devices in your home. To archive this, he uses groups of ports. That means, if you use to send maybe from port 2048 to a webserver in internet with two devices, the router gives one device another port (like 2049). The response has the Port of the requester, so the router can map it back. Unfortunately most router always map ports so you never now which port you have from the internet side.
There are two common ways to work around and archive your goal.
Port Fowarding
You can force most router not to map special ports but bind them to unique MAC addresses. You can use UPNP to config most router to do that, but I do not recommend that for security reasons and also it does not work in many enviroments where Router do not allow UPNP manipulation.
Most router have port forwarding abilities for gaming reasons (mostly it is used in P2P networks)
It works with TCP and UDP.
NAT Traversal
The common way is NAT traversal, also known as NAT hole punching. I will describe it in short for UDP. You can find a wiki explanation here for TCP and for UDP here. Unfortunately you need a server in the internet both clients can reach. Here the steps:
Both clients contact the server. The server now know IP and PORT of both clients.
Server send back the information to the clients.
Both(!) clients send now packages to each other on the known address.
It is necessary that both client send a UDP package and have to accept that the first package get lost. The reason is the router. Most router only accept packages from a source on a mapped PORT if a client has send a package to that source before.
UPDATE
Regarding to a comment of Remy Lebau I changed the Firewall piercing part to NAT Traversal as it was partly wrong.

The most important basics of P2P

I've been reading around on the www but just can't get the most important basics of P2P.
The diagram is like this:
[peer1]<-->[dsl-router1]<-->[central server]<-->[dsl-router2]<-->[peer2]
I'm developing a chat software on the central server. Chat messages being transfered thru' the central server well by now, however, I need to make the p2p file sharing feature because the bandwidth (the cable bandwith, not the transfer limit) of the server supposed for transfering chat messages only.
The problem is that, my software on central server knows the IPs and ports of router1 and router2, but not the peer1 and peer2 as these peers are behind the routers and don't have IP addresses.
How to actually transfer some data from peer1 to peer2 and vice versa without having this data passing thru' central server?
(and the worst case is that there is a wireless router between peer and dsl-router)
There are two basic ways of doing this. The new way is to use IGDP (opening a port via uPnP). This is described quite well here:
http://www.codeproject.com/Articles/13285/Using-UPnP-for-Programmatic-Port-Forwardings-and-N
If neither of the two nodes have a router supporting uPnP then another alternative is TCP hole punching, which is not perfect but works quite well in practice. This is described here:
http://www.brynosaurus.com/pub/net/p2pnat/
During some situations, "routers" supplied by the ISP may run on bridge mode, which directly exposes the peer computer on the internet (the computer gets a public internet address). If at least one side has this configuration (or in a similar situation that the peer client is not behind another device), then things should be rather straight forward: simply assign the central server's job to whoever that have this privilege.
In the other case where both peers only have a local address (e.g. 192.168.0.2) assigned to their computers, it would then be rather difficult to get through the routers; clients behind routers are for the most part unreachable from the outside unless they originated the request. Then, one solution to the problem is port forwarding. By doing port forwarding, either through explicitly written rules or UPnP, some ports on the peer computer is exposed to the public internet, as in the first situation where instead of only some ports the entire computer is exposed.
If you are without either of these, then there is no simple way to avoid sending through the central server. Though you could, potentially, find other peers who have the capability to transfer for others.

Making my TCP server Internet-facing - where should I start?

I have written a client/server code.
The server program executes on a terminal which plainly receives text data from the client and the client is a GUI where in you could specify the IP address of the machine where the server would be running.
However this works only in a closed Network(LAN).
I have just learnt TCP IP and have written a few codes that run on a LAN.
I wanted to make this program work across the network(over the internet).
But I have some basic doubts like,does one need the permission of the local ISP for such programs to execute across the internet.Does it involve buying a domain or some kind of permissions?
Can some one please help me on,what should I be doing,or where should I start from?
Listener have to have IP port opened in some way. If you are behind router, you should set proper port forwarding on router and if ISP provides its own subnet, you should know how to setup such link. (i do not know what kind of tehnology ISP might use for this).
For beginning you do not need you own domain name but you should be able to address by ip. If you need domain, register own domain name or create subdomain for free (i was using http://freedns.afraid.org/ )
If your server is behind a router which creates a LAN, you have to configure the router that it will forward the packages from your client to the server.
You have to forward all the incoming packages at the specific port to the local ip of the server.