CSIE,NTUT,Taiwan 1
Client Software Design
Algorithms and Issues
Chuan-Ming Liu
Computer Science and Information Engineering
Spring 2004,NTUT
TAIWAN
2CSIE,NTUT,Taiwan
Client Software Design
How applications become clients by initiating
communication
How applications use TCP or UDP protocols
to contact a server
How they use socket calls to interact with the
protocols
3CSIE,NTUT,Taiwan
Learning Algorithms Instead of
Details
Learn know-what instead of know-how first
After knowing what to do,finding out how to
do is straightforward
4CSIE,NTUT,Taiwan
Client Architecture
Client applications are simpler than server
applications
No concurrent interaction
Most client software executes as a conventional
application
Don’t need to enforce protection
5CSIE,NTUT,Taiwan
Identifying Server’s location
Have the server’s domain name or IP address
as constants when program is compiled
Using address makes client software faster and less
dependent on local computing environment
– no flexibility
Using a generic name for the server and an alias to
the domain name system for that name
– improvement
Obtain information from stable storage
Can not execute program unless the file is available
6CSIE,NTUT,Taiwan
Identifying Server’s location
Use a separate protocol to find a server
Works in a small,local environment
The dynamic property makes additional complexity
and broadcast traffic on network
Ask users to identify
Avoid unnecessary complexity
Makes the client software general
Eliminates dependency
Most flexible
Approach taken depends on services
7CSIE,NTUT,Taiwan
Parsing an Address Argument
Specify arguments on command line when
invoking a client program
Each argument consists of a character string
Client uses an argument’s syntax to interpret
its meaning
Example:
Domain name,csie.ntut.edu.tw
IP address,140.124.180.1
8CSIE,NTUT,Taiwan
Parsing an Address Argument
Additional information beyond the server’s
machine,or IP address can also be specified
Example,(two arguments)
csie.ntut.edu.tw smtp
Csie.ntut.edu.tw:smtp
Keep consistency with the local system
9CSIE,NTUT,Taiwan
Looking up a Domain Name
Client needs to specify the address of a server
There are two forms in address argument
Domain name
IP address (dotted decimal)
Converting IP address to 32-bit binary
representation is trivial
Converting from a domain name require more
effort.
10CSIE,NTUT,Taiwan
Looking up a Domain Name
Socket API includes library routines,inet_addr
and gethostbyname to perform the conversion
inet_addr:
Takes dotted decimal address (in ASCII) and
returns the IP address in binary
gethostbyname:
Takes the domain name and returns the address of
a hostnet structure containing the IP address in
binary
11CSIE,NTUT,Taiwan
Hostnet Structure
struct hostent
{
char *h_name; /* Official name of host,*/
char **h_aliases; /* Alias list,*/
int h_addrtype; /* Host address type,*/
int h_length; /* Length of address,*/
char **h_addr_list; /* List of addresses from name server,*/
};
#define h_addr h_addr_list[0] /* Address,for backward
compatibility,*/
12CSIE,NTUT,Taiwan
Looking up a Well-known Port by
Name
Client also needs to look up the protocol port
for the specific service
Example
A client of an SMTP mail server needs to look up
the well-known port assigned to SMTP
Library function,getservbyname,can do this
Takes two arguments (strings):
Service
Protocol
Returns a pointer to servent structure
13CSIE,NTUT,Taiwan
Servent Structure
struct servent
{
char *s_name; /* Official service name,*/
char **s_aliases; /* Alias list,*/
int s_port; /* Port number,*/
char *s_proto; /* Protocol to use,*/
};
14CSIE,NTUT,Taiwan
Looking up a Protocol by Name
Mapping a protocol name to the integer
constant assigned to that protocol
Library function,getprotobyname,performs
this lookup
getprotobyname:
Takes a protocol name (in string) and returns the
address of a protoent structure
15CSIE,NTUT,Taiwan
Protoent Structure
struct protoent
{
char *p_name; /* Official protocol name.*/
char **p_aliases; /* Alias list,*/
int p_proto; /* Protocol number,*/
};
16CSIE,NTUT,Taiwan
TCP Client Algorithm
1,Find the IP address and protocol port number
of the server
2,Allocate a socket
3,Specify that the connection needs an
arbitrary,unused protocol port on the local
machine,and allow TCP to choose one
4,Connect the socket to the server
5,Communicate with the server using the
application-level protocol
6,Close the connection
17CSIE,NTUT,Taiwan
Allocating a Socket
TCP is specified by protocol family PF_INET
and service SOCK_STREAM
Recall the system call,socket(),which has
three arguments
int socket(int domain,int type,int protocol);
Example:
#include <sys/types.h>
#include<sys/socket.h>
int s
s=socket (PF_INET,SOCK_STREAM,0)
18CSIE,NTUT,Taiwan
TCP Client Algorithm
1,Find the IP address and protocol port number
of the server
2,Allocate a socket
3,Specify that the connection needs an
arbitrary,unused protocol port on the local
machine,and allow TCP to choose one
4,Connect the socket to the server
5,Communicate with the server using the
application-level protocol
6,Close the connection
19CSIE,NTUT,Taiwan
Local Protocol Port Number
Application needs to specify remote and local
endpoint addresses
Server operates at a well-known protocol port
address
TCP client does not operate on a pre-assigned
port,but a local port is necessary for its
endpoint address
Socket interface provides a way that a client
can allow TCP to choose a local port
automatically – side effect of the connect call
20CSIE,NTUT,Taiwan
Local IP Address
Client also needs local IP address for endpoint
address
For single host attached to one network,the
selection is trivial
For a host with multiple IP addresses,the
selection is difficult
Correct choice depends on routing
Applications seldom have access routing information
21CSIE,NTUT,Taiwan
Local IP Address -- Problem
host
140.124.180.xxx
140.113.xxx.xxx
192.168.0.xxx
Problem,IP source address in an outgoing datagram
should match the IP address of the network interface
over which IP routes the datagram
22CSIE,NTUT,Taiwan
Local IP Address
Client may work even using the wrong IP since
the packets travel a different route
Difficult to manage
Confusing
Less reliability
How to solve
socket calls make it possible for an application
to leave the local IP address field unfilled and to
allow TCP/IP software to choose a local IP
address automatically at the time the client
connects to a server
23CSIE,NTUT,Taiwan
TCP Client Algorithm
1,Find the IP address and protocol port number
of the server
2,Allocate a socket
3,Specify that the connection needs an
arbitrary,unused protocol port on the local
machine,and allow TCP to choose one
4,Connect the socket to the server
5,Communicate with the server using the
application-level protocol
6,Close the connection
24CSIE,NTUT,Taiwan
Connection
Initiate TCP 3-way handshake
The call does not return until a TCP
connection established or TCP connection fails
Four steps:
Socket validation
Fill remote endpoint address
Choose a local endpoint address for connection
Initiate a TCP connection and return a value
25CSIE,NTUT,Taiwan
Client Server
SYN
ISN=X
1
SYN
ISN=YACK=X+1
2
ACK=Y+1 3
26CSIE,NTUT,Taiwan
TCP 3-way handshake
Client:,I want to talk,and I’m starting with byte
number X”.
Server:,OK,I’m here and I’ll talk,My first byte will
be called number Y,and I know your first byte will
be number X”.
Client:,Got it - you start at byte number Y”.
Bill:,Monica,I’m afraid I’ll syn and byte your ack”
1
2
3
27CSIE,NTUT,Taiwan
Why 3-Way?
Why is the third message necessary?
HINTS,
TCP is a reliable service.
IP delivers each TCP segment.
IP is not reliable.
28CSIE,NTUT,Taiwan
Communicate with the Server
Request-response interaction
Client sends a sequence of requests and waits for a
response to each
Send a request by send( )
Wait for a response by recv( )
29CSIE,NTUT,Taiwan
Receiving a Response
TCP is stream-oriented,not block-oriented
Guarantee to deliver the sequence of bytes sent,
but does not guarantee to delivery them in the
same grouping they were sent
Because TCP does not preserve record
boundaries,any program that receives from a
TCP connection must be prepared to accept
data a few bytes at a time,This rule holds even
if the sending application transfers data in
large blocks
30CSIE,NTUT,Taiwan
Closing a TCP Connection
Using close () system call to terminate the
connection and gracefully deallocating the
socket
Closing a socket seems simple,but actually not
client server
Repeatedl
y sending
requests
Repeatedl
y
receiving
requests
Can not
terminateCan not
terminate also
31CSIE,NTUT,Taiwan
FIN
Either end of the connection can initiate
termination.
A FIN is sent,which means the application is
done sending data.
The FIN is ACK’d.
The other end must now send a FIN.
That FIN must be ACK’d.
32CSIE,NTUT,Taiwan
App1 App2
FIN
SN=X
1
ACK=X+1 2
ACK=Y+1 4
FIN
SN=Y
3
...
33CSIE,NTUT,Taiwan
TCP Termination
1
2
3
4
App1:,I have no more data for you”.
App2:,OK,I understand you are done sending.”
dramatic pause…
App2:,OK - Now I’m also done sending data”.
App1:,Roger,Over and Out,Goodbye,Astalavista
Baby,Adios,It’s been real,..”
camera fades to black,..
34CSIE,NTUT,Taiwan
Partial Close
To overcome the problem for closing a socket
earlier,most implementations use a system call,
shutdown( )
Shutdown system call has two arguments
Descriptor
Direction:
0,no more input
1,no more output
2,shutdown both directions
35CSIE,NTUT,Taiwan
UDP Client Algorithm
1,Find the IP address and protocol port number of the
server
2,Allocate a socket
3,Specify that the connection needs an arbitrary,unused
protocol port on the local machine,and allow UDP to
choose one
4,Specify the server to which messages to be sent
5,Communicate with the server using the application-
level protocol
6,Close the connection
36CSIE,NTUT,Taiwan
Unconnected v.s,Connected
UDP socket can use
connected mode
use connect to specify a remote endpoint address
unconnected modes
specify destination at each time sending a message
convenience
flexibility
37CSIE,NTUT,Taiwan
Connect – UDP
To be UDP,using SOCK_DGRAM type of
service
When calling connect() using SOCK_DGRAM
connect only stores an address
Does not guarantee that the remote endpoint
address is valid or that the server is reachable
38CSIE,NTUT,Taiwan
UDP Client Algorithm
1,Find the IP address and protocol port number of the
server
2,Allocate a socket
3,Specify that the connection needs an arbitrary,unused
protocol port on the local machine,and allow UDP to
choose one
4,Specify the server to which messages to be sent
5,Communicate with the server using the application-
level protocol
6,Close the connection
39CSIE,NTUT,Taiwan
Communication with a Server
Also,use request-response interaction
UDP provides message transfer,i.e.,block-
oriented
40CSIE,NTUT,Taiwan
UDP Client Algorithm
1,Find the IP address and protocol port number of the
server
2,Allocate a socket
3,Specify that the connection needs an arbitrary,unused
protocol port on the local machine,and allow UDP to
choose one
4,Specify the server to which messages to be sent
5,Communicate with the server using the application-
level protocol
6,Close the connection
41CSIE,NTUT,Taiwan
Closing a Socket
Use close() to close a socket and release the
resource related with it.
After closing,UDP will reject further
messages
The other side (server) never knows
Shutdown can be used on a UDP socket; again,
it only stops transferring data in the specified
direction
42CSIE,NTUT,Taiwan
UDP Unreliability
The simple UDP algorithm is unreliable
Duplicated packets
Unordered packets
Long delay
Etc…
CSIE,NTUT,Taiwan 43
Client Software Design
Examples
44CSIE,NTUT,Taiwan
Why Small Examples
Highlight the fundamental algorithms
Illustrate clearly how client and server interact
Have more intuition about implement services
45CSIE,NTUT,Taiwan
Developing Methods
Modular program (procedures)
Top-down analysis
Hiding details
Reusable segments (pieces of codes)
Abstraction
46CSIE,NTUT,Taiwan
Developing Methods
To implement TCP and UDP applications
What are in common?
The differences exist in connection and communication
Others are the same
For connection,by following parameterization,we
may use arguments to differentiate them
Or,we follow the examples in the text
connectTCP(machine,service)
connectUDP(machine,service)
47CSIE,NTUT,Taiwan
Developing Methods
Examples – online
connectTCP.c
connectUDP.c
connectsock.c
memset( )
memcpy( )
errexit.c
48CSIE,NTUT,Taiwan
DAYTIEM service
Allows a user to obtain the data and time in a
format fit for human consumption
Available for TCP and UDP
Protocol port,13
TCP version
using TCP connection to trigger output
UDP version
client needs a sends a request which will be
discarded by the server
49CSIE,NTUT,Taiwan
50CSIE,NTUT,Taiwan
TCP DAYTIME Service
TCPdaytime.c
Exercise:
Can you write UDPdaytime.c similarly?
51CSIE,NTUT,Taiwan
TIME Service
Allows one machine to obtain the current data
and time of day from another
All time and date information is represented in
Universal Coordinated Time (UCT,or UT)
TIME service is intended for use by programs
that store or manipulate times
52CSIE,NTUT,Taiwan
TIME Service
Available for TCP and UDP
Protocol port,37
TCP connection is similar to DAYTIME
service
UDP connection needs a client to send a
request
Be careful about the time you get by using
TIME service
53CSIE,NTUT,Taiwan
UDP TIME Service
UDPtime.c
Exercise:
Can you write TCPtime.c similarly?
54CSIE,NTUT,Taiwan
ECHO Service
Returns all the data it receives from a client
Can be implemented by both UDP ad TCP
TCPecho.c
UDPecho.c
Biggest difference between UDP and TCP
versions,datagram-oriented v.s,stream-
oriented