CAN201 W3

This’s note is about the lecture and lab of Week2 CAN201.
The lecture III - Application Layer.

  1. Domain Name System (DNS)
  2. P2P Applications
  3. Socket Programming 套接字编程

Lecture

DNS

aka domain name system

  • Application-layer protocol: DNS是一个应用层协议
    • C/S architeture client-server architeture 不是p2p的
    • UDP (port53) 传输方式(无保障 unreliable) 方便提取 不需要握手🤝
    • hosts, name servers communicate to resolve names (name/address translation)
  • Distributed database implemented in hierarchy of many name servers 分布式数据库在多个名称服务器的层次结构中实现 分层化命名可以减小重名的可能性 分布数据库解决名字到ip解析的关系,方便维护

services, structure

DNS services

  • Hostname to IP address translation (A) 最重要的就是翻译功能
  • Host aliasing (cname)
    • canonical, alias names 别名问题
  • Mail server aliasing (mx)
  • Load distribution
  • Replicated Web servers: many IP addresses correspond ot one name

Why not centralize DNS? 为什么DNS要用分布数据库?

  • Single point of failure 单点故障
  • Traffic volume 交通量
  • Distant centralized database 远程集中式数据库
  • Maintenance 维护

a distributed, hierarchical database

DNS: a distributed, hierarchical database
Client wants IP for www.amazon.com:

  • Client queries root server to find com DNS server
  • Client queries .com DNS server to get amazon.com DNS server
  • Client queries amazon.com DNS server to get IP address for www.amazon.com

root name servers

  • contacted by local name server that can not resolve name
  • root name server:
    • contacts authoritative name server if name mapping not known
    • get mapping
    • returns mapping to local name server

root name servers

域名的每一个点都是一层,根据这个点的内容来寻找对应的位置。

