330 likes | 472 Vues
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
E N D
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
C/S Architecture Application Server Client
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.
2-Tier vs. 3-Tier C/S Design • 3-Tier design extends distribution of application across more levels / platforms. • Client Local Server Enterprise Server Local Database Enterprise Database App. Server Database Server
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
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
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 ( )
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
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.
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.
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.
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
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.
Windows Sockets Introduction • History • Shared Functions • Protocol Specific Functions • Socket Database Routines
Windows SocketsWinsock 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).
Windows Sockets • Actual implementations are system or machine dependent • All implementations should support winsock.h • Implementation support provided through winsock.dll
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
Windows Sockets • Close down access to socket implementation • retvalue = WSACleanup ( void); • retvalue = 0 or SOCKET_ERROR
Windows SocketsCommon 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
Windows SocketsCommon Socket Routines • htonl ( ); htons ( ); • ntohl ( ); ntohs ( ); • socket ( ); • bind ( ); • connect ( ); • select ( ); • closesocket ( ); • shutdown ( );
Windows SocketsCommon 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
Windows SocketsCommon 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
Windows SocketsCommon 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
Windows SocketsCommon 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)
Windows Sockets Protocol Specific Functions TCPUDP accept ( ); recvfrom( ); listen ( ); sendto ( ); recv ( ); send ( );
TCP Connection-OrientedCommunications • 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
TCP Connection-OrientedCommunications • 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....
TCP Connection-OrientedCommunications • 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....
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
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
Windows Socketsdatabase 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
Summary • Foundations for Client / Server Programming • Client Side Design Principles • Windows Sockets Introduction • Windows Sockets Functions