... | ... | @@ -4,10 +4,10 @@ |
|
|
|
|
|
* **Time**: 2h
|
|
|
* **Goals**:
|
|
|
* Learn basic network concepts
|
|
|
* Understand IP addresses
|
|
|
* Check the connectivity of Internet machines
|
|
|
* Understand how a simple client works
|
|
|
* To learn basic network concepts
|
|
|
* To understand IP addresses
|
|
|
* To check the connectivity of Internet machines
|
|
|
* To understand how a simple client works
|
|
|
|
|
|
## Contents
|
|
|
|
... | ... | @@ -34,9 +34,9 @@ |
|
|
|
|
|
## Introduction
|
|
|
|
|
|
Now that we **master** the tools (pycharm, git, etc.) and we have recalled how to **program in python** (although I am sure you had it fresh), it is time to learn how to create our own applications capable of **communicating** between them (that's what this course is about!). There are many concepts that we should learn first in order to **fully understand** how communications work.
|
|
|
Now that we **master** the tools (PyCharm, git, etc.) and we have recalled how to **program in Python** (although I am sure you had it fresh), it is time to learn how to create our own applications capable of **communicating** between them (that's what this course is about!). There are many concepts that we should learn first in order to **fully understand** how communications work.
|
|
|
|
|
|
In this session we will **introduce** all these concepts. **DON'T PANIC!** We will use them during all the course. Therefore, you will have time to learn them. What is important is that you **practice**. And do the exercises many times. The more you repeat the exercises, the more you will learn
|
|
|
In this session we will **introduce** all these concepts. **DON'T PANIC!** We will use them during all the course. Therefore, you will have time to learn them. What is important is that you **practice** by doing the exercises many times. The more you repeat the exercises, the more you will learn.
|
|
|
|
|
|
## Introduction to client-server model
|
|
|
|
... | ... | @@ -50,17 +50,17 @@ Let's identify **all the parts** that are needed for this communication to happe |
|
|
|
|
|
* The client needs to identify the computer where the server is running. It needs its **"address"** (an IP address, I am sure you have heard about it)
|
|
|
* This address is **not fixed**; it depends on the network interface. You may have one, or two, and they may change.
|
|
|
* Inside the machine there usually are **many apps running** at the same time. You have to specify to which app you want to access to. It is specified via the **port** (I am sure you have heard about this concept as well).
|
|
|
* How can we send information to another app from our own apps? We need and object to send the information, and this is called a **socket**.
|
|
|
* The **client** and the **server** can be located in the **same computer** (we will do it for developing our clients and servers to avoid using two different machines (this is good, because you will be able to test your projects from your home)
|
|
|
* Inside the server machine there are typically **many apps running** at the same time. You have to specify which app you want to access to. It is specified via the **port** (I am sure you have heard about this concept as well).
|
|
|
* How can we send information to another app from our own apps? We need a way to send the information, and this is called a **socket**.
|
|
|
* The **client** and the **server** can be located in the **same computer** (we will do it for developing our clients and servers to avoid using two different machines. This is good, because you will be able to test your projects from home.
|
|
|
|
|
|
### IP Address
|
|
|
|
|
|
* **IP address**: Numbers to identify the network interfaces of a computer. IP examples: 216.58.201.174, 8.8.8.8
|
|
|
|
|
|
All the **computers** connected to internet has a special number that **identify** that interface. It is know as the **IP address**. Your mobile phone has an IP address. Your computer in the LAB has an **IP address** and so on.
|
|
|
All the **computers** connected to the internet has a special number that **identify** that interface. It is know as the **IP address**. Your mobile phone has an IP address. Your computer in the lab has an **IP address** and so on.
|
|
|
|
|
|
For getting the **IP address** of your **Linux machine** in the **LAB** follow the next steps:
|
|
|
To obtain the **IP address** of your **Linux machine** in the **lab** follow the next steps:
|
|
|
|
|
|
1. Open a terminal
|
|
|
2. Write the following command:
|
... | ... | @@ -75,7 +75,7 @@ You can also use the `hostname -I`command. |
|
|
|
|
|
### Ping: testing the connectivity of a computer
|
|
|
|
|
|
Once you know the **IP address** of one device, you can test whether it is **connected to internet or not** using the **ping command**. This command sends a **small message** to that computer and tells you if an answer has been detected (sometimes machines have this ping thing not operative and they do not answer to ping messages...).
|
|
|
Once you know the **IP address** of a device, you can test whether it is **connected to internet or not** using the **ping command**. This command sends a **small message** to that computer and tells you if an answer has been detected (note that sometimes machines they do not answer to ping messages...).
|
|
|
|
|
|
Open a terminal and try to detect if the machine with the IP Address 8.8.8.8 is connected:
|
|
|
|
... | ... | @@ -83,7 +83,7 @@ Open a terminal and try to detect if the machine with the IP Address 8.8.8.8 is |
|
|
ping 8.8.8.8
|
|
|
```
|
|
|
|
|
|
You **stop** it by pressing **ctr-C**. The ping command also informs you about the time it takes for the Ping message to reach the machine and return to your computer. Notice that the values are not the same. Write down some of them in your notes (Exercise 2).
|
|
|
You may **stop** it by pressing **Ctrl-C**. The ping command also informs you about the time it takes the ping message to reach the machine and return to your computer. Notice that the values are not the same. Write down some of them in your notes (Exercise 2).
|
|
|
|
|
|
### URLs
|
|
|
|
... | ... | @@ -100,7 +100,7 @@ PING www.urjc.es (212.128.240.50) 56(84) bytes of data. |
|
|
...
|
|
|
```
|
|
|
|
|
|
The ping command returns the IP address of the pinged machinge: 212.128.240.50. We can ping it directly:
|
|
|
The ping command returns the IP address of the pinged machine: 212.128.240.50. We can ping it directly:
|
|
|
|
|
|
```
|
|
|
$ ping 212.128.240.50
|
... | ... | @@ -113,15 +113,15 @@ PING 212.128.240.50 (212.128.240.50) 56(84) bytes of data. |
|
|
|
|
|
### Ports
|
|
|
|
|
|
As previously stated, there are **many apps** running within the same computer. We use the **IP** to identify the **machine**, and the **Port** to identify the **service** (or application).
|
|
|
As previously stated, there are **many apps** running within the same computer. We use the **IP** to identify the **machine**, and the **port** to identify the **service** (or application).
|
|
|
|
|
|
In order to communicate with a remote app, we need the pair of values (IP, port). And the remote app also needs that information from our local app: (IP, port).
|
|
|
In order to communicate with a remote app, we need the pair of values (IP, port). And the remote app also needs that information from our local app (where we also will have an IP and a port).
|
|
|
|
|
|
There are some **standard ports** for several services. For example the **80 port** is reserved for **web servers**.
|
|
|
There are some **default ports** for several services. For example the **80 port** is reserved for **web servers**.
|
|
|
|
|
|
## Introduction to the programming of clients
|
|
|
|
|
|
We will learn the **basic ideas** used for communicating a **client an a server** throught internet.
|
|
|
We will learn the **basic ideas** used for communicating a **client and a server** through the internet.
|
|
|
|
|
|
### Sockets
|
|
|
|
... | ... | @@ -141,14 +141,14 @@ Each application that is willing to communicate with the outer world should do t |
|
|
|
|
|
### The nc tool
|
|
|
|
|
|
In Linux, for testing the communication between computers we have the **nc command** (netcat). It allows us to test our clients and servers. Let's do some experiments:
|
|
|
In Linux, for testing the communication between computers we have the **nc command** (netcat). It allows to test our clients and servers. Let's do some experiments:
|
|
|
|
|
|
* In the Teacher's computer the following command is ran:
|
|
|
* In the Teacher's computer the following command is executed:
|
|
|
|
|
|
```
|
|
|
nc -l 8000
|
|
|
```
|
|
|
It means that nc is working as a server, waiting to connections coming from other computers. It is "listening" in port 8000.
|
|
|
It means that nc is working as a server, waiting to connections coming from other computers. We say it is "listening" in port 8000.
|
|
|
|
|
|
From any other machine in the lab you can **connect to server** with the command:
|
|
|
|
... | ... | @@ -165,7 +165,7 @@ The `nc` command only allows the connection of 1 client to the server. So that o |
|
|
|
|
|
For programming our **first client** we need a server to connect to. This is the purpose of the **Teacher's server**. It will be running on the **Teacher's computer** so you can test your clients. Once you learn how to do it, you can launch the server in your own computer.
|
|
|
|
|
|
* This is the **code** of the **Teacher's server**. Do not worry if you don't understand this code yet. You will have time for that.
|
|
|
* This is the **code** of the **Teacher's server**. Do not worry if you don't understand this code yet. You will have time for that later on to run and understand it.
|
|
|
|
|
|
```python
|
|
|
import socket
|
... | ... | @@ -216,11 +216,11 @@ except KeyboardInterrupt: |
|
|
serversocket.close()
|
|
|
```
|
|
|
|
|
|
It will wait for the **clients to connect**. Once a client is connected, It will print the message given by the client (if any) and respond with a greeting message.
|
|
|
It will wait for the **clients to connect**. Once a client is connected, it will print the message given by the client (if any) and respond with a greeting message.
|
|
|
|
|
|
### Sending messages to the server from the command line
|
|
|
|
|
|
Once the **Teacher's server is running**, we will use the **c ommands printf** and **nc** for sending messages to it. Execute the following command from your LAB computer. Change the IP and Port according to the Teacher's specification:
|
|
|
Once the **Teacher's server is running**, we will use the **printf** and **nc** commands to send messages to it. Execute the following command from your computer in the lab. Change the IP and port according to the Teacher's specification:
|
|
|
|
|
|
```
|
|
|
printf "Testing!!! :-)" | nc 192.168.124.179 8081
|
... | ... | @@ -232,17 +232,17 @@ You will see the **server's response** printed on your console: |
|
|
|
|
|

|
|
|
|
|
|
In the **Server's console**, you will see **your message**:
|
|
|
In the **server's console**, you will see **your message**:
|
|
|
|
|
|

|
|
|
|
|
|
### Client-1: Creating the socket and sending a message to the server
|
|
|
|
|
|
Let's learn how to **send messages** from our **python programs**. We will **assume** that there is already a **server running** so we will connect to it to send messages.
|
|
|
Let's learn how to **send messages** from our **Python programs**. We will **assume** that there is already a **server running** so we will connect to it to send messages.
|
|
|
|
|
|
For doing so, we need **sockets**. And to create a socket we will use the **system module socket**.
|
|
|
|
|
|
Create a **client_1 .py** file with the following code (inside the S08 folder):
|
|
|
Create (inside the S08 folder) a **client_1.py** file with the following code:
|
|
|
|
|
|
```python3
|
|
|
import socket
|
... | ... | @@ -269,13 +269,13 @@ s.send(str.encode("HELLO FROM THE CLIENT!!!")) |
|
|
s.close()
|
|
|
```
|
|
|
|
|
|
When the **socket() constructor** is executed, a **new object** is stored in variable we have called **s**. It is our socket and it is an object so it offers a catalogue of methods that we may make use of. The method **connect()** is used to configure the **IP** and **PORT** of the **remote application**. The **send()** method is used to **send messages** to the remote app. And finally, the method **close()** is used to close the connection.
|
|
|
When the **socket() constructor** is executed, a **new object** is stored in a variable we have called **s**. It is our socket and it is an object so it offers a catalogue of methods that we may make use of. The method **connect()** is used to configure the **IP** and **port** of the **remote application**. The **send()** method is used to **send messages** to the remote app. And finally, the method **close()** is used to close the connection.
|
|
|
|
|
|
**Notice** that what is sent to the server are **raw bytes**. That is the reason why we must **encode** our **text messages**: we need to convert the strings into raw bytes.
|
|
|
|
|
|
### Client-2: Receiving and sending
|
|
|
|
|
|
This is a similar example, but in this case, the clinet waits for the **server's response**, and prints it on the **console**.
|
|
|
This is a similar example, but in this case, the client waits for the **server's response**, and prints it on the **console**.
|
|
|
|
|
|
```python3
|
|
|
import socket
|
... | ... | @@ -314,9 +314,9 @@ All the exercises and experiments performed during this session should be stored |
|
|
|
|
|
Let's play a little bit with the **IP address**. Try to find the IP address of your **mobile phone**. If you are connected to the URJC wifi you should have an IP.
|
|
|
|
|
|
How to find this information depends on your mobile phone. Usually, on **Android phones**, you can find it in **Settings/system/About the phone/state/IP address**.
|
|
|
How to find this information depends on your mobile phone. Usually, on **Android phones**, you can find it in **Settings/System/About the phone/state/IP address**.
|
|
|
|
|
|
Get the **IP address** of your **computer** in the LAB.
|
|
|
Get the **IP address** of your **computer** in the lab.
|
|
|
|
|
|
Open a new file (IPs.txt) and complete the following information:
|
|
|
|
... | ... | @@ -326,9 +326,9 @@ Open a new file (IPs.txt) and complete the following information: |
|
|
|Lab computer | Wire | | | |
|
|
|
|..... | ..... | ..... | .......... | .............. |
|
|
|
|
|
|
The first column is for your computer in the lab and/or your mobile. The second is the type of interface. Wifi or wire. The third is the date. The fourth is the place: home/Alcorcon Lab and the last one is the IP address.
|
|
|
The first column is for your computer in the lab and/or your mobile. The second is the type of interface: wifi or wire. The third is the date. The fourth is the place: home/Fuenlabrada Lab, and the last one is the IP address.
|
|
|
|
|
|
You can also get the IP of your phone when you reach **home**. And also, take note of your mobile IP when you reach the LAB next date. Write down all the IPs in the table.
|
|
|
You can also get the IP of your phone when you get **home**. And also, take note of your mobile IP when you get to the lab next date. Write down all the IPs in the table.
|
|
|
|
|
|
### Exercise 2
|
|
|
|
... | ... | @@ -336,7 +336,7 @@ You can also get the IP of your phone when you reach **home**. And also, take no |
|
|
|
|
|
Create a new text file: **e2.txt**. Fill it in with the time given by the **ping** command when executed from your LAB computer to different machines:
|
|
|
|
|
|
| Destination IP | Name (if known) | Time |
|
|
|
| Destination IP | Name (if known) | Times |
|
|
|
|------------------|-----------------|-----------------------|
|
|
|
| 192.168.124.179 | Self | 0.020 ms, 0.017 ms... |
|
|
|
| 212.128.255.129 | f-l3208-pc01 | |
|
... | ... | @@ -348,13 +348,13 @@ Create a new text file: **e2.txt**. Fill it in with the time given by the **ping |
|
|
| | newdomain.com.au| |
|
|
|
|
|
|
|
|
|
The first should be your own computer (ping to your IP address). The second is an IP address of a machine located in Fuenlabrada. The third is the google dns machine and the fourth you mobile IP address. Complete the table for the other URLs.
|
|
|
The first should be your own computer (ping your own IP address). The second is an IP address of a machine located in Fuenlabrada. The third is the google dns machine and the fourth you mobile IP address. Complete the table for the other URLs.
|
|
|
|
|
|
### Exercise 3
|
|
|
|
|
|
* **Filename**: S08/e3.py
|
|
|
|
|
|
Write a python program for implementing a **chat application**. Your client will ask the user to **enter a message**. Then, it will create a socket, connect to the **Teacher's server**, send the message, close the socket and repeat the operation:
|
|
|
Write a Python program that implements a **chat application**. Your client will ask the user to **enter a message**. Then, it will create a socket, connect to the **teacher's server**, send the message, close the socket and repeat the operation:
|
|
|
|
|
|
```python3
|
|
|
import socket
|
... | ... | @@ -379,7 +379,7 @@ while True: |
|
|
|
|
|
* **Filename**: S08/server.py
|
|
|
|
|
|
In this exercise you should learn to launch the teacher's server from your computer. Write the server in the file server.py. **Configure** the **IP address**: it should be the IP address of your computer. Then **Run** the server.
|
|
|
In this exercise you should learn how to launch the teacher's server from your computer. Write the server in the file server.py. **Configure** the **IP address**: it should be the IP address of your computer. Then **Run** the server.
|
|
|
|
|
|
Ask any of your mates to **connect** to the server, using the client developed in exercise 3. Once it is working, close the server, and connect your client to your mate's server, so that both of you have tested that everything works.
|
|
|
|
... | ... | @@ -399,7 +399,7 @@ The session is finished. Make sure, during this week, that everything in this li |
|
|
* [ ] e2.txt
|
|
|
* [ ] e3.py
|
|
|
* [ ] server.py
|
|
|
* [ ] All the previous files have been pushed to your remote Gitlab repo
|
|
|
* [ ] All the previous files have been pushed to your remote Gitlab repo.
|
|
|
|
|
|
# Credits
|
|
|
|
... | ... | |