TLD, authoritative servers

  • Top-level domain (TLD) servers: 顶级域名服务器

    • Responsible for com, org, net, edu, aero, jobs, museums, and all top-level country domains, e.g.: cn, uk, fr, ca, jp
    • Network Solution maintains servers for .com TLD
    • Educause for .edu TLD (https://net.educause.edu/)
  • Authoritative DNS servers: 权威DNS服务器

    • Organization’s own DNS servers, providing authoritative hostname to IP mappings for organization’s named hosts 组织自己的DNS服务器,为组织的命名主机提供权威主机名到IP映射
    • Can be maintained by organization or service provider 可由组织或服务提供商维护

Local DNS name server

  • Does not strictly belong to hierarchy
  • Each ISP (residential ISP, company, university) has one
    • Also called “default name server”
  • When host makes DNS query, query is sent to its local DNS server
    • Has local cache of recent name-to-address translation pairs (but may be out of date!) 本地缓存最近的名称-地址转换对
    • Acts as proxy, forwards query into hierarchy 充当代理,将查询转发到层次结构

DNS name resolution example

两种查询方法

  • Host at XJTLU wants IP address for www.feimax.com
  • Iterated query 迭代查询
    • contacted server replies with name of server to contact
    • “I don’t know this name, but ask this server”
    • 相当于不知道之后指路,让用户去问其他服务器
      DNS 迭代查询
  • Recursive query 递归查询
    • Puts burden of name resolution on contacted name server
    • Heavy load at upper levels of hierarchy
    • 一条路走到底,再走回来
      DNS 递归查询

caching, updating records

缓存,升级记录

  • Once (any) name server learns mapping, it caches mapping
    • Cache entries timeout (disappear) after some time (TTL) 缓存条目超时后一段时间
    • TLD servers typically cached in local name servers 经常会缓存顶级服务器,所以根服务器不常被访问
      • thus root name servers not often visited
  • Cached entries may be out-of-date
    • if name host changes IP address, may not be known Internet-wide until all TTLs expire 如果主机名更改了IP地址,可能在所有ttl过期之前都不知道
  • Update/notify mechanisms proposed IETF standard 更新/通知机制提议的IETF标准
    • RFC 2136

DNS records

DNS: distributed database storing resource records (RR)
RR format: (name, value, type, ttl)
four types of DNS records

Loop up a domain name

There’re several ways to deal with.

  1. Use nslookup command.
  2. Use whois command.

DNS protocol, message

Query and reply messages, both with same message format. 查询和回复消息,都使用相同的消息格式。
message 1
message 2

Inserting records into DNS

  • Example: new startup “feimax.com”
  • Register name feimax.com at DNS register
    • Normally, you don’t need to set up the NS record
    • Insert A record for the IP address of your host
    • Insert MX record for email

Domain name is a scarce resource! 稀缺资源


Pure P2P architecture

  • no always-on server
  • arbitrary end systems directly communicate
  • peers change IP addresses
    Examples: file distribution, streaming, VoIP

File distribution: client-server vs P2P

Q: how much time to distribute file (size F) from one server to N peers?
Peer upload/download capacity is limited resource.
client-server vs P2P
Example:
client upload rate = u, F/u = 1hour, u_s = 10u, d_min >= u_s

File distribution time: client-server

  • server transmission: must sequentially send (upload) N file copies:
    • time to send one copy: F/u_s
    • time to send N copies: NF/u_s 发送N个file的大小再除服务器的带宽
  • client: each client must download file copy
    • d_min = min client download rate
    • max client download time: F/d_min 最慢的那个用户下载文件所用的时间


后来会线性增长

File distribution time: P2P

  • server transmission: must sequentially send (upload) at least one file copies:
    • time to send one copy: F/us 服务器也是有文件的,假如网络里没有人参与p2p那就是服务器直接向唯一的用户分发。
  • client: each client must download file copy
    • min client download time: F/dmin
  • clients: as total must download NF bits
    • max upload rate (limiting max download rate) is us + Sui 所有的客户端也可以作为服务器上传

P2P file distribution: BitTorrent 文件分发:比特流

  • File divided into 256Kb chunks 文件分成碎片
  • Peers in torrent send/receive file chunks
    BitTorrent 比特流
  • Peer joining torrent:
    • has no chunks, but will accumulate them over time from other peers
    • registers with tracker to get list of peers, connects to subset of peers
  • While downloading, peer uploads chunks to other peers
  • Peer may change peers with whom it exchanges chunks
  • Peers may come and go
  • Once peer has entire file, it may leave or remain in torrent

BitTorrent: requesting, sending file chunks

Requesting chunks:

  • at any given time, different peers have different subsets of file chunks
  • Periodically, Alice asks each peer for list of chunks that they have
  • Alice requests missing chunks from peers, rarest first

Sending chunks: tit for tat

  • Alice sends chunks to those four peers currently sending her chunks at highest rate
    • other peers are choked by Alice (do not receive chunks from her)
    • re-evaluate top 4 every 10 secs
  • every 30 secs: randomly select another peer, starts sending chunks
    • “optimistically unchoke” this peer
    • newly chosen peer may join top 4

Higher upload rate: find better trading partners, get file faster!


Socket Programming 套接字编程

Two socket types for two transport services

  • UDP: unreliable datagram
  • TCP: reliable, byte stream-oriented

Application Example:

  1. client reads a line of characters (data) from its keyboard and sends data to server
  2. server receives the data and converts characters to uppercase
  3. server sends modified data to client
  4. client receives modified data and displays line on its screen

Socket programming with UDP

UDP: no “connection” between client & server

  • No handshaking before sending data
  • Sender explicitly attaches IP destination address and port # to each packet
  • Receiver extracts sender IP address and port # from received packet

UDP: transmitted data may be lost or recieved out-of-order
Application viewpoint

  • UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server

详细见ppt


Lab

  • File Operation
    • Text file – Write / Read
    • Binary file – Write /Read
  • Networking Programming Using Socket
    • UDP Server and Client
    • TCP Server and Client

Open

open method is exploit to operate files:

1
f = open(file='filename.xxx', mode='r')

File open modes

1
2
3
4
5
6
7
8
‘r’       open for reading (default)
‘w’ open for writing, truncating the file first
‘x’ create a new file and open it for writing
‘a’ open for writing, appending to the end of the file if it exists
‘b’ binary mode, used for media files
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)

Write

1
2
f.write(Str)  # Write a str to a file
f.writelines(StrList) # Write a list of str to a file

Read

1
2
3
f.read(n) # read n chars(including \n), n=-1 means read all
f.readline() # read one line (including \n)
f.readlines() # return a list of all lines

Text file appending

1
2
3
f = open('/Users/fei/lesson1.txt', 'a')  # open the file
f.write('Bye.\n') # add text into the file
f.close() # close the file

Binary file read

