overview of computer networks

Upload: ankita-anand

Post on 23-Feb-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 Overview of Computer Networks

    1/26

    Overview of Computer NetworksNorman Matloff

    Dept. of Computer Science

    University of California at Davis

    c

    2001-200! N. Matloff

    "pril 11! 200

    Contents1 Significance of Networks 3

    1.1 #istory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . $

    1.2 %&at "re Networks Use' (or) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . $

    1.$ %&ic& "spects of Networks "re *mportant to +now) . . . . . . . . . . . . . . . . . . . . . $

    2 An Introductory Example 3

    3 Te !amous" #$errated %ut &seful '()ayer *odel +

    $.1 Overview of t&e ,ayers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.1.1 &ysical ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.1.2 Data ,ink ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . $.1.$ Network ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.1./ ransport ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.1. Session ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.1. resentation ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.1. "pplication ,ayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $.2 #ow t&e ,ayers *nteract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    , *ore on TC-.I- /

    /.1 C* Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    1

    /.1.1 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    /.1.2 UD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    /.1.$ Stream 5s. Data6ram Communication . . . . . . . . . . . . . . . . . . . . . . . . . 10

    /.1./ * "''resses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    /.1. eer Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    /.1. 5iewin6 Current Socket Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    /.1. %&at Makes a Connection Uni7ue . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    /.2 Sample C* "pplication8 N(S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    + Network -rogramming 13

    .1 C Socket 9:ample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1$

    .1.1 Source Co'e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1/

    .1.2 %&o S&all * Say *s Callin6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    .2 UD Socket 9:amples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    .2.1 ;asic 9:ample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20.2.2 "'vance' Use of Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    .$ Non

  • 7/24/2019 Overview of Computer Networks

    2/26

    &e key to t&e computer revolution of t&e 1430s was t&e invention of t&e microprocessor in t&e 140s.

    &is

    6ave rise to t&e c&eap computers w&ic& &ave

  • 7/24/2019 Overview of Computer Networks

    3/26

    &e N*C also &as a ,"N a''ress! typically a /3-! ftp! ss an' so on.2"not&er similar term is intranet! w&ic& refers to a private collection of networks! say wit&in a particular company! w&ic&

    we

    'o not wis& to make accessi will

  • 7/24/2019 Overview of Computer Networks

    4/26

    an' it 'oes not always correspon' to specific portions of specific networks. Nevert&eless! it serves as

    a useful

    overview of t&e fiel'. #ere is &ow some of t&e layers relate to our sample network a

  • 7/24/2019 Overview of Computer Networks

    5/26

    Suppose saturnFs messa6e to olstein consists of a lar6e file transfer! say 100 me6a

  • 7/24/2019 Overview of Computer Networks

    6/26

    *nformation is communicate' from one layer to t&e ne:t.4 (or instance! t&ink of t&e file-transfer

    e:ample

    presente' earlier. &e file-transfer pro6ram! say ftp! works in t&e "pplication ,ayer. *t will call socket

    service functions in t&e Session ,ayer! suc& as t&e socket to t&e 9t&ernet N*C! w&ic& will use t&e Data ,ink an' &ysical

    ,ayers to

    put frames out onto t&e ,"N. "t any 6iven layer! a function passes t&e messa6e in a packet of

  • 7/24/2019 Overview of Computer Networks

    7/26

    C* is a very comple: system! t&e su is 'roppe'. UDFs virtue is t&at it is simple an' t&us

    &as

    very little over&ea'! compare' to C! w&ic& spen's a lot of time ne6otiatin6 an' maintainin6 a

    connection

  • 7/24/2019 Overview of Computer Networks

    8/26

    Similarly! suppose we are

  • 7/24/2019 Overview of Computer Networks

    9/26

    one lon6 stream! t&e runnin6 on t&e remote mac&ine.1

    (or t&e application pro6rammer! it woul'

  • 7/24/2019 Overview of Computer Networks

    10/26

    1$123 ! w&ic& in turn uses UD. GC is Bust w&at t&e name implies8 " pro6ram

    can

  • 7/24/2019 Overview of Computer Networks

    11/26

    actually call a functionAwit& parametersAw&ic& will e:ecute on anot&er mac&ine. %e &ave earlier referre' to UDFs

    ?unrelia! t&e function open! an' on t&e former mi6&t test wps as follows8toto% wps garnacha.engr.ucdavis.edu w

    1:01pm up 37 days, 17:54, 2 users, load average: 0.06, 0.00, 0.00

    User tty login@ idle JCPU PCPU what

    matloff ttyp1 12:58pm 1 4 1 script

    matloff ttyp2 1:00pm 1 2 1 w

    bslouie ttyp3 11:43am 1 42 13 tin-new

    toto%

    =Note t&at my name appeare' in t&e output. &is is Bust a coinci'ence! arisin6 from t&e fact t&at * &a'

    telnet-e' into garnaca from toto in or'er to start up s$r.>14Kou mi6&t not &ave use' t&ese

  • 7/24/2019 Overview of Computer Networks

    12/26

    5 w and ps commands.

    6

    7 User can check load on machine without logging in

    8 (or even without being able to log in).

    9

    10 Usage: wps remotehostname command (where "command"

    11 is either w or ps).

    12 */

    13

    14

    15 /* these are needed for socket calls */

    16 #include

    17 #include

    18 #include

    19 #include

    20 #include

    21

    22

    23 #define WPSPORT 2000 /* server port number */

    24 #define BUFSIZE 1000

    2526

    27 main(argc,argv)

    28 int argc; char **argv;

    29

    30 { int SD,MsgSize;

    31 struct sockaddr_in Addr;

    32 struct hostent *HostPtr,*gethostbyname();

    33 char Buf[BUFSIZE];

    34

    35 /* open a socket */

    36 SD = socket(AF_INET,SOCK_STREAM,0);

    40

    41 /* set up for an Internet connection to the host whose

    42 name was specified by the user on the command line */43 Addr.sin_family = AF_INET;

    44 Addr.sin_port = WPSPORT;

    45 /* get IP address of host */

    46 HostPtr = gethostbyname(argv[1]);

    47 memcpy(&Addr.sin_addr.s_addr,

    48 HostPtr->h_addr_list[0],HostPtr->h_length);

    49

    50 /* OK, now connect */

    51 connect(SD,&Addr,sizeof(Addr));

    55

    56 /* send user command */

    57 write(SD,argv[2],strlen(argv[2]));

    58

    59 /* display response */

    60 MsgSize = read(SD,Buf,BUFSIZE);

    1/61 write(1,Buf,MsgSize);

    62 }

    63

    1 /* WPsServer.c */

    2

    3 /* A server for remote versions of the w and ps

    4 commands.

    5

    6 User can check load on machine without logging

    7 in (or even without being able to log in).

    8 */9

    10

  • 7/24/2019 Overview of Computer Networks

    13/26

    11 /* these are needed for socket calls */

    12 #include

    13 #include

    14 #include

    15 #include

    16 #include

    17

    18

    19 /* this is needed for the disk read */

    20 #include

    21

    22

    23 #define WPSPORT 2000 /* server port number */

    24 #define BUFSIZE 1000

    25

    26

    27 int ClntDescriptor, /* socket descriptor to client */

    28 SrvrDescriptor; /* socket descriptor for server */

    29

    30

    31 char InBuf[BUFSIZE], /* messages from client */32 OutBuf[BUFSIZE]; /* messages to client */

    33

    34

    35 Write()

    36

    37 { int FD,NB;

    38

    39 FD = open("tmp.client",O_RDONLY);

    40 NB = read(FD,OutBuf,BUFSIZE);

    41 write(ClntDescriptor,OutBuf,NB);

    42 unlink("tmp.client");

    43 }

    44

    4546 Respond()

    47

    48 { memset(OutBuf,0,sizeof(OutBuf)); /* clear buffer */

    49 if (!strcmp(InBuf,"w"))

    50 system("w > tmp.client");

    51 else if (!strcmp(InBuf,"ps"))

    52 system("ps -ax > tmp.client");

    53 else

    54 system("echo invalid command > tmp.client");

    155 Write();

    56 }

    57

    58

    59 main(argc,argv)

    60 int argc; char **argv;

    61

    63 { struct sockaddr_in BindInfo;

    66

    67 /* create an Internet TCP socket */

    68 SrvrDescriptor = socket(AF_INET,SOCK_STREAM,0);

    73

    74 /* bind it to port 2000 (> 1023, to avoid the

    75 "well-known ports"), allowing connections from

    76 any NIC */

    77 BindInfo.sin_family = AF_INET;

    78 BindInfo.sin_port = WPSPORT;79 BindInfo.sin_addr.s_addr = INADDR_ANY;

    80 bind(SrvrDescriptor,&BindInfo,sizeof(BindInfo));

  • 7/24/2019 Overview of Computer Networks

    14/26

    86

    87 /* OK, set queue length for client calls */

    88 listen(SrvrDescriptor,5);

    91

    93 while (1) {

    94 /* wait for a call */

    95 ClntDescriptor = accept(SrvrDescriptor,0,0);

    106 /* read client command */

    107 memset(InBuf,0,sizeof(InBuf));

    108 read(ClntDescriptor,InBuf,sizeof(InBuf));

    113 /* process the command */

    114 Respond();

    115 }

    116 }

    117

    Note t&e Pinclu'e files. "lso! &ere are some ot&er notes on pro6rammin68

    On Solaris mac&ines! you may nee' to a'' (lsocket an' possi! Bust

    in case t&ere are still some packets to come in. &us a 6iven port will not

  • 7/24/2019 Overview of Computer Networks

    15/26

    %e are

  • 7/24/2019 Overview of Computer Networks

    16/26

    send

    Now letFs take a look at t&e server co'e.

    Line 6#:

    #ere t&e server creates a socket.

    Lines -#5:

    &e system function 5ind

  • 7/24/2019 Overview of Computer Networks

    17/26

    Normally we 'o not nee' to know w&ic& ep&emeral port &as actually su

  • 7/24/2019 Overview of Computer Networks

    18/26

    (ollowin6 are a pair of pro6rams w&ic& communicate usin6 UD sockets81/* BasicCln.c */2

    $/* introductory UDP example (client), with client sending a one-line

    /message to server at port 4000 */

    #include

    #include 3#include

    4#include

    10#include 11

    12main()1$

    1/{ struct hostent *h_name;

    1int sockfd;

    1char buf[10];

    1struct sockaddr_in your;13

    14your.sin_family = AF_INET;

    20your.sin_port = htons(4000);

    21h_name = gethostbyname("sgi8.cs.ucdavis.edu");

    22your.sin_addr.s_addr = *(u_long *) h_name->h_addr_list[0];2$

    2/if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) {

    2printf("socket error\n");

    2exit(1);

    2}23

    24strcpy(buf,"OK");$0

    $1if (sendto(sockfd,buf,strlen(buf),0,&your,sizeof(your)) < strlen(buf)) {

    $2printf("send error\n");

    $$exit(1);

    $/}

    $}

    1/* BasicSrv.c */2

    $/* introductory UDP example (server), with client sending a one-line

    /message to server at port 4000 */

    #include

    #include

    3#include

    204#include

    10#include 11

    12main()1$

    1/{ struct hostent *h_name;

    1int sockfd;

    1char buf[10];

    1struct sockaddr_in mine;

    13/* these two are needed as placeholders but are not used here: */

    14struct sockaddr_in rcvaddr;

    20int addlen;21

    22mine.sin_family = AF_INET;

    2$mine.sin_port = htons(4000);

    2/mine.sin_addr.s_addr = INADDR_ANY;2

    2if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) {

    2printf("socket error\n");

    23exit(1);

    24}

    $0$1if (bind(sockfd,(struct sockaddr *)&mine,sizeof(mine)) < 0) {

    $2close(sockfd);

  • 7/24/2019 Overview of Computer Networks

    19/26

    $$printf("bind error\n");

    $/exit(1);

    $}$

    $recvfrom(sockfd,buf,sizeof(buf),0,&rcvaddr,&addlen);

    $3printf("%s\n",buf);

    $4}

    %e &ave use' t&e tons! it 'oes a call to

    sendto81/* BCastCln.c */2

    $/* introductory UDP example (client), with client sending a one-line

    /message to all servers on the given network (see below), at port 4000

    the server is still BasicSrv.c, no change */

    3#include

    4#include

    10#include

    11#include

    12#include 1$

    1/main()1

    1{ struct hostent *h_name;

    1int sockfd;

    13char buf[10];

    14struct sockaddr_in your;20

    21const int turn_option_on = 1;22

    2$your.sin_family = AF_INET;

    2/your.sin_port = htons(4000);

    2/* UCD CS and Engrg. machines are on the network 128.120.*.*,

    2i.e. hex IP addresses 0x8078zzzz, so the broadcast address

  • 7/24/2019 Overview of Computer Networks

    20/26

    2replaces zzzz by ffff (after this program was written,

    23the addresses became 169.237.*.*; in general, run

    24/sbin/ifconfig to determine broadcast address */

    $0your.sin_addr.s_addr = 0x8078ffff;$1

    $2if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) {

    $$printf("socket error\n");

    $/exit(1);$}$

    $setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&turn_option_on,

    $3sizeof(turn_option_on));$4

    /0strcpy(buf,"OK");/1

    /2if (sendto(sockfd,buf,strlen(buf),0,&your,sizeof(your)) < strlen(buf)) {

    /$printf("send error\n");

    //exit(1);

    /}

    /}

    "not&er e:ample of t&e a'vance' use of sockets is raw sockets. #ere one can

  • 7/24/2019 Overview of Computer Networks

    21/26

    telnet pc8.cs.ucdavis.edu 1088

    *f we 6et a response &ere

    *n 6eneral! 'e

  • 7/24/2019 Overview of Computer Networks

    22/26

    02 I-%yte B@ &e first four .

    03 Eternet-ream5le@ Start-of-frame in'icator! a special /-

  • 7/24/2019 Overview of Computer Networks

    23/26

    2

    So! t&e effect of t&e write fiel'. "fter creatin6 t&is packet! C at onda will pass it to * in t&e Network ,ayer

    of

    onda! alon6 wit& t&e information t&at t&e 'estination will

    &e Data ,ink ,ayer will t&en create an 9t&ernet frame. *t will put 0:/12$/34a< for t&eDestination

    9t&ernet *D! an' 0:12$/34a< for t&e Source 9t&ernet *D. *t will fill in 0:0300 for t&e ype fiel'.

    "n'

    it will put in t&e entire * packet it receive' from t&e Network ,ayer in for t&e current Data fiel'.

    (inally!

    t&e Data ,ink ,ayer will pass t&is 9t&ernet frame to t&e 9t&ernet 'evice 'river on onda.

    &e 9t&ernet 'evice 'river on onda will t&en put t&e frame on network ;. "ll N*Cs on t&at network

    will

    see it! inclu'in6 t&e N*C 0:/12$/34a< on;eep.

    &at N*C will say! ?O&! t&is frame is for meI@ Note t&at t&e N*C will not notice t&at t&e ultimate

    'estination

    of t&e frame is $enus all t&e N*C cares a

  • 7/24/2019 Overview of Computer Networks

    24/26

    own. "ll t&e N*C will 'o is pass t&is frame up to t&e ne:t layer at ;eep!2 w&ic& will

    &e frame will t&en 6o up t&e protocol stack at $enus like it 'i' at;eep! . Suppose you are lma5ccom

    an'

    are sen'in6 e-mail to u$wxy:org! an' your messa6e is 6oin6 to

  • 7/24/2019 Overview of Computer Networks

    25/26

    1HELO abc.com

    2MAIL FROM: [email protected]

    $RCPT TO: [email protected]

    /DATA

    Hi, how have you been?

    .

    QUIT

    =&e en' of t&e e-mail messa6e itself is in'icate' &e mac&ine woul' respon'

  • 7/24/2019 Overview of Computer Networks

    26/26

    %in'ows networks use MicrosoftFs own name lookup system! %*NS.> ;riefly! your OS will &ave one

    or

    more DNS mac&ines w&ic& it can 7uery to convert t&e ?9n6lis&@ name! say wwwyaoocom to an *

    num