03 Client

download 03 Client

of 30

Transcript of 03 Client

  • 7/27/2019 03 Client

    1/30

  • 7/27/2019 03 Client

    2/30

    Client designs Connection-oriented clients

    Connectionless clients

    Client examples TCP DAYTIME

    UDP TIME

    TCP and UDP ECHO

  • 7/27/2019 03 Client

    3/30

    hostentry

    ip address(32b)

    ip addressdot.decimal

    gethostbyname

    gethostbyaddr

    inet_aton

    serviceentry

    port #(16b)

    getservbyname

    protocolentry

    portocol #getprotobyname

    getsockname

    getsockopt

    setsockopt

    host nt

    addr type

    name

    alias

    length

    addr list

    icmp 1igmp 2tcp 6udp 17rsvp 46sctp 132raw 255

    s rv nt

    port

    name

    alias list

    protocol

    proto nt

    protocol #

    name

    alias list

    http 80dns 53smtp 25telnet 23daytime 13ftp 21

  • 7/27/2019 03 Client

    4/30

    Domain Name System (DNS) System configuration files, e.g.

    /etc/hosts, /etc/services, /etc/resolv.conf

    Convert between names and numeric values gethostbyname() and gethostbyaddr()

    Convert between hostnames and IPv4 addresses

    getservbyname() and getservbyport()

    Convert between service names and port numbers getaddrinfo() and getnameinfo()

    Two protocol-independent functions

    Convert between hostnames and IP addresses andbetween service names and port numbers

  • 7/27/2019 03 Client

    5/30

    Typical arrangement of clients, resolvers andname servers

    /etc/resolv.conf(IP addresses oflocal name servers)

  • 7/27/2019 03 Client

    6/30

    #include

    struct hostent *gethostbyname(const char *hostname);

    struct hostent {

    char *h_name; /* official (canonical) name of host */

    char **h_aliases; /* ptr to array of ptrs to alias names */

    int h_addrtype; /* host address type: AF_INET */

    int h_length; /* length of address: 4 */

    char **h_addr_list; /* ptr to array of ptrs with IPv4 addrs */

    }

  • 7/27/2019 03 Client

    7/30

    #include

    struct servent *getservbyname(const char *servname,

    const char *protoname);

    struct servent *getservbyport(int port,

    const char *protoname);

    struct servent {

    char *s_name; /* official service name */

    char **s_aliases; /* aliases list */

    int s_port; /* port number, network byte order */int s_proto; /* protocol to use */

    }

  • 7/27/2019 03 Client

    8/30

    struct servent *sptr;

    sptr = getservbyname(domain, udp); // DNS using UDP

    sptr = getservbyname(ftp, tcp); // FTP using tCP

    sptr = getservbyname(ftp, udp); // This call will fail

    sptr = getservbyport(htons(21), tcp); // FTP using TCP

    sptr = getservbyport(htons(21), NULL); // FTP using TCP

    sptr = getservbyport(htons(21), udp); // This call will fail

  • 7/27/2019 03 Client

    9/30

    1. Find the IP address and protocol portnumber of the server

    2. Allocate a socket

    3.

    Specify the local port number or allow TCPto choose one

    4. Connect the socket to the server

    5. Communicate with the server

    6. Close the connection

  • 7/27/2019 03 Client

    10/30

    1a) Find servers IP address Converting from dotted decimal to 32-bit binary

    notation#include

    int inet_aton(const char *strp, struct in_addr *inp);in_addr_t inet_addr(const char *strp); // deprecated

    Converting from symbolic name to 32-bit binarynotation

    #include

    struct hostent *gethostbyname(const char *hostname);

  • 7/27/2019 03 Client

    11/30

    1b) Find servers port number by service#include

    struct servent *getservbyname(const char *name,

    const char *protoname);

    namedesired service name, e.g. smtp protonametransport protocol being used, e.g. tcp

  • 7/27/2019 03 Client

    12/30

    2. Allocate a socket#include

    #include

    int sd; /* socket descriptor */

    sd = socket(PF_INET, SOCK_STREAM, 0);

    // valid only if sd > 0

  • 7/27/2019 03 Client

    13/30

    3. Specify local port # or allow TCP to choose one Which port number is right for client?

    Users may choose the specific port Dont have to use a known port number

    Possible, cause an error if the port is in use

    struct sockaddr_in sockinfo;

    sockinfo.sin_port = htons(8000);

    Or let the system choose one (recommended)

    sockinfo.sin_port = htons(0);

  • 7/27/2019 03 Client

    14/30

    4. Connect the socket to the server#include

    int connect(int sockfd,

    const struct sockaddr *servaddr,

    socklen_t addrlen);

    What does it do? Ensure socket is valid and unconnected

    Enter remote endpoint into socket descriptor table Select and enter local endpoint information

    Complete 3-way handshake and establish TCPconnection

  • 7/27/2019 03 Client

    15/30

    Why there is no source address specified inthe interface? The system chooses the right IP address for you

    Otherwise, users should figure out it if there are

    multiple network interfaces available Not sure which IP address should be used in the local

    address

    Wrong address could result in inefficient routingcausing significant delays

  • 7/27/2019 03 Client

    16/30

    5. Communicate with the server Reading from the connection

    recv(socket, msg, msglen, flags) or

    read(socket, msg, msglen)

    Writing into the TCP connection send(socket, msg, msglen, flags) or

    write(socket, msg, msglen)

  • 7/27/2019 03 Client

    17/30

    TCP is a stream oriented protocol TCP packets could be fragmented during transmission

    Need to loop until all the data are receivedn = recv(sd, buf, buflen, 0);

    while (n != SOCKET_ERROR && n != 0) {

    buf += n;

    buflen -= n;

    n = recv(sd, buf, buflen, 0);

    }

  • 7/27/2019 03 Client

    18/30

    6. Close the connection What if there are outstanding data to

    send/receive when closing the connection?

    Better coordination: Partial close#include

    int shutdown(int sockfd, int howto);

    SHUT_RD no further reception allowed

    SHUT_WR no further transmission allowed

    SHUT_RDWR no further receptions & transmission allowed

  • 7/27/2019 03 Client

    19/30

    close() Only closes the socket ID for the process

    Leaves the connection open if there is anotherprocess using the same socket

    shutdown() Breaks the connection for all the processes using

    the socket

  • 7/27/2019 03 Client

    20/30

    sd= connect();if (fork() == 0) { /* Child: send input */

    while (gets(buf) > 0) {

    write(sd, buf, strlen(buf));

    }

    close(sd);

    exit(0);} else { /* Parent: read from server */

    while ((datalen = read(sd, buf, buflen)) > 0) {

    do_something(buf, datalen);

    }

    wait(0);exit(0);

    }

    sdis used by both read and write processes.

  • 7/27/2019 03 Client

    21/30

    1. Find the IP address and protocol portnumber of the server

    Similar to connection-oriented clients

    2. Allocate a socket

    socket(PF_INET, SOCK_DGRAM, 0);

    3. Specify the local port number or allow UDPto choose one

    Like connection-oriented clients

  • 7/27/2019 03 Client

    22/30

    4. Specify the server to which the message issent

    5. Communicate with the server Unconnected UDP sockets

    Connected UDP sockets

    6. Close the socket or partial close the socket

  • 7/27/2019 03 Client

    23/30

    Socket is NOT tied to a specific server Should specify remote address every time called

    Can interact with multiple servers

    sendto(socket, msg, msglen, flags, to, tolen)

    recvfrom(socket, buf, buflen, flags,

    from, fromlen)

  • 7/27/2019 03 Client

    24/30

    The socket is tied to a specific server afterconnect call Remote endpoint info is stored in socket descriptor

    table

    Dont have to specify remote address every time called recv() and send() can be used

    Interact with only one server

  • 7/27/2019 03 Client

    25/30

    What should be different? What should becareful in UDP programming? What if the packet is dropped?

    What if the packet is delivered in out of order?

    For reliability Packet sequencing

    Acknowledgements

    Timeouts

    Retransmission

  • 7/27/2019 03 Client

    26/30

    DAYTIME service TCP client for DAYTIME

    TIME service UDP client for the TIME service

    ECHO service TCP and UDP clients for the ECHO service

    Use common procedures

    connectTCP(host, service) connectUDP(host, service) connectsock(host, service, transport) errexit(format, )

  • 7/27/2019 03 Client

    27/30

    Available as TCP and UDP services at port 13 How to support both TCP and UDP services?

    E.g. select(), multi-threaded, or multi-process

    select()

    Poll multiple file/socket descriptors Allow to handle whichever comes available first

  • 7/27/2019 03 Client

    28/30

    Server sends daytime as a character string Example:

    Wednesday, September 10, 2014 18:30:40-PDT

    TCP version Server sends daytime message whenever it accepts

    a connection

    UDP version Server sends daytime message whenever it receives

    a datagram

    Example code TCPdaytime.c in the textbook

  • 7/27/2019 03 Client

    29/30

    Available as TCP and UDP services at port 37 Server responds with time in Universal

    Coordinated Time (UCT) 32-bit integer as the number of seconds since

    January 1, 1900.

    Requesting the time TCP: connection triggers a response

    UDP: client datagram triggers a response

    Example code: UDPtime.c

  • 7/27/2019 03 Client

    30/30

    Available as TCP and UDP services at port 7 Test connectivity

    debug protocols

    find routing problems

    Server sends back to client all the data itreceives from the client

    Example code: TCPecho.c and UDPecho.c