Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi,

I have one doubts about tcp/ip.
I read a few tutorials about this subject and I made some projects to communicate inside my local net, at my home, but I can't figure out and I can't find anywhere what I must to do to communicate outside my local net. For instance, I am in Portugal and I want to make a app in c++ to share files or communicate with a friend of mine which is in Brazil.

My guess is that I need my global ip, but how can i get it? I only get the local ip.

Thanks in advance
Filipe
Posted
Comments
Resmi Anna 15-Dec-11 5:48am    
Did you check the function gethostbyname()?
Filipe Marques 15-Dec-11 15:57pm    
Thanks Resmi for your tip, but I try gethostbyname() and I still get the local IP.
[no name] 15-Dec-11 20:52pm    
5!

This is not a C++ question...

What you need to do is configure your router or modem to do port forwarding.

This will tell it to route any traffic going to a specific port, to an IP or machine that you specify.

The default behavior is to just drop any packets that were not requested.
 
Share this answer
 
Comments
Filipe Marques 15-Dec-11 16:05pm    
Thanks Jack.
Makes sense. So, if I understood, I configure my router to a certain port and a certain ip address and when I send data for that port and that ip address, I can receive in my computer.

And I don't take the chance of that ip address and port be used in other computer?

Thanks
JackDingler 15-Dec-11 16:27pm    
Your router has a public IP. It's unique on the internet. All of your computers behind the router have unique private IPs. Their IPs are unique within your network.

So the answer is yes. If you send a packet to that predefined port to the public (outbound) interface of your router, then your router will send it on to the system that it is configured to forward to.
This is a good basic question, which is often misunderstood.

If you are a client, and initiating a connection, you do not need a global IP address yourself. However, you need to know the other party's (server's)unique, routable, global IP address. For example, if your friend in Brazil is a server, and has a global IP, you can initiate a connection to his computer from your home computer which may have a private IP address range (such as 10.x.x.x, 192.168.x.x and so on).

Once a TCP/IP connection has established, you can communicate both ways.

The other way around (server on a private IP address) is generally, not always possible, unless you start mucking around with router settings such as mapping ports through NAT and NAPT, or if you use dynamic DNS. If you are learning about TCP/IP, do not worry about it until you get the basics.

Try to understand the concept of source and destination TCP ports, and how a connection is uniquely determined by four parameters (2xTCP port numbers, 2xIP addresses).

Hope this helps.
 
Share this answer
 
Comments
Filipe Marques 17-Dec-11 9:52am    
But I always need a global IP, if I am server, my IP, if my friend is a server, his IP.

Imagine I want create a chat room. If I connect first, I am the server and my friend will connect to me as a client. If my friend connect first, he is the server and I am the client.

Filipe
michaelmel 18-Dec-11 19:23pm    
If your design is such that both sides should be able to act as a server (which sounds like a type of peer to peer protocol), then yes, both will need a global IP. However, chatroom is a bad example - you could host it on a server with a global IP, and let your clients (people who want to chat) connect to it. Clients do not need a global IP address themselves, but they will need to know the server (best done through DNS). So, the clients talk through the server. This is how (almost) every chat/messaging application works on the net.
You are describing a situation where your computer is hosting a server, and you need to know your public IP address so that you can give it to clients thereby allowing clients to connect to you.

The public IP address is difficult to determine from any single computer. There might be a situation, for example, where many computers on a local network all share the same public IP address that's exposed from a single modem/router.

In some systems, it's possible to discover the public IP address using the router which can inform a computer of the public IP address, using UPnP.

The simplest solution, however, is for the computer to access one of the many web sites that provide the public IP address that the site is responding to. One such site is www.WhatIsMyIP.com. Parse the response.

Another solution is DNS or dynamic DNS (DDNS), where you register your computer with a DNS server. For DDNS, the computer must also run a small application to advise the DDNS server of its IP address. The big advantage of DNS (or DDNS) is that your clients can find you using an ASCII text string (like microsoft.com), which the DNS server maps into your actual public IP address. One example of a DDNS provider (that's free) is www.no-ip.com.

Mike
 
Share this answer
 
Comments
Filipe Marques 17-Dec-11 9:39am    
Thanks.

Sometime ago I discovery a site like www.WhatIsMyIP.com but I thought that there was an easy way to get the public IP address by using a some function or some options of a function.

FIlipe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900