+ All Categories
Home > Documents > Design of Client Software

Design of Client Software

Date post: 14-Apr-2018
Category:
Upload: toaster97
View: 224 times
Download: 0 times
Share this document with a friend

of 19

Transcript
  • 7/30/2019 Design of Client Software

    1/19

    1

    Chapter 7

    Design of Client Software(Recommended reading: Comer, Vol 3, Chapters 6, 7)

    In the previous chapter, we studied a

    simple TCP client.

    In this chapter, we study more issues about

    TCP clients, and then study UDP clients.

    Review of C Programming

    Memory Copy Function

    The function memcpy() copies the value of

    one field to another:

    The first argument is a pointer to the

    destination field.

    The second argument is a pointer to the

    source field.

    The third argument is the number of

    bytes to be copied.

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    2/19

    or

    2

    Example

    Copy the value of the y field to the x

    field:

    memcpy ( &x , &y , sizeof(x) ) ;

    Pointer to Structure

    If the pointer variable pv stores the address

    of a structure, we can access any member of

    this structure as follows:

    pv -> member_name

    (*pv) . member_name

    Example

    struct info {

    } ;

    int

    int

    ID ;

    age ;

    struct info student, *pv ;

    student . ID = 1234 ;

    student . age = 18 ;

    pv -> ID = 5678 ;

    (*pv) . age = 19 ;

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    3/19

    3

    Pointer Pointer

    A pointer pointer is a memory location that

    holds the address of a pointer.

    Example:

    char **x ;

    Design of TCP Clients

    A TCP client adopts TCP for connection-oriented communication. Recall its major steps

    for communication:

    Initialize sockets.

    Communicate between sockets.

    Close sockets.

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    4/19

    4

    Also recall the sequence of execution of

    WinSock functions:

    In this section, we study more issues in the

    design of client programs.

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    5/19

    5

    Specifying the Endpoint Address of the Server

    The client has to specify the endpoint

    address of the server, so that it can take

    initiative to connect to the server. An endpoint address contains three items:

    Address family

    IP address

    Port number

    For TCP/IP, the first item is alwaysAF_INET.

    Then the client has to specify the IP

    address and port number of the server.

    Problem

    How can a client program know the IP

    address and port number of the server?

    Some possible solutions

    1. The programmer specifies the server's

    endpoint address in the client program.

    When the server is moved to

    another computer, the clientprogram must be modified andrecompiled!

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    6/19

    6

    2. When a user invokes a client program,

    he specifies the endpoint address.

    The client program is general and

    flexible. This is a common approach.

    Examples

    The user specifies the IP address

    and port number:

    158.182.7.1 2000

    The user specifies the domain name

    (a symbolic representation of an IPaddress) and the port number:

    cs8888.comp.hkbu.edu.hk 2000

    Web browsing uses the well-known

    port number 80. When a userinvokes a client program (i.e., abrowser), he just inputs the domain

    name or IP address of the webserver.

    3. The client program obtains the endpoint

    address from stable storage (such as afile on a local disk).

    The client program cannot be

    executed without the file.

    Example: bookmarks in web

    browsing.

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    7/19

    7

    4. The client program uses a separate

    protocol to find the endpoint address

    (e.g., multicast or broadcast a

    message to which all serversrespond).

    Format Conversion for IP Address

    Very often, the IP address of the server is

    given in various formats:

    dotted decimal notation (e.g.,158.10.2.3) or

    domain name (e.g.,

    cs8888.comp.hkbu.edu.hk).

    It is necessary to convert these

    formats to 32-bit format. Conversion for Dotted Decimal Notation

    Call inet_addr() to convert an IP

    address in dotted decimal notation to

    32-bit IP address.

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    8/19

    8

    Conversion for Domain Name

    Call gethostbyname() to convert the

    domain name to 32-bit IP address. This

    function

    takes a string of domain name as an

    input, and

    returns the address of a hostnet

    structure:

    The above structure contains several

    items, and the item h_addr contains

    the pointer to the IP address.

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    9/19

    9

    Example

    char *ServerName =

    "cs8888.comp.hkbu.edu.hk" ;

    struct hostent *h ;

    struct sockaddr_in ServerAddr ;

    ServerAddr.sin_family = AF_INET ;

    ServerAddr.sin_port = 2000 ;

    h = gethostbyname (ServerName) ;

    memcpy ( &ServerAddr.sin_addr ,

    h->h_addr , h->h_length );

    Looking Up a Well-Known Port Number

    TCP/IP defines some standard applications

    (e.g., email, www), and the server of each

    application has a well-known port number. Examples

    Email server uses port number 25.

    Web server uses port number 80.

    Problem

    When a client program is given thename of a standard server application,

    how can it look up the well-known port

    number for this application?

    Dr. Y.W. Leung

  • 7/30/2019 Design of Client Software

    10/19

    Solution

    The client program calls getservbyname().

    This function has two arguments:

    a string that specifies the application

    (e.g., smtp);

    a string that specifies the protocol to be

    used (e.g., tcp).

    This function returns a pointer to a structure

    of type servent:

    where s_portcontains the port number innetwork byte order.

    Example

    To look up the port number for SMTP

    (simple mail transfer protocol), a client

    program calls getservbyname() as follows:struct servent *s ;

    s = getservbyname ( "smtp" , "tcp" )

    ;

    ServerAddr . sin_port = s->s_port ;

    Dr. Y.W. Leung 10

  • 7/30/2019 Design of Client Software

    11/19

    Choosing a Local Port Number

    A client has a local port number to form a

    local endpoint address.

    A client does not have a preassigned port

    number, and it has to select a local port

    number when it is executed.

    The client can select any port number within

    the range 49152 65535, as long as this

    number is not being used by the otherapplications on the same computer.

    A TCP client usually allows the TCP/IP

    software to select an unused local port

    number automatically.

    Send and Receive Data A client calls send() once to send a block of

    data.

    A client may have to call recv() multiple

    times to receive a complete block of data,

    because it may receive a portion at a time(why?).

    Dr. Y.W. Leung 11

  • 7/30/2019 Design of Client Software

    12/19

    Example

    The client sends a message to a server,

    and expects a reply with not more than

    120 characters.

    Dr. Y.W. Leung 12

  • 7/30/2019 Design of Client Software

    13/19

    The Design of UDP Clients

    A UDP client is similar to a TCP client, except that

    it needs not establish a connection to the server. Warning about UDP unreliability

    Recall that UDP does not guarantee reliable

    datagram delivery.

    A UDP client may have to tackle reliability

    itself (e.g., through retransmission, packetresequencing, ...)

    This may be complicated.

    Even if a UDP client works well in local

    networks, it may not work across a wide area

    internet (why?)

    It may be difficult to debug a UDP client

    program.

    When a UDP client calls socket() to create a

    new socket, it must specify that it needs a

    datagram socket. Example:

    s = socket( AF_INET , SOCK_DGRAM , 0 );

    A UDP client uses eitherconnected mode or

    unconnected mode for communication.

    Dr. Y.W. Leung 13

  • 7/30/2019 Design of Client Software

    14/19

    Unconnected Mode

    Each time a client sends a message, it has to

    specify the endpoint address of the server.

    sendto()

    Call sendto() to send a block of data to

    a specified endpoint address.

    The function sendto() is similar to

    send(), except that sendto() needs the

    destination endpoint address as anadditional argument.

    recvfrom()

    Call recvfrom() to receive

    data contained in one segment, and

    the sender's endpoint address.

    The function recvfrom() is similar to

    recv(), except that recvfrom() extracts

    and records the sender's endpoint

    address.

    It is more convenient to send a

    reply.

    Dr. Y.W. Leung 14

  • 7/30/2019 Design of Client Software

    15/19

    Example

    charSOCKET

    buf[100] ;s;

    struct sockaddr_in ServerAddr ;

    sendto ( s , buf , sizeof(buf) , 0 ,

    &ServerAddr , sizeof(ServerAddr) );

    Dr. Y.W. Leung 15

  • 7/30/2019 Design of Client Software

    16/19

    Examplechar

    SOCKET

    buf[100] ;

    s ;

    struct sockaddr_in

    int len;

    RemoteAddr ;

    len = sizeof ( RemoteAddr ) ;

    recvfrom ( s , buf , sizeof(buf) , 0

    , &RemoteAddr , &len) ) ;

    Dr. Y.W. Leung 16

  • 7/30/2019 Design of Client Software

    17/19

    Connected Mode

    Using the connected mode, the client calls

    connect() to specify a remote endpoint

    address, and then it can send and receivemessages without specifying the remote

    endpoint address again.

    The connected mode is convenient when the

    client interacts with only one server at a

    time, because the client only needs tospecify the remote endpoint address once no

    matter how many datagrams it sends.

    Communication in Connected Mode

    A UDP client calls connect() with

    SOCK_DGRAMto record the remoteendpoint address in the socket's data

    structure (not to establish a connection

    to the server.)

    Then the UDP client can call send() to

    send a message and recv() to receive a

    message contained in one segment.

    Dr. Y.W. Leung 17

  • 7/30/2019 Design of Client Software

    18/19

    Remarks

    In network programming, we have to handle

    many details.

    A complete program is quite complicated.

    Many programs may contain the same

    segments of code (e.g., the code for allocating

    and connecting a socket).

    We can code these segments as procedures.

    The program is more structured, and the

    procedures can be re-used in the other

    programs.

    Network programming becomes simpler.

    A programmer may construct a library of

    procedures for his/her own use.

    Example

    A programmer may write a procedure for

    allocating and connecting a TCP socket:

    sock_descriptor = connectTCP ( machine, service )

    Whenever he writes a network program, he

    can call this procedure without concerning

    their details.

    Dr. Y.W. Leung 18

  • 7/30/2019 Design of Client Software

    19/19

    Tutorial Problems

    What is the main difference between the usage

    ofsend() for TCP communication and UDP

    communication?

    What is the main difference between the usage

    ofrecv() for TCP communication and UDP

    communication?

    A student argues that a UDP client in

    unconnected mode can call recv() instead of

    recvfrom() to receive data, because the client

    must know the endpoint address of the server

    and therefore it is not necessary to use

    recvfrom() to extract this endpoint address. Do

    you agree? If a client wants to communicate with three

    servers, does this client require three sockets?

    List the sequence of execution of WinSock for

    a UDP client using the

    unconnected mode,

    connected mode.

    Dr Y W Leung 19


Recommended