-
Notifications
You must be signed in to change notification settings - Fork 0
Session 7: Client server model
-
Goals:
- Learn about the client-server model
- Write our first client using sockets
- Understand some concepts related to computer networks
- Duration: 2h
- Date: Week 4: Tuesday, Feb-12th-2019
- These session consist of the teacher's guidelines for driving the Lecture, following the learn by doing approach
- DON'T PANIC! Many concepts will be introduce! But we will use then 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. ¡Let's get to work! you will see that many exercises are redundant! (we are doing it on purpose!)
- Setting your environment
- Introduction to client server model
- Introduction
- IP Address
- Hi! Are you alive?
- URLs
- Ports
- Introduction to the programming of clients
- Classroom chat
- Authors
- Credits
- License
- Enter into your linux account at the LAB's computer. It is important that you work during this session in the Lab's computer: we need to execute the python applications inside Lab's local network in other to do the proposed activities
- Run pythcarm and open the project: 2018-19-PNE-practices. Get the lates version from github (update/pull)
- Create a new folder: Session-7. This is were you will work today
- Create a new file (text file) for taking notes. Call it: mynotes.txt (It is part of the project, so add it to github)
- Introduction to the client-server model: request, response
- Client and servers are applications inside a computer (and there are more apps in the same computer)
- Client and servers can be in the same computer (we will do it for developing our clients and server from our computer, without having to use two different computers. Which is good, because you will be able to test your projects from your home)
- Internet. How it works
- IP address: Numbers for identifying the network interfaces. IP examples: 216.58.201.174, 8.8.8.8
- Activity 1: Find the IP address of your mobile phone. write it down in your mynotes.txt file
- Exercise 1: Find the IP address of your mobile phone at home, or in other place different than the URJC university. Compare the two IPs. ¿Are they the same?
- Activity 2: Let's find the IP of your computer in the lab. Open the Terminal and execute this command:
ifconfig
Along with many other information, you will find your IP Address. Write it down in your notes (we will need it later)
- Ping!: Explain the ping command
- Activity 3: Let's ping the computer at this IP address: 8.8.8.8
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
- Activity 4: Ping your own IP address. And write down the time
- Activity 5: Ask one of your mates for their computer's IP address (in the lab). Ping the machine and write down the time
- Activity 6: Ping your mobile phone!. Write down the time
- Domain name: For getting the IP address of a machine we use its domain name
IP address are difficult to remember for humans. We prefer to use characters and names. That's why we use the URLs. There are special servers in the internet, called DNS servers (Domain Name system)
- Activity 7: Ping the the www.google.com (URL)
ping www.google.com
Notice that it returns also its IP address. Try to ping directly to that IP
- Activity 8: Repeat the activity with another domain name (for example github.com, www.urjc.es....)
- IP identify the interface address of a computer. But inside a computer (or mobile, or tablet) there are many apps running at the same time
- For indentifying app we use the pair (IP, port)
- There are some standard ports for some services. For example the 80 port is reserved for web servers
- Try Some connections from the browser: to urjc.es:80, urjc:81... change the port
We will learn the basic ideas used for communicating a client an a server thought internet
- The communicating model is very similar to the already know model of reading and writing local files in your computer
- Let's review how to do that
- Activity 8: write a python program for reading your mynotes.txt fine, and printing the information on the console. Call it file_read.py
# Example for reading a file located in our local filesystem
# -- Filename to read
NAME = "mynotes.txt"
# -- Open the file
myfile = open(NAME, 'r')
# -- myfile is an object!!! Let's see what it has inside
# -- Print the filename
print("Print: file opened: {}".format(myfile.name))
# -- Read the whole file into a string
contents = myfile.read()
# -- Print the files's contents
print("The file contents are: {}".format(contents))
print("The end!")
# -- Close the file
myfile.close()
-
Activity 9: Run the program
-
Activity 10: Debug the program (step by step execution) Have a look at the objects created
-
Notice that the myfile is an object! It contains inside many methods and properties
-
Activity 11: Write a program, file_write.py, that do the same than the previous exercise but it should add some information in the end of the mynotes.txt file
FILENAME = "mynotes.txt"
# -- Open the file for reading
f = open(FILENAME, 'r')
# -- Read the content of the file
contents = f.read()
# -- Print the contents
print("File: {}".format(f.name))
print("{}".format(contents))
# -- Closing the file
f.close()
# -- Open again for adding
f = open(FILENAME, "a")
# -- write some information to the file
f.write("Helllo!!!! just writing into your file!\n")
f.close()
print("End")
-
For communicating with a program that is running in another computer we used the socket concept. Instead of opening a local file, it opens a "channel" for communicating with the other programs. The socket is an object, and as such, it has properties and methods for managing that channel
-
Launch the server in the teacher's computer (Teacher). Do not worry about this code. You do not have to understand it yet
import socket
# Configure the Server's IP and PORT
PORT = 8081
IP = "192.168.1.36"
MAX_OPEN_REQUESTS = 5
# Counting the number of connections
number_con = 0
# create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
serversocket.bind((IP, PORT))
# become a server socket
# MAX_OPEN_REQUESTS connect requests before refusing outside connections
serversocket.listen(MAX_OPEN_REQUESTS)
while True:
# accept connections from outside
print("Waiting for connections at {}, {} ".format(IP, PORT))
(clientsocket, address) = serversocket.accept()
# Another connection!e
number_con += 1
# Print the conection number
print("CONNECTION: {}. From the IP: {}".format(number_con, address))
# Read the message from the client, if any
msg = clientsocket.recv(2048).decode("utf-8")
print("Message from client: {}".format(msg))
# Send the messag
message = "Hello from the teacher's server"
send_bytes = str.encode(message)
# We must write bytes, not a string
clientsocket.send(send_bytes)
clientsocket.close()
except socket.error:
print("Problems using port {}. Do you have permission?".format(PORT))
except KeyboardInterrupt:
print("Server stopped by the user")
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 response with a greeting message
- Let's create our first socket:
import socket
# First, create the socket
# We will always use this parameters: AF_INET y SOCK_STREAM
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Print some information about the socket
print()
print("Socket created:")
print(s)
print("End")
- Activity 11: Run it. And debug it
- Activity 12: Setting our first connection. For connecting to a server we need to specify the IP address and the port
import socket
# SERVER IP, PORT
IP = "192.168.1.36"
PORT = 8080
# First, create the socket
# We will always use this parameters: AF_INET y SOCK_STREAM
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# establish the connection to the Server (IP, PORT)
s.connect((IP, PORT))
print(s)
print("End")
s.close()
- When the client is executes it connects to the server. And the server will show the following message:
Waiting for connections at 192.168.1.36, 8080
CONNECTION: 1. From the IP: ('192.168.1.36', 39092)
Message from client:
- Activity 13: Send some data to the server
import socket
# SERVER IP, PORT
IP = "192.168.1.36"
PORT = 8080
# First, create the socket
# We will always use this parameters: AF_INET y SOCK_STREAM
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# establish the connection to the Server (IP, PORT)
s.connect((IP, PORT))
# Send data. No strings can be send, only bytes
# It necesary to encode the string into bytes
s.send(str.encode("HELLO FROM THE CLIENT!!!"))
- Activity 14: Finally receive some data from the server
import socket
# SERVER IP, PORT
IP = "192.168.1.36"
PORT = 8080
# First, create the socket
# We will always use this parameters: AF_INET y SOCK_STREAM
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# establish the connection to the Server (IP, PORT)
s.connect((IP, PORT))
# Send data. No strings can be send, only bytes
# It necesary to encode the string into bytes
s.send(str.encode("HELLO FROM THE CLIENT!!!"))
# Receive data from the server
msg = s.recv(2048).decode("utf-8")
print("MESSAGE FROM THE SERVER:\n")
print(msg)
-
Exercise 2: Write a python client for implementing a chat. It should ask the user to enter a string and then sending the message to the Teacher's server, were we all will see all the messages. The progra should run in a loop (for sending multiples messages)
-
Exercise 3: Work in pairs. One should launch the server and the other the chat client. Then change
-
Exercise 4: Execute the server and the client in your own computing. This is usually how we test the client and server
- Boni García
- Juan González-Gómez (Obijuan)
- Alvaro del Castillo. He designed and created the original content of this subject. Thanks a lot :-)
S0: Introducction
S1: Tools I
S2: Tools II
S7: Client-Server-1
S8: Client-Server-2
S9: Client-server-3
S10: Client-server-4
S11: HTTP protocol-1
S12: HTTP protocol-2
S13: HTTP module
S14: HTTP module
S15: HTML forms
S16: HTML forms
S17: JSON and API REST
S18: JSON and API REST