1
2
3
f.read(n) # read n bytes(including \n), n=-1 means read all 
f.readline() # read one line (including \n)
f.readlines() # return a list of all lines

Binary file write

1
f.write(bytes) # Write some bytes to a file

Binary file operation is very powerful, but we don’t go deep today.

Socket programming

UDP

1
2
3
4
5
6
7
8
9
10
11
12
13
# Server side
from socket import *

server_port = 12000
server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(('', server_port))

print('The server is ready to receive')

while True:
message, client_address = server_socket.recvfrom(20480)
modifiedMessage = message.decode().upper()
server_socket.sendto(modifiedMessage.encode(), client_address)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Client side
from socket import *

server_name = '127.0.0.1' # 127.0.0.1 is your local IP address
# Others should not use this IP. You need to use ipconfig(win)
# or ifconfig(linux, macos) to get you IP address in the network

server_port = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)

message = input('Input lowercase sentence:')
clientSocket.sendto(message.encode(), (server_name, server_port))
modifiedMessage, serverAddress = clientSocket.recvfrom(20480)
print(modifiedMessage.decode())
clientSocket.close()

TCP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Server side
from socket import *
server_port = 12000
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.bind(('', server_port))
server_socket.listen(1)
print('The server is ready to receive')

while True:
connectionSocket, addr = server_socket.accept()
sentence = connectionSocket.recv(20480).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.encode())

connectionSocket.close()
1
2
3
4
5
6
7
8
9
10
11
# Client Side
from socket import *
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = input('Input lowercase sentence:')
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(20480)
print('From Server:', modifiedSentence.decode())
clientSocket.close()

Lab

TCP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Server side
# TCP
from socket import *

server_port = 12000
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.bind(('', server_port))
server_socket.listen(2)
print('The server is ready to receive')

while True:
connectionSocket, addr = server_socket.accept()
xjtlu1 = connectionSocket.recv(20480) # 接受xjtlu文件
f = open('/Users/chenzhengde/PycharmProjects/CAN201Lab3/xjtlu1.jpg', 'wb') # 保存至xjtlu1 保存图片用wb
f.write(xjtlu1)
connectionSocket.send(xjtlu1) # 由服务器端发送至客户端
f.close()

connectionSocket.close()


# Client Side
# TCP
from socket import *

serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)

clientSocket.connect((serverName, serverPort))
f = open('/Users/chenzhengde/PycharmProjects/CAN201Lab3/xjtlu.jpeg', 'rb') # 打开xjtlu文件 打开图片用rb
image_data = f.read() # 赋值到image_data里
f.close()
clientSocket.send(image_data) # 发送至服务器

modifiedImage = clientSocket.recv(20480) # 接受服务器发来的xjtlu1
xjtlu2 = open('/Users/chenzhengde/PycharmProjects/CAN201Lab3/xjtlu2.jpg', 'wb')
xjtlu2.write(modifiedImage) # 保存至xjtlu2
xjtlu2.close()
clientSocket.close()

UDP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Server side
# UDP
from socket import *

server_port = 12000
server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(('', server_port))

print('The server is ready to receive')
while True:
xjtlu1, client_address = server_socket.recvfrom(20480)
f = open('/Users/chenzhengde/PycharmProjects/CAN201Lab3/xjtlu1.jpg', 'wb')
f.write(xjtlu1)
server_socket.sendto(xjtlu1, client_address)
f.close()


# Client side
# UDP
from socket import *
server_name = '127.0.0.1' # 127.0.0.1 is your local IP address
# Others should not use this IP. You need to use ipconfig(win)
# or ifconfig(linux, macos) to get you IP address in the network

server_port = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)

# message = input('Input lowercase sentence:')
f = open('/Users/chenzhengde/PycharmProjects/CAN201Lab3/xjtlu.jpeg', 'rb')
image_data = f.read()
f.close()
clientSocket.sendto(image_data, (server_name, server_port))

modifiedImage, serverAddress = clientSocket.recvfrom(20480)
xjtlu2 = open('/Users/chenzhengde/PycharmProjects/CAN201Lab3/xjtlu2.jpg', 'wb')
xjtlu2.write(modifiedImage)
xjtlu2.close()
clientSocket.close()

Reference

  1. XJTLU slides (Week3)
作者

Felix Chen

发布于

2021-09-23

更新于

2021-10-08

许可协议

评论