Week10 Sockets

Embed Size (px)

Citation preview

  • 8/8/2019 Week10 Sockets

    1/44

    1

    Communication

    Sockets (Haviland Ch. 10)

  • 8/8/2019 Week10 Sockets

    2/44

    2

    Simple Web Request

    Karens Home Page

    -Courses-kites

  • 8/8/2019 Week10 Sockets

    3/44

    3

    The request

    How do we tell the server what wewant?

    How do we even find the server? How do the server and browser talk to

    each other?

  • 8/8/2019 Week10 Sockets

    4/44

  • 8/8/2019 Week10 Sockets

    5/44

    5

    How do we find the server?

    Every computer on the Internet has anInternet address.

    Called an IP address (Internet Protocol) An IP address is 4 numbers separated

    by dots.

    www.tkf.toronto.on.ca = 207.245.2.3

  • 8/8/2019 Week10 Sockets

    6/44

    6

    Domain Name Serversbrowser

    localname server

    rootname server ca

    name server

    onname server

    torontoname server

    www.tkf.

    toronto.on.ca?

    caserver

    onserver

    torontoserver

    207.245.2.3

    207.245.2.3

    www.tkf.toronto.on.ca?

    www.tkf.toronto.on.ca?

    www.tkf.toronto.on.ca?

    www.tkf.toronto.on.ca?

  • 8/8/2019 Week10 Sockets

    7/44

    7

    This is getting complicated!

    browser

    localname server

    rootname server

    caname server

    onname server

    torontoname server

    server

    Number of

    messages?

    10-12

  • 8/8/2019 Week10 Sockets

    8/44

    8

    Actually, its worse than that

    The web page for www.tkf.toronto.on.ca doesntreally live at 207.245.2.3

    So we need to go look up www.kites.org

    GET / HTTP/1.1Connection: Keep-AliveHost:www.tkf.toronto.on.ca

    request

    HTTP/1.1 301 Moved PermanentlyDate: Tues, 23 July 2002Server: Apache/1.3.22(unix)Location: http://www.kites.orgContent-Type: text/html

    reply

  • 8/8/2019 Week10 Sockets

    9/44

    9

    Now what?

    Okay, we have the address. What do we do with it? Lets look at how two computers

    communicate.

    HTTP is a high-level protocol HTTP is specific to the web. Computers communicate for other

    reasons.

  • 8/8/2019 Week10 Sockets

    10/44

    10

    Protocols

    Computers use several layers ofgeneral protocols to communicate.

    To understand why these layers areimportant, think about how a company

    sends you an invoice for a purchase.

  • 8/8/2019 Week10 Sockets

    11/44

  • 8/8/2019 Week10 Sockets

    12/44

    12

    TCP/IP

    Transmission Control Protocol.Tells us how to package up the data.

    source address dest. address

    bytes ack port

    data

  • 8/8/2019 Week10 Sockets

    13/44

    13

    3-way

    handshakeSYN

    Heres some data

    Got it

    TCP Connection

    Hi 24.197.0.67Connection port 80? Hi 128.100.27.9

    Lets talk

    okay

    Send me a file

    Heres some more

    Got itIm done

    Im done too

    ack

    ack fin

    fin

  • 8/8/2019 Week10 Sockets

    14/44

    14

    TCP: Three-way handshakeclient server

    socket

    connect

    (blocks)

    connect

    returns

    socket,bind,listen

    accept(blocks)

    accept returns

    SYN J

    SYN K, ack J+1

    ack K+1

    sequence

    number = J

    sequence

    number = K

  • 8/8/2019 Week10 Sockets

    15/44

    15

    read()

    write()

    close()

    socket()

    connect()

    write()

    read()

    close()

    block until connection

    from client

    Connection establishment

    (3-way handshake)

    data transfer

    end-of-file notification

    socket()

    bind()

    listen()

    accept()

    TCP ServerTCP Client

  • 8/8/2019 Week10 Sockets

    16/44

    16

    Details011001110010010010001000111110100010111

    101010001111010101100110010110101111001011011

    101010001111010101100110010110101111001011011

    101010001111010101100110010110101111001011011

    101010001111010101100110010110101111001011011

    IP

    TCP

    IP IP IP

    To24.197.0.67

    To24.197.0.67

    To24.197.0.67

    To24.197.0.67

    makepackets

    Each TCP packet is given a header-sequence number-checksum

    put in anIP envelopewith anotherheader

  • 8/8/2019 Week10 Sockets

    17/44

    17

    When something goes wrong

    A packet might not arrivetraffic overloadbit corruption

    Receiver asks for missing packets to beresent.

    Want to send data as fast as possible. But sending too fast wastes resources.

  • 8/8/2019 Week10 Sockets

    18/44

    18

    TCP Congestion Control Window-based:

    some number of packets allowed to besent and not ackd

    as successful acks arrive, grow windowif packet loss is detected, cut window size

    window

    ackd sent but not ackd

  • 8/8/2019 Week10 Sockets

    19/44

    19

    TCP Congestion Control

    Time

    Wind

    ow

    siz

    e

    packet losses

  • 8/8/2019 Week10 Sockets

    20/44

    20

    The Big Picture

    Client-Server model: a client process wants to talk to aserver process

    Client must find server - DNS lookup Client must find process on server - ports Finally establish a connection so two processes can talk

    Client

    Process

    Server

    Process

    network

  • 8/8/2019 Week10 Sockets

    21/44

    21

    Routing

    browser

    www.tkf.toronto.on.ca

    sf-cs27.gw.

    utoronto.ca

    mcl-gpb.gw.utoronto.ca

    fe6-3.gwy2-tor.bb.attcanada.ca

    pos10-1.peera-toroon.ip.

    grouptelecom.net

    border.onet.on.ca

    gate-gateway.gw.utoronto.ca

    srp2-0.core2-tor.bb.attcanada.ca

    pos8-0-0.hcap2-tor.bb.attcanada.ca

    atm1-0-101.adsl1-tor.bb.attcanada.ca

    206.222.73.174

  • 8/8/2019 Week10 Sockets

    22/44

    22

    Plus the redirect (20 hops)

    browser

    gar3-p370.attga.ip.att.net

    gbr6-p40.attga.ip.att.net

    www.kites.org

    sf-cs27.gw.utoronto.ca

    mcl-gpb.gw.utoronto.ca

    if-10-0.core1.Scarborough.Teleglobe.net

    if-3-2.core1.Toronto2.Teleglobe.net

    border.onet.on.ca

    gate-gateway.gw.utoronto.ca

    ??

    if-7-0.core1.Chicago3.Teleglobe.net

    ??

    bed-va-0-105.cablenet-va.com

    ??

    12.122.10.29

    tbr1-p012201.

    n54ny.ip.att.net

    tbr2-p013601.

    wswdc.ip.att.net

    12.122.10.74

    12.124.65.22

    if-7-0.core1.Chicago3.Teleglobe.net

  • 8/8/2019 Week10 Sockets

    23/44

    23

    At least 5 different cities

    my office St. LouisMissouri

    CSLab

    U of T

    Scarborough.ON

    Toronto,ON

    Toronto,ON

    U of T

    ??

    Chicago

    ??

    St. LouisMissouri

    ??

    ??

    Washington,

    DC

    Washington,

    DC

    ??

    ??

    Atlanta,Georgia

    Atlanta,Georgia

    Chicago

  • 8/8/2019 Week10 Sockets

    24/44

    24

    Putting it together

    browser

    local

    name server

    rootname serverca

    name server

    onname server

    torontoname server

    localname server

    rootname server

    orgname server

    www.tkf.toronto.on.ca

    sf-cs27.gw.

    utoronto.ca

    mcl-gpb.gw.

    utoronto.ca

    fe6-3.gwy2-tor.

    bb.attcanada.ca

    pos10-1.peera-

    toroon.ip.grouptelecom.net

    border.onet.on.ca

    gate-gateway.gw

    .utoronto.ca

    srp2-0.core2-tor.bb.attcanada.ca

    pos8-0-0.hcap2-tor.

    bb.attcanada.ca

    atm1-0-101.adsl1-tor.

    bb.attcanada.ca

    206.222.73.174

    gar3-p370.attga.

    ip.att.net

    gbr6-p40.attga.

    ip.att.net

    www.kites.org

    sf-cs27.gw.

    utoronto.ca

    mcl-gpb.gw.

    utoronto.ca

    if-10-0.core1.

    Scarborough.Teleglobe.net

    if-3-2.core1.Toronto2.

    Teleglobe.netborder.onet.on.ca

    gate-gateway.gw

    .utoronto.ca

    ??

    if-7-0.core1.Chicago3.

    Teleglobe.net

    ??bed-va-0-105.

    cablenet-va.com

    ??

    12.122.10.29

    tbr1-p012201.

    n54ny.ip.att.net

    tbr2-p013601.

    wswdc.ip.att.net

    12.122.10.74

    12.124.65.22

    if-7-0.core1.Chicago3.

    Teleglobe.net

  • 8/8/2019 Week10 Sockets

    25/44

    25

    It depends on the size of the web pagewe retrieve.

    If the web page is 100 Kbytes (small!) itwill be broken up into ~80 IP packets.

    How many messages?

    10 (DNS) +

    22 (Connect with toronto.on.ca) +6 (DNS) ++ 80 * 20 hops = 1638 messages!

  • 8/8/2019 Week10 Sockets

    26/44

    26

    Sockets

    One form of communication betweenprocesses.

    Similar to pipes, except sockets can beused between processes on different

    machines.

    Use file descriptors to refer to sockets. Built on top of TCP layer

  • 8/8/2019 Week10 Sockets

    27/44

    27

    TCP: Three-way handshakeclient server

    socket

    connect

    (blocks)

    connect

    returns

    socket,bind,listen

    accept(blocks)

    accept returns

    SYN J

    SYN K, ack J+1

    ack K+1

    sequence

    number = J

    sequence

    number = K

  • 8/8/2019 Week10 Sockets

    28/44

    28

    read()

    write()

    close()

    socket()

    connect()

    write()

    read()

    close()

    block until connection

    from client

    Connection establishment

    (3-way handshake)

    data transfer

    end-of-file notification

    socket()

    bind()

    listen()

    accept()

    TCP ServerTCP Client

  • 8/8/2019 Week10 Sockets

    29/44

    29

    Connection-OrientedServer

    Create a socket: socket()

    Assign a name to a socket: bind() Establish a queue for connections: listen() Get a connection from the queue: accept()!Client

    Create a socket: socket() Initiate a connection: connect()!

  • 8/8/2019 Week10 Sockets

    30/44

    30

    Socket Types Two main categories of sockets

    UNIX domain: both processes on the samemachine

    INET domain: processes on different machines Three main types of sockets:

    SOCK_STREAM: the one we will useSOCK_DGRAM: for connectionless socketsSOCK_RAW

  • 8/8/2019 Week10 Sockets

    31/44

    31

    Addresses and Ports A socket pairis the two endpoints of the

    connection.

    An endpoint is identified by an IP address anda port.

    IPv4 addresses are 4 8-bit numbers: 128.100.31.200 = werewolf 128.100.31.201 = seawolf 128.100.31.202 = skywolf

    Ports because multiple processes can communicate

    with a single machine we need another identifier.

    try nslookup

  • 8/8/2019 Week10 Sockets

    32/44

  • 8/8/2019 Week10 Sockets

    33/44

    33

    read()

    write()

    close()

    socket()

    connect()

    write()

    read()

    close()

    block until connection

    from client

    Connection establishment

    (3-way handshake)

    data transfer

    end-of-file notification

    socket()

    bind()

    listen()

    accept()

    TCP ServerTCP Client

  • 8/8/2019 Week10 Sockets

    34/44

    34

    Server sideint socket(int family, int type,

    int protocol);

    family specifies protocol family:PF_INET IPv4PF_LOCAL Unix domain

    typeSOCK_STREAM, SOCK_DGRAM, SOCK_RAW

    protocol set to 0 except for RAW sockets

    returns a socket descriptor

  • 8/8/2019 Week10 Sockets

    35/44

    35

    bind to a nameint bind(int sockfd,

    const struct sockaddr *servaddr,

    socklen_t addrlen);

    sockfd returned by socket struct sockaddr_in {short sin_family; /*PF_INET */

    u_short sin_port;

    struct in_addr sin_addr;

    char sin_zero[8]; /*filling*/}; sin_addr can be set to INADDR_ANY to communicate

    on any network interface.

  • 8/8/2019 Week10 Sockets

    36/44

    36

    Set up queue in kernelint listen(int sockfd, int backlog)

    after calling listen, a socket is ready toaccept connections

    prepares a queue in the kernel where partiallycompleted connections wait to be accepted.

    backlog is the maximum number of partiallycompleted connections that the kernel shouldqueue.

  • 8/8/2019 Week10 Sockets

    37/44

    37

    Complete the connectionint accept(int sockfd,

    struct sockaddr *cliaddr,

    socklen_t *addrlen);

    blocks waiting for a connection (from thequeue)

    returns a new descriptor which refers to theTCP connection with the client

    sockfd is the listening socketcliaddr is the address of the client reads and writes on the connection will use

    the socket returned by accept

  • 8/8/2019 Week10 Sockets

    38/44

    38

    Client sidesocket() same as server, to say how we are

    going to talk

    int connect(int sockfd,

    const struct sockaddr *servaddr,

    socklen_t addrlen);

    the kernel will choose a dynamic port and sourceIP address.

    returns 0 on success and -1 on failure settingerrno.

    initiates the three-way handshake.

  • 8/8/2019 Week10 Sockets

    39/44

  • 8/8/2019 Week10 Sockets

    40/44

    40

    inetserver.cstruct sockaddr_in peer;!struct sockaddr_in self; !int soc, ns, k;!int peer_len = sizeof(peer);!self.sin_family = PF_INET;!self.sin_port = htons(PORT); !self.sin_addr.s_addr = INADDR_ANY;

    ! bzero(&(self.sin_zero), 8);!peer.sin_family = PF_INET;!/* set up listening socket soc */!soc = socket(PF_INET, SOCK_STREAM, 0);!if (soc < 0) { !perror("server:socket"); exit(1);!

    }!if (bind(soc, (struct sockaddr *)&self, sizeof(self))== -1){!perror("server:bind"); close(soc); exit(1); !

    }! listen(soc, 1);!... !

    full code forthis example is

    on the

    webpage

  • 8/8/2019 Week10 Sockets

    41/44

    41

    inetserver.c (concluded)/* ... repeated from previous slide ...!soc = socket(PF_INET, SOCK_STREAM, 0);!

    bind(soc, (struct sockaddr *)&self, sizeof(self))== -1){!perror("server:bind"); close(soc); exit(1); !

    }! listen(soc, 1);!... and now continuing ... */

    !/* accept connection request */!ns = accept(soc, (struct sockaddr *)&peer, &peer_len); !if (ns < 0) { !perror("server:accept"); close(soc); exit(1);!

    }!/* data transfer on connected socket ns */!k = read(ns, buf, sizeof(buf));!printf("SERVER RECEIVED: %s\n", buf);!

    write(ns, buf, k);! close(ns); ! close(soc);!

  • 8/8/2019 Week10 Sockets

    42/44

    42

    Byte order Big-endian

    Little-endian

    Intel is little-endian, and Sparc is big-endian

    00 01 64 C1

    A A+1 A+2 A+3

    91,329 =

    00 01 64 C1

    A+3 A+2 A+1 A

    91,329 =

  • 8/8/2019 Week10 Sockets

    43/44

    43

    Network byte order To communicate between machines with

    unknown or different endian-ness we

    convert numbers to network byte order (big-endian) before we send them.

    There are functions provided to do this:unsigned long htonl(unsigned long)!unsigned short htons(unsigned short)!unsigned long ntohl(unsigned long)!unsigned short ntohs(unsigned short)!

  • 8/8/2019 Week10 Sockets

    44/44

    44

    Sending and Receiving Dataread and write calls work on sockets, but

    sometimes we want more control

    ssize_t send(int fd, const void *buf,size_t len, int flags);

    works like write ifflags==0 flags: MSG_OOB, MSG_DONTROUTE, MSG_DONTWAIT

    ssize_t recv(int fd, void *buf,size_t len, int flags); flags: MSG_OOB, MSG_WAITALL, MSG_PEEK