Network: The Basis
Ah the network! If you are a programer you probably already thought about making your programs communcating through internet. We all did. This tutorial won't be about code, it explains only the basics about the network. You have to know what is explained here to really understand what is happening when you will start programming. So... let's start!
Local IP vs Public IP
What is an ip address? An ip address actually looks pretty much to a real home address but it is with numbers. It is used to make a computer unique! There is no other computer in the world with the same ip address as yours! An ip address may looks like these ones:
What is the difference? Well, the first one is a Public IP, the second one is a Local IP. Actually the computer on which you are reading this article doesn't really have a unique ip address, but your internet box does. All the internet boxes have a unique ip address, it is called a public ip, simply because everybody can "see" it. When you browse a website, the page sees your public ip: your internet box ip. In a way, you can say that every house has a public ip address (like the first one).
Then what is the second one? I told you your computer's ip wasn't unique, remember? It is true, it is not unique in the whole world, but in your house it is and this is why it is called a Local ip. What happen here is that when you connect your computer to your network (Wifi or Ethernet), your box gives to your computer an ip address, a Local one. This way the box can recognize any computer connected to it.
All public addresses are of the form: XXX.XXX.XXX.XXX
Most of local addresses are of the form: 192.168.X.X
This is a diagram summarising the process:
You can get your public ip by searching on the internet: "what is my ip address". And for your local ip, open your terminal/command line and type:
- Windows users: ipconfig. Search for the IPv4 field.
- OSX/Linux users: ifconfig. Search for inet addr field in the ethX/wlanX sections (ethernet/wifi)
Server, Client and Port
If you want to make an internet connection in your program you will definitely need to set a server/client architecture. The server and the client are actually two different programs, and each one "belongs" to someone. The server program is like the box in the diagram above, it listens for connections, and the client program is like any of the computers above, it connects to the server.
There is only one computer running the server program, but the clients may be two, three or four thousand.
Nota bene: Actually you can have more than one server, but it is a much more complicated architecture. All big companies like Facebook or Google have thousands of servers to provide a fast service. Unless you intend to have a very high traffic on your server you don't need more than one server. If you are interested in combining several servers powers to handle a high traffic, you can do it, it is called a Cluster. We will probably make a tutorial about it.
Now you have to know that when you start a server, it has to listen on a port. ...Wait what?
On each computer there is exactly 216 ports availables (from 0 to 65 535). Ports are like doors, they allow you to enter in a room (server). Each room (server) has its own door (port). When you start a server, you start it on a port; and when the client is connecting to the server, it connects to an ip ON a port. The first thousand ports (approximately) are already used by your OS (80 for website, 21 for FTP, 22 for SSH etc), all the rest are yours.
As you can see, I represented the port 5000 with an open door. It is because as a door, you can chose to open a port or to close it. If a port is closed, you cannot receive connections on it. All that stuff happen on the box side:
When you "open" a port, you actually redirect the traffic which comes through this port to a local ip address. If you want to start a server on your computer on port 5000, you must tell the box (which is your access to the internet) to redirect all the traffic which comes on its port 5000 to your local ip address; because when a client tries to make connection to the server, it connects to your public ip (the box) on the port 5000. So it is your box which receives the connection first; and "opening" the port 5000 will result in a redirection of the traffic which comes on this port to your local ip (your computer, where a server is running and listening on port 5000).
In order to open a port, you should go to your internet access provider website and manage your internet box to redirect the traffic to an ip.
Local Server vs Public Server
However, you can omit the port forwarding part if you are making a local server. Let me explain:
Until now, the clients in my examples, were connecting to a public ip (the box), you must do it like this if you want people from outsite your network to get access to the server. However, if you only need people on the same network as you to connect to the server, you don't need to open any port and the clients should connect to the local ip of the computer on which the server is running (192.168.x.x).
You can make both, local and public server, it doesn't affect the code on the server side (just for the client who has to connect on your local ip, no more the public one). It depends on your needs.
Well, this is the end of this tutorial. I hope I was clear, it is very important to understand those basis to continue in the future. Leave a comment for any question or suggestion!