Client Software Design

advertisement
Client Software Design
Objectives:
Understand principles of C/S design, with
focus on clients
Review Windows implementations of Socket
functions
Architectural Objectives for C/S
• Partition applications across multiple machines
– better utilization of computing resources
• Place user interaction portion of application
closer to the user (client)
– Better utilization of network bandwidth.
• Share server resources among many clients
– Improve efficiency / utilization of resources
– Synchronize use of shared resources
– Control / manage access to shared resources
CS431-cotter
2
C/S Architecture
Application
Client
CS431-cotter
Server
3
Example C/S Applications
• File Servers
– C/S application used to locate and retrieve remote
files
• Database Applications
– C/S used to present database queries to remote db
• Groupware
– C/S used to share messages among group members
• Web Server
– C/S used to query web server for web site
information, pages.
CS431-cotter
4
2-Tier vs. 3-Tier C/S Design
• 3-Tier design extends distribution of application
across more levels / platforms.
• Client
CS431-cotter
Local Server
Enterprise Server
Local Database Enterprise Database
App. Server
Database Server
5
Client Software Design Issues
• Identifying Server Location
– Command Line Argument
– User query
– Embedded (fixed) server ID
• Parsing Address Argument
– domain name vs. IP address format
• Look up domain name
– hptr = gethostbyname( example_name)
– IP addr in hptr --> h_addr;
• Network byte order vs. local byte order
CS431-cotter
6
TCP Client Algorithm
• Find IP address and port number of server
• Allocate a socket
• Allow TCP to choose an arbitrary, unused
local port
• Connect the socket to the server
• Communicate with server (application
level)
• Close connection
CS431-cotter
7
TCP Client Algorithm Issues
• Chose a local Port number
– allow connect to select port
– [could use bind ( ) if needed]
• Identify local IP address
– allow connect to specify local IP address
– use gethostname ( )
– use gethostbyname ( )
CS431-cotter
8
TCP Client Algorithm Issues
• Client / Server Communications
– request / response interaction
– write / read (send / recv)
• Single write may require multiple reads
– response may be segmented
– continue appending reads until return length =
0
CS431-cotter
9
TCP Client Algorithm Issues
• Connection may not be certain when all
information or requests have been transferred
• Partial close allows graceful termination
–
–
–
–
shutdown ( s, direction)
0 = no input, 1 = no further output, 2 = both directions
Sends an EOF to Server
When server has completed transmission, it shuts
down and then closes socket.
CS431-cotter
10
TCP Receive Completion
• Protocol may fragment transmitted packet, but
does not directly support any way for the
application to determine when the full packet
has been received.
• Therefore, application needs to provide a
mechanism to identify when all segments of the
packet have been received.
CS431-cotter
11
Reception Complete Methods
• Predetermine packet size. Reception is
complete when the predetermined number of
bytes have been received.
– All messages are a fixed size
– Each message is of a predetermined size
• Terminate each packet with a sentinel character
• Follow each packet with a packet of 0 bytes
• etc.
CS431-cotter
12
UDP Client Algorithm
• Find IP addr and port number of server
• Allocate a socket
• Allow UDP to choose an arbitrary, unused local
port
• Specify Server for messages
• Communicate with server (application level)
• Close socket
CS431-cotter
13
UDP Client Algorithm Issues
• UDP Basic communication Modes
– Connected: use aconnect call to specify remote
server. then use read/write to communicate
– Unconnected: specify server address with each
message
• UDP transfers entire message in a single call.
– (assumes buffer space and transport are sufficient)
– partial close provides no intersocket communications
– UDP is UNRELIABLE! App must be able to deal with
inconsistent results.
CS431-cotter
14
Windows Sockets Introduction
•
•
•
•
History
Shared Functions
Protocol Specific Functions
Socket Database Routines
CS431-cotter
15
Windows Sockets
Winsock 1.1
• First formal specification developed in
1992 through an industry task force
• Winsock 1.1 finalized 1993.
• Intended to facilitate porting BSD socket
code to Windows environment
• Supports both 16 bit and 32 bit operating
environments (Windows 3.0 and up).
CS431-cotter
16
Windows Sockets
• Actual implementations are system or
machine dependent
– All implementations should support winsock.h
– Implementation support provided through
winsock.dll
CS431-cotter
17
Windows Sockets
• Negotiate for appropriate winsock support
• retvalue = WSAStartup (WORD version,
WSADATA SocketImp)
– version = The highest version of sockets that
the app can use.
– SocketImp = data structure containing info on
the available socket implementations
– retvalue = 0 or error
CS431-cotter
18
Windows Sockets
• Close down access to socket
implementation
• retvalue = WSACleanup ( void);
– retvalue = 0 or SOCKET_ERROR
CS431-cotter
19
Windows Sockets
Common Socket Routines
• retval = inet_addr ( dotted )
– dotted = string with IP address in dotted
decimal form
– retval = unsigned long with IP addr in binary
form or INADDR_NONE if not valid
• retval = inet_ntoa ( ipaddr )
– ipaddr = struct in_addr with IP addr info
– retval = string with IP address indotted
decimal form
CS431-cotter
20
Windows Sockets
Common Socket Routines
•
•
•
•
•
•
•
•
htonl ( );
ntohl ( );
socket ( );
bind ( );
connect ( );
select ( );
closesocket ( );
shutdown ( );
CS431-cotter
htons ( );
ntohs ( );
21
Windows Sockets
Common Socket Routines
• retvalue = ntohl(netlong);
– converts unsigned long from network format to local
– (IP Address)
• retvalue = ntohs(netshort);
– converts unsigned short from network format to local
– (Port Number)
• retvalue = htonl(hostlong);
– converts unsigned long from local format to network
• retvalue = htons(hostshort);
– converts unsigned short from local format to network
CS431-cotter
22
Windows Sockets
Common Socket Routines
• retvalue = socket (family, type, protocol);
– retvalue = socket descriptor or INVALID_SOCKET
(use WSAGetLastError() to retrieve error code)
– family = protocol family (PF_INET for IP)
– type = (service type: SOCK_STREAM for TCP,
SOCK_DGRAM for UDP)
– protocol = protocol number or use 0 to match type
CS431-cotter
23
Windows Sockets
Common Socket Routines
• retvalue = bind (socket, localaddr, addrlen);
–
–
–
–
retvalue = 0 for success or SOCKET_ERROR
socket = socket to be bound to a port
localaddr = sockaddr struct for local binding address
addrlen = length of localaddr
• retvalue = connect (socket, addr, addrlen)
–
–
–
–
retvalue = 0 for success or SOCKET_ERROR
socket = local socket to be used for connection
addr = sockaddr struct for remote binding address
addrlen = length of addr
CS431-cotter
24
Windows Sockets
Common Socket Routines
• retvalue = select (ignore, refds, wefds, exfds,
time);
– Will be explained later…….
• retvalue = closesocket (socket);
– retvalue = 0 or SOCKET_ERROR
– socket = socket to be closed;
• retvalue = shutdown ( socket, how);
– retvalue = 0 or SOCKET_ERROR;
– socket = socket to be shutdown;
– how = 0 (incoming), 1 (outgoing), 2 (both directions)
CS431-cotter
25
Windows Sockets
Protocol Specific Functions
TCP
accept ( );
listen ( );
recv ( );
send ( );
CS431-cotter
UDP
recvfrom( );
sendto ( );
26
TCP Connection-Oriented
Communications
• retvalue = listen (socket, queuelen);
– retvalue = 0 / SOCKETT_ERROR
– socket = socket being monitored
– queuelen = incoming request queue size
• retvalue = accept (socket, addr, addrlen);
– retvalue = socket descriptor assigned to new
connection
– socket = incoming socket being monitored
– addr = struct sockaddr that is filled in with incoming
address information
– addrlen = length of address structure
CS431-cotter
27
TCP Connection-Oriented
Communications
• retvalue = recv (socket, buffer, length, flags);
– retvalue
–
–
–
–
= # of bytes received, or
0 if connection is closed, or
SOCKET_ERROR if an error occurred
socket = socket used for incoming message
buffer = place where incoming message is stored
length = length of buffer
flags = control type info....
CS431-cotter
28
TCP Connection-Oriented
Communications
• retvalue = send (socket, msg, msglen, flags);
– retvalue = # of bytes sent, or
SOCKET_ERROR if an error occurred
– socket = socket used for outgoing message
– msg = pointer to outgoing message
– msglen = length of message
– flags = control type info....
CS431-cotter
29
UDP Connectionless
Communications
• ret = recvfrom (socket, buf, buflen, flags, from,
fromlen);
SOCKET socket = client socket
char FAR* buf = buffer to receive incoming msg
int
buflen = size of buffer
int
flags = control bits (OOB data, etc.)
struct sockaddr FAR* from = server address
int
fromlen = size of address structure
CS431-cotter
30
UDP Connectionless
Communications
• ret = sendto ( socket, msg, msglen, flags, to,
tolen);
SOCKET socket = client socket descriptor
const char FAR* msg = message for server
int
mesglen = length of message
int
flags = control flags (OOB data, etc.)
const struct sockaddr FAR* to = server address
int
tolen = length of address structure
CS431-cotter
31
Windows Sockets
database routines
• gethostbyaddr ( );
– Returns primary Domain Name for given IP address
• gethostbyname ( );
– Returns primary IP address for given Domain Name
• gethostname ( );
– Returns localhost domain name
• getprotobyname ( );
– Returns protocol number for given protocol name
• getservbyname ( );
– Returns well-known port number for given well-known
port name
CS431-cotter
32
Summary
• Foundations for Client / Server
Programming
• Client Side Design Principles
• Windows Sockets Introduction
• Windows Sockets Functions
CS431-cotter
33
Download