Mang-May-Tinh - Philip-Levis - L1-Intro-&-Socket-Programming - (Cuuduongthancong - Com)
Mang-May-Tinh - Philip-Levis - L1-Intro-&-Socket-Programming - (Cuuduongthancong - Com)
Networking
[email protected]
http://cs144.scs.stanford.edu/
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Networks class
• Goal: Teach the concepts underlying networks
- How do networks work? What can one do with them?
- Give you experience using and writing protocols
- Give you tools to understand new protocols & applications
- Not: train you on all the latest “hot” technologies
• Prerequisites:
- CS110 or equiv; class assumes good knowledge of C, some
socket programming helpful (e.g., CS110 web server)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Administrivia
• All assignments are on the web page
• Text: Kurose & Ross, Computer Networking: A
Top-Down Approach, 4th or 5th edition
- Instructors working from 4th edition, either OK
- Don’t need lab manual or Ethereal (used book OK)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Administrivia 2
• Send all assignment questions to newsgroup
- Someone else will often have the same question as you
- Newsgroup su.class.cs144 dedicated to class
- For information on accessing Usenet, see
http://www.stanford.edu/services/usenet/
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Grading
• Exams: Midterm & Final
• Homework
- 5 lab assignments implemented in C
• Grading
- Exam grade = max (final, (final + midterm)/2)
- Final grade will be computed as:
exam + lab
(1 − r) + r · max(exam, lab)
2
- r may vary per student, expect average to be ∼ 1/3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Labs
• Labs are due by the beginning of class
- Lab 1: Stop & wait
- Lab 2: Reliable transport
- Lab 3: Static routing
- Lab 4: NAT
- Lab 5: Dynamic routing
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Late Policy
• No credit for late assignments w/o extension
• Contact cs144-staff if you need an extension
- We are nice people, so don’t be afraid to ask
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Topics
• Network programming (sockets, RPC)
• Network (esp. Internet) architecture
- Switching, Routing, Congestion control, TCP/IP, Wireless
networks
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Networks
• What is a network?
- A system of lines/channels that interconnect
- E.g., railroad, highway, plumbing, communication,
telephone, computer
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Building blocks
• Nodes: Computers, dedicated routers, . . .
• Links: Coax, twisted pair, fibers, radio . . .
(a) point-to-point
(b) multiple access – every node sees every packet
(a)
…
(b)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
From Links to Networks
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Protocol layering
Application
TCP UDP
IP
Link Layer
CuuDuongThanCong.com https://fb.com/tailieudientucntt
OSI layers
End host End host
Application Application
Presentation Presentation
Session Session
Transport Transport
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Layers
• Physical – sends individual bits
• Data link – sends frames, handles access control to
shared media (e.g., coax)
• Network – delivers packets, using routing
• Transport – demultiplexes, provides reliability &
flow control
• Session – can tie together multiple streams (e.g.,
audio & video)
• Presentation – crypto, conversion between
representations
• Application – what end user gets, e.g., HTTP (web)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Addressing
• Each node typically has unique address
- (or at least is made to think it does when there is shortage)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Hourglass
FTP HTTP NV TFTP
TCP UDP
IP
…
NET1 NET2 NETn
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Internet protocol
• Most computer nets connected by Internet protocol
- Runs over a variety of physical networks, so can connect
Ethernet, Wireless, people behind modem lines, etc.
Channel
Host
Application
Host Host
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UDP and TCP
• UDP and TCP most popular protocols on IP
- Both use 16-bit port number as well as 32-bit IP address
- Applications bind a port & receive traffic to that port
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Uses of TCP
• Most applications use TCP
- Easier interface to program to (reliability, lecture 5)
- Automatically avoids congestion (don’t need to worry
about taking down network, lecture 6)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Programming Sockets
• Book has Java source code
• CS144 is in C
- Many books and internet tutorials
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Quick CS110 review: System calls
• System calls invoke code in the OS kernel
- Kernel runs in a more privileged mode than application
- Can execute special instructions that application cannot
- Can interact directly with devices such as network card
CuuDuongThanCong.com https://fb.com/tailieudientucntt
File descriptors
• Most IO done on file descriptors
- Small integers referencing per-process table in the kernel
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Error returns
• What if syscall failes? E.g. open non-existent file?
- Returns -1 (invalid fd number)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Sockets: Communication between machines
• Network sockets are file descriptors too
• Datagram sockets: Unreliable message delivery
- With IP, gives you UDP
- Send atomic messages, which may be reordered or lost
- Special system calls to read/write: send/recv,
sendto/recvfrom, and sendmsg/recvmsg (most general)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Socket naming
• Recall how TCP & UDP name communication
endpoints
- 32-bit IP address specifies machine
- 16-bit TCP/UDP port number demultiplexes within host
- Well-known services “listen” on standard ports: finger—79,
HTTP—80, mail—25, ssh—22
- Clients connect from arbitrary ports to well known ports
CuuDuongThanCong.com https://fb.com/tailieudientucntt
System calls for using TCP
Client Server
socket – make socket
bind – assign address
listen – listen for clients
socket – make socket
bind* – assign address
connect – connect to listening socket
accept – accept connection
*This call to bind is optional; connect can choose address & port.
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Socket address structures
• Socket interface supports multiple network types
• Most calls take a generic sockaddr:
struct sockaddr {
uint16_t sa_family; /* address family */
char sa_data[14]; /* protocol-specific address */
}; /* (may be longer than this) */
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Dealing with address types [RFC 3493]
• All values in network byte order (big endian)
- htonl converts 32-bit value from host to network order
- ntohl converts 32-bit value from network to host order
- ntohs/htons same for 16-bit values
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Looking up a socket address w. getaddrinfo
struct addrinfo hints, *ai;
int err;
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Address lookup details
• getaddrinfo notes:
- Can specify port as service name or number (e.g., "80" or
"http", allows possibility of dynamically looking up port)
- May return multiple addresses (chained with ai next field)
- You must free structure with freeaddrinfo
CuuDuongThanCong.com https://fb.com/tailieudientucntt
EOF in more detail
• Simple client-server application
- Client sends request
- Server reads request, sends response
- Client reads response
CuuDuongThanCong.com https://fb.com/tailieudientucntt
shutdown
• int shutdown (int fd, int how);
- Shuts down a socket w/o closing file descriptor
- how: 0 = reading, 1 = writing, 2 = both
- Note: Applies to socket, not descriptor—so copies of
descriptor (through dup or fork affected)
- Note 2: With TCP, can’t detect if other side shuts for reading
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Small request/reply protocol
Client Server
request
reply
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Large reply protocol
Client Server
request
reply
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Performance definitions
• Throughput – Number of bits/time you can sustain
at the receiver
- Improves with technology
• Goodput – TransferSize/Latency
• Jitter – Variation in latency
• What matters most for your application?
- We’ll look at network applications next week
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Today’s Lecture
• Basic networking abstractions
- Protocols
- OSI layers and the Internet Hourglass
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Structure of Rest of Class
• IP and above (5 weeks)
- Application layers
- Network layer: IP and routing, multicast
- Transport layer: TCP and congestion control
- Naming, address translation, and content distribution
• Below IP (2 weeks)
- Network address translation (NAT)
- Link and physical layers
CuuDuongThanCong.com https://fb.com/tailieudientucntt