computer networks
DESCRIPTION
COMPUTER NETWORKS. 谭晓阳 CS dept. SYLLABUS. 教材: 谢希仁 计算机网络 ( 第三版 ) 大连理工大学出版社 参考书 : 1. 计算机网络, A.S. 坦尼伯姆 清华大学出版社 2. 计算机网络与 Internet E.Comer ( 第三版) 清华大学出版社. SYLLABUS. 谭晓阳 计算机网络研究室 10号楼二楼 Email:[email protected] [email protected] Tel:4892803 4802150( 晚8点以后). - PowerPoint PPT PresentationTRANSCRIPT
COMPUTERNETWORKS
谭晓阳CS dept.
23/4/222
SYLLABUS
教材:谢希仁 计算机网络 (第三版 ) 大连理工大学出版社参考书 :
– 1. 计算机网络, A.S. 坦尼伯姆 清华大学出版社– 2. 计算机网络与 Internet E.Comer (第三版) 清华大学出版社
23/4/223
SYLLABUS
• 谭晓阳• 计算机网络研究室 10 号楼二楼• Email : [email protected] [email protected]• Tel : 4892803 4802150( 晚 8 点以后 )
Part I
Introduction
“The kingdom of God is like a net…” --Matthew 13:47
23/4/225
课程内容 :
系统介绍计算机网络的基本概念和原理:从底层的硬件连接、数据传输到高层的网络应用等各个方面的内容。
23/4/226
将学习… 网络术语 通信基础
– 通信媒介及信号– 同步和非同步通信– 带宽( bandwidth )、吞吐量( throughput )、噪声( noise )及其关系– 频分和时分多路复用
23/4/227
将学习… (continued)
联网技术– Packet switching– Framing, parity, and error detection– Local and wide area technologies– Network addressing– Connection and extension (repeaters, bridges, hubs,
switches)– Topologies and wiring (star, ring, bus)– Next-hop forwarding– Shortest path computation– Measures of delay and throughput– Protocol layers
23/4/228
将学习… (continued)
网间互连技术– Motivation and concept– Internet Protocol (IP) datagram format and
addressing – Internet routers and routing– Address binding (ARP)– Internet control messages (ICMP)– User Datagram Protocol (UDP)– Transmission Control Protocol (TCP)– Protocol ports and demultiplexing
23/4/229
将学习… (continued)
网络应用– Client-server paradigm– Domain name system (DNS)– File transfer (FTP)– Remote login (TELNET)– Email transfer (SMTP)– Web technologies and protocols
HTTP, CGI, Java
23/4/2210
不涉及 商业相关… .
– Products– Vendors– Prices– Network operating systems
如何采购、配置、操作网络 如何设计、实现协议软件
23/4/2211
Background Required
C 编程能力 (subset of C++) 操作系统基本知识
– Terminology– Functionality– Processes and concurrent processing
Desire to learn : ACTIVE LEARNING= THINKING + PRACTISING
23/4/2212
课程内容安排 Signals, media, bandwidth, throughput, and
multiplexing (~2.5 weeks) Packet transmission: concepts, technologies
(~4.5 weeks) Internetworking fundamentals (~2.5 weeks) Internet applications (~4.5 weeks)
23/4/2213
●1. 网络的前世今生-什么来头? ●2. 网络的定义-到底什么是网络? ●3. 联网的动机-为何要联网? ●4. 网络解剖-网络如何构成的? ●5. 幕后-网络为我们做了什么? ●6. 驱动网络-网络编程,困难吗?
第一章 引论
23/4/2214
单机 大、中、小型机—庞大,昂贵 缺点——资源无法共享网络 始于 50’s ,近 20 年发展迅速。发展的动力:
–资源共享的需求–大型项目的合作–人与人之间的沟通
从体系结构来观察,网络的发展可分为三个时代: ◆联机终端系统 ◆以通信子网为中心 ◆采用标准化的层次体系结构
1.1 计算机网络的发展
23/4/2215
1. 以主机为中心的联机终端网络系统 20 世纪 60’s 以前。。。 特征——共享主机资源
– 单台主机——计算、通信– 多台终端——用户交互– 本地、远程连接
结构
Ö÷»ú
ÖÕ¶Ë ÖÕ¶Ë
ÖÕ¶Ë
ÖÕ¶Ë ÖÕ¶Ë
ͼ1-1 ÒÔµ¥¸öÖ÷»úΪÖÐÐÄ
23/4/2216
美国的飞机订票系统 SABRE-1 1 HOST 2000 Terminal 通信线路(电话线路)
– 此结构的网络至今仍在使用 缺点
– 主机负荷重,数据处理+通信– 线路利用率低
改进– 多点通信,近距– 终端集中器,近 / 远距– 前端处理机——通信任务分离
Ö÷»ú
ÖÕ¶Ë ÖÕ¶Ë
ÖÕ¶Ë
ÖÕ¶Ë ÖÕ¶Ë
ͼ1-1 ÒÔµ¥¸öÖ÷»úΪÖÐÐÄEXAMPLE
23/4/2217
20 世纪 60’s –20 世纪 70’s 特征
– 单主机终端网络的互联,形成多主机为中心的网络– 网络结构从“主机-终端” 转变为“主机-主机”
结构
2. 主机-主机网络
HOST
HOSTHOSTT
TT
T
T
T
T
T T T
通信线路
23/4/2218
演变阶段 1– 通信任务从主机中分离– CCP(Communication Control Processor)
通信控制处理机 专门处理主机之间的通信任务
CCP
CCP
HOST
HOST
T
T
TT
T
T
CCP
HOST
T T
主机-主机网络的演变
23/4/2219
两层网络概念的出现– 1. 由 CCP 组成的传输网络——通信子网,为资源子网提供信息传输服务
ͨÐÅ ×Ó Íø
Ö÷»ú
Ö÷»úÖ÷»ú
Ö÷»ú
ÖÕ¶Ë
ÖÕ¶Ë
ͼ1-2 ÒÔͨÐÅ×ÓÍøΪÖÐÐÄ
23/4/2220
C
CCH H
H
T T
T
T
TT
T
资源子网通信子网
T T
– 2. 主机的集合——资源子网,提供各种网络资源,建立在通信子网基础上(可多系统并存)
23/4/2221
通信子网规模扩大,私有→社会公用 公用数据通信网
– PSTN– X.25
优点– 降低用户系统建设成本– 通信线路利用率高– 兼容性好
公用数据通信网HOST
HOST
T
T
TT
T
T
HOST
T T
TT
演变阶段 2
23/4/2222
因特网的前身—— ARPANET 美国军方建立的实验性网络最初 4 个节点→ 70’s 的 60 多个节点 地域跨越美洲、欧洲具有现代网络的许多特征,例如
– 分组交换– 分层次的网络协议
第二代计算机网络的不足之处– 网络普及程度低– 标准不统一– 网络体系结构的研究不成熟
Example
23/4/2223
20 世纪 70’s- 至今 不同网络设备之间的兼容性和互操作性是推动网络体系结构的标准化的原动力 各厂商、研究机构、大学在网络技术、方法、理论等方面的研究日趋成熟是其基础 标准化过程的两个阶段
– 厂商标准: IBM-SNA,DEC-DNA 缺点:适用范围:兼容性?
技术垄断:竞争? 标准不统一:用户利益?
标准制定问题– 标准化任务只能由不偏向于任何厂商的非盈利中立组织来制定– 例外——“事实上的标准”,如 TCP/IP
3. 体系结构标准化网络
23/4/2224
– 国际标准: ISO-OSI/RM Open System Interconnection/ Recommended Model(开放系统互联参考模型,简称 OSI参考模型) OSI参考模型是一种概念上的网络模型 其标准保证了不同网络设备之间的兼容性和互操作性规定了网络体系结构的框架只说明了做什麼( WHAT TO DO )而未规定怎样做( HOW TO
DO ) 现在的计算机网络均是在 OSI/RM 的框架下运作的
– PC导致了局域网的出现• 局域网的标准: IEEE802• IEEE802也符合 OSI/RM 标准• 开始就建立在标准化的基础上
国际标准阶段
23/4/2225
计算机网络的定义– 以相互共享资源方式连接起来,且各自具有独立功能的计算机系统的集合;利用各种通信手段,把地理分散的计算机互联起来,互相通信且共享资源的系统。– 归纳:自主计算机的互连集合
– 自主:不是主从关系– 主控-从属– 主机-终端(终端网络)
– 互连:以任何通信方式– 铜线、光纤– 红外、激光、微波、卫星
1.2 计算机网络的概念
23/4/2226
类似系统– 终端分时系统– 多机系统– 分布式系统 (distributed System)
计算机网络与终端分时系统从设备在系统中的地位进行比较– 分时系统
主机以分时方式为终端服务 终端完全依赖于主机 主机与终端是支配与被支配的关系,数据处理-用户交互 资源高度集中,由许多用户共享 终端之间无联系
– 计算机网络 每台计算机地位平等,互相共享 每台计算机都能独立工作 资源分布
与类似系统的区别 (1)
23/4/2227
计算机网络与多机系统从耦合度和用途方面进行比较– 多机系统
设备之间耦合度紧 设备分布近 传输速度极快 主要用途为科学计算
– 计算机网络 网络设备之间耦合度松 设备分布相对较远 传输速度快 主要用途为资源共享
与类似系统的区别 (2)
23/4/2228
计算机网络与分布式系统从协调性和用途两方面进行比较 分布式系统
– 在分布式 OS 统一调度下,各计算机协调工作,共同完成一项任务。如并行计算。用户面对的是一台逻辑上的计算机,组成分布式系统的各计算机怎样协同工作,对用户透明。– 用途主要是科学计算和数据处理
计算机网络 – 非协调性的,各计算机对用户非透明,用户必须指定资源的位置。用户必须明确地指定在哪一台机器上登录。 – 用途主要是资源共享
从效果上讲,分布式系统是建立于网络之上的软件系统。它具有高度的整体性和透明性。因此,网络和分布式系统的区别更多地取决于软件(尤其是操作系统)而不是硬件。
与类似系统的区别 (3)
23/4/2229
1.3 联网的动机是什么??访问信息 应用程序之间交互协同工作 资源共享 (“摆脱地理位置的束缚” )
23/4/2230
实际上你看到的 ...
Email File transfer / access Web browsing Remote login / execution The Internet
23/4/2231
1.4 网络的构成 传输硬件 是完成特定功能的硬件设备,包括:
– 互连的传输媒介– 传输控制设备– 运行协议软件
协议软件,包括:– Encodes and formats data– Detects and corrects problems
23/4/2232
1.5 幕后: What a Network Does
1. 提供:– 可靠的、– 公正的、– 有效的、– 从应用到应用的通信服务。
23/4/2233
幕后: What a Network Does (continued)
2.自动检测和纠正:– Data corruption– Data loss– Duplication– Out-of-order delivery
3.自动发现从源到目的的最优路径。
23/4/2234
1.7 网络编程 任意两个应用程序之间的通信: Network 使之成为可能。 程序员不必理解网络技术。只需利用 Application Program Interface 来访问网络提供的服务即可。
23/4/2235
复习 : C Programming
Subset of C++直接操作 bits, bytes, words, pointers Source files use .c file extension
23/4/2236
输出格式化int printf (const char *control_string, …);
Writes data to the console Returns the number of characters written control_string consists of two types of items
– characters that will be printed on the screen– format specifiers that define the way the subsequent
arguments are displayed
23/4/2237
Examples
printf (“I like %c %s”, ‘C’, “very much!”);
displays“I like C very much!”
printf (“I have %d CS classes.”, 3 );
displays“I have 3 CS classes.
23/4/2238
3 个重要的格式%c character
%d signed decimal integer
%f decimal floating point
%s string of characters
23/4/2239
主函数 main() 的参数 从命令行接收参数 argc and argv receive command line arguments
int main (int argc, char *argv[] ){…
argc :参数个数 Argv :指向参数的指针数组
23/4/2240
Example: Name
int main(int argc, char *argv[]){if (argc != 2){printf(“You must type a name.\n”);exit(1);}printf(“Hello %s”, argv[1]);return 0;
}
23/4/2241
位域 是一种特殊的结构成员,定义域的位数struct struct-type-name{
type name1:length;type name1:length;…
};
23/4/2242
Example: Status Byte
struct status_type{
unsigned delta_cts: 1;unsigned delta_dsr: 1;unsigned tr_edge: 1;unsigned delta_rec: 1;unsigned cts: 1;unsigned dsr: 1;unsigned ring: 1;unsigned rec_line: 1;
} status;
23/4/2243
Example: Status Byte
status = get_port_status();if (status.cts) printf(“clear to send”);if (status.dsr) printf(“data ready”);status.ring = 0;
23/4/2244
网络通信的基本模型 建立联系。 交换数据(双向) 结束。
23/4/2245
Establishing Contact
由两个应用程序之间执行一个应用程序等待连接请求 (called: 服务器 )另一个应用程序发出连接请求 (called:客户端 )
23/4/2246
识别一个等待的应用程序(服务器) 从概念上说,需要指出
– 哪个计算机– 该计算机上的哪个应用软件
术语– 通过域名识别计算机– 通过程序名称识别应用程序
23/4/2247
表示和翻译 人类使用名称来表示:
– computer: txy.cs.nuaa.edu– application: ftp
而网络协议使用二进制值 需要库程序把名字翻译成数字
23/4/2248
API 举例
23/4/2249
应用程序交互
23/4/2250
API 中使用的数据类型
23/4/2251
函数原型
23/4/2252
Example #1: Echo
用于网络测试 服务器返回发送数据的精确备份 在某台计算机 X 上的用户运行
echoserver 22000 在另一台计算机上的用户运行
echoclient X 22000
23/4/2253
Example #2: Chat
因特网聊天服务的微缩模型允许两个用户之间交流 在计算机 X 上的用户运行
chatserver 25000 在另一台计算机上的用户运行
chatclient X 25000
23/4/2254
Example #3: Web Server
在计算机 X 上的用户运行webserver 27000
在另一台计算机上的用户运行浏览器并输入 URL:
http://X:27000/index.html
23/4/2255
Echoserver
运行在因特网上 使用 API请求,而非通常的 I/O不需要了解网络知识
23/4/2256
Echoserver/* echoserver.c */
#include <stdlib.h>#include <stdio.h>#include <cnaiapi.h>
#define BUFFSIZE 256
/*----------------------------------------------------------------------- * * Program: echoserver * Purpose: wait for a connection from an echoclient and echo data * Usage: echoserver <appnum> * *----------------------------------------------------------------------- */intmain(int argc, char *argv[]){
23/4/2257
connection conn; int len; char buff[BUFFSIZE];
if (argc != 2) { (void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]); exit(1); }
/* wait for a connection from an echo client */
conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1);
/* iterate, echoing all data received until end of file */
while((len = recv(conn, buff, BUFFSIZE, 0)) > 0) (void) send(conn, buff, len, 0); send_eof(conn); return 0;}
23/4/2258
Echoclient/* echoclient.c */
#include <stdlib.h>#include <stdio.h>#include <cnaiapi.h>
#define BUFFSIZE 256#define INPUT_PROMPT "Input > "#define RECEIVED_PROMPT "Received> "
int readln(char *, int);
/*----------------------------------------------------------------------- * Program: echoclient * Purpose: contact echoserver, send user input and print server response * Usage: echoclient <compname> [appnum] * Note: Appnum is optional. If not specified the standard echo appnum * (7) is used. *----------------------------------------------------------------------- */
23/4/2259
intmain(int argc, char *argv[]){ computer comp; appnum app; connection conn; char buff[BUFFSIZE]; int expect, received, len;
if (argc < 2 || argc > 3) { (void) fprintf(stderr, "usage: %s <compname> [appnum]\n", argv[0]); exit(1); }
23/4/2260
/* convert the arguments to binary format comp and appnum */
comp = cname_to_comp(argv[1]); if (comp == -1) exit(1);
if (argc == 3) app = (appnum) atoi(argv[2]); else if ((app = appname_to_appnum("echo")) == -1) exit(1); /* form a connection with the echoserver */
conn = make_contact(comp, app); if (conn < 0) exit(1);
(void) printf(INPUT_PROMPT); (void) fflush(stdout);
23/4/2261
/* iterate: read input from the user, send to the server, */ /* receive reply from the server, and display for user */
while((len = readln(buff, BUFFSIZE)) > 0) {
/* send the input to the echoserver */
(void) send(conn, buff, len, 0); (void) printf(RECEIVED_PROMPT); (void) fflush(stdout);
/* read and print same no. of bytes from echo server */
expect = len; for (received = 0; received < expect;) { len = recv(conn, buff, (expect - received) < BUFFSIZE ? (expect - received) : BUFFSIZE, 0);
23/4/2262
Echoclient
if (len < 0) { send_eof(conn); return 1; } (void) write(STDOUT_FILENO, buff, len); received += len; } (void) printf("\n"); (void) printf(INPUT_PROMPT); (void) fflush(stdout); }
/* iteration ends when EOF found on stdin */
(void) send_eof(conn); (void) printf("\n"); return 0;}
23/4/2263
Webserver/* webserver.c */
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <cnaiapi.h>
#if defined(LINUX) || defined(SOLARIS)#include <sys/time.h>#endif
#define BUFFSIZE 256#define SERVER_NAME "CNAI Demo Web Server"
#define ERROR_400 "<head></head><body><html><h1>Error 400</h1><p>Th\e server couldn't understand your request.</html></body>\n"
23/4/2264
#define ERROR_404 "<head></head><body><html><h1>Error 404</h1><p>Do\cument not found.</html></body>\n"
#define HOME_PAGE "<head></head><body><html><h1>Welcome to the CNAI\ Demo Server</h1><p>Why not visit: <ul><li><a href=\"http://netbook.cs.pu\rdue.edu\">Netbook Home Page</a><li><a href=\"http://www.comerbooks.com\"\>Comer Books Home Page<a></ul></html></body>\n"
#define TIME_PAGE "<head></head><body><html><h1>The current date is\: %s</h1></html></body>\n"
int recvln(connection, char *, int);void send_head(connection, int, int);
/*----------------------------------------------------------------------- * Program: webserver * Purpose: serve hard-coded webpages to web clients * Usage: webserver <appnum> * *----------------------------------------------------------------------- */
23/4/2265
intmain(int argc, char *argv[]){
connection conn; int n; char buff[BUFFSIZE], cmd[16], path[64], vers[16]; char *timestr;#if defined(LINUX) || defined(SOLARIS) struct timeval tv;#elif defined(WIN32) time_t tv;#endif
if (argc != 2) { (void) fprintf(stderr, "usage: %s <appnum>\n",
argv[0]); exit(1); }
23/4/2266
while(1) {
/* wait for contact from a client on specified appnum */
conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1);
/* read and parse the request line */
n = recvln(conn, buff, BUFFSIZE); sscanf(buff, "%s %s %s", cmd, path, vers);
/* skip all headers - read until we get \r\n alone */
while((n = recvln(conn, buff, BUFFSIZE)) > 0) { if (n == 2 && buff[0] == '\r' && buff[1] == '\n') break; }
23/4/2267
/* check for unexpected end of file */
if (n < 1) { (void) send_eof(conn); continue; } /* check for a request that we cannot understand */
if (strcmp(cmd, "GET") || (strcmp(vers, "HTTP/1.0") && strcmp(vers, "HTTP/1.1"))) { send_head(conn, 400, strlen(ERROR_400)); (void) send(conn, ERROR_400,
strlen(ERROR_400),0); (void) send_eof(conn); continue; }
23/4/2268
/* send the requested web page or a "not found" error */
if (strcmp(path, "/") == 0) { send_head(conn, 200, strlen(HOME_PAGE)); (void) send(conn, HOME_PAGE, strlen(HOME_PAGE),0); } else if (strcmp(path, "/time") == 0) {#if defined(LINUX) || defined(SOLARIS) gettimeofday(&tv, NULL); timestr = ctime(&tv.tv_sec);#elif defined(WIN32) time(&tv); timestr = ctime(&tv);#endif (void) sprintf(buff, TIME_PAGE, timestr); send_head(conn, 200, strlen(buff)); (void) send(conn, buff, strlen(buff), 0); } else { /* not found */ send_head(conn, 404, strlen(ERROR_404)); (void) send(conn, ERROR_404, strlen(ERROR_404),0); } (void) send_eof(conn); }}
23/4/2269
/*----------------------------------------------------------------------- * send_head - send an HTTP 1.0 header with given status and content-len *----------------------------------------------------------------------- */voidsend_head(connection conn, int stat, int len){ char *statstr, buff[BUFFSIZE];
/* convert the status code to a string */
switch(stat) { case 200: statstr = "OK"; break; case 400: statstr = "Bad Request"; break; case 404: statstr = "Not Found"; break;
23/4/2270
default: statstr = "Unknown"; break; } /* * send an HTTP/1.0 response with Server, Content-Length, * and Content-Type headers. */
(void) sprintf(buff, "HTTP/1.0 %d %s\r\n", stat, statstr); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, "Server: %s\r\n", SERVER_NAME); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, "Content-Length: %d\r\n", len); (void) send(conn, buff, strlen(buff), 0);
23/4/2271
(void) sprintf(buff, "Content-Type: text/html\r\n"); (void) send(conn, buff, strlen(buff), 0);
(void) sprintf(buff, "\r\n"); (void) send(conn, buff, strlen(buff), 0);}
23/4/2272
Example Code Using API: Webserver
23/4/2273
Webserver (2 of 6)
23/4/2274
Webserver (3 of 6)
23/4/2275
Webserver (4 of 6)
23/4/2276
Webserver (5 of 6)
23/4/2277
Webserver (6 of 6)
23/4/2278
Summary
学习网络知识非常重要– 世界是互连的– 应用程序在分布的环境中工作
本课程– 解释神秘的网络幕后工作原理– Will be hard work
23/4/2279
Summary (continued)
计算机网络的三个任务– 将数据从源 deliver 到目标– 自动寻找优化路径– 处理 deliver过程中发生的错误
我们将学习这三大“不可能完成的任务”是如何完成的。