Chapter 3: Client Software Design Chapter 3: Client Software Design Chapter goal: Chapter goal: Algorithm and issues Algorithm and issues Concurrency in client Concurrency in client Client examples Client examples Learning algorithm instead of details Client architecture plication that uses TCP /IP mus Clients are conceptually simpler than servers functions and their exact parameters does not provide Most client does not explicitly handle designed, distributed concurrent interaction with multiple servers Most client executes as a conventional app If the pru how to do it is strang gram should do Most client does not need to enforce protect ams instead of memorizing the tails of a particular interface Identify a server Parsing an address argument Several methods to find a servers IP address and lient can use domain name or ip address to me or IP address as a constant in program: To determine whether the user has specified a name or an address. the client scans the Using a separate protocol to argument s Client sometimes need additional information e toc such as protocol port Building client that accepts a server address as an merinos. purdue edu smtp Single argument: server address merlin.cs, purdue. edussmtp Consistency is important
1 哈工大计算机学院 李全龙 Network Application Development Client Software Design 1 Chapter 3: Client Software Design Chapter goal: Algorithm and issues Concurrency in client Client examples 哈工大计算机学院 李全龙 Network Application Development Client Software Design 2 Chapter 3: Client Software Design Chapter goal: Algorithm and issues Concurrency in client Client examples 哈工大计算机学院 李全龙 Network Application Development Client Software Design 3 Learning algorithm instead of details An application that uses TCP/IP must specify many details about the desired communication. Knowing the low-level details of all possible socket functions and their exact parameters does not provide programmers with an understanding of how to build well-designed, distributed program. General understanding of the functions used for network communication is important. If the programmer knows what the program should do, finding out how to do it is straightforward. Although programmers need to understand the conceptual capabilities of the protocol interface, they should concentrate on learning about ways to structure communicating programs instead of memorizing the details of a particular interface. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 4 Client architecture Clients are conceptually simpler than servers. Most client does not explicitly handle concurrent interaction with multiple servers. Most client executes as a conventional app program. Most client does not need to enforce protections. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 5 Identify a server Several methods to find a server’s IP address and protocol port number: Domain name or IP address as a constant in program; Requiring user to identify; From stable storage; Using a separate protocol to find; Allowing the user to specify a server address when invoking client software makes the client program more general and makes it possible to change server locations. Building client that accepts a server address as an argument makes it easy to build extended versions of the software that use other ways to find the server address. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 6 Parsing an address argument Client can use domain name or IP address to identify the server machine. To determine whether the user has specified a name or an address, the client scans the argument. Client sometimes need additional information, such as protocol port. Two arguments: merlin.cs.purdue.edu smtp Single argument: merlin.cs.purdue.edu:smtp Consistency is important
Looking up a domain name hostent structure A client must specify the address of a server usin structure of stuct hostent oing so means converting an address in dotted mal notation(or domain name)i char FAr·FAB h_lengty: ontains a dotted decimal address and returns the char FAR·FAR· h addr list;P" list of address Function gethostbyname takes an ASCII string that #define h addr h addr_ listlol Fields that contain names and addresses must be lists because among other strings, the hosts IP address in binary The hostent structure is declared in include file Aadr refer to the first location in the host address list A program can use h addres if it were a field of structure Looking up a well-known port by Example for gethostbyname name stuct hostenthptr: Look up the protocol port for specific har' examplenam=“ merlin.cs. purdue. edu” service if(hptr=gethostbyname(example))t ame r IP address Two arguments A string that specifies the desired service Perror in name.handle it'l A string that specifies the protocol being used a Returns a pointer to a structure of type fo ccess lys cell stthtuste otr me returns a pointe servent Returns a NULL pointer. Defined in include file winsock. h servent structure Example stuct servent stuct servent sptr, char FAR s names name char FAR* FAR g aliases: /port for this service " if(sptr=getservbyname("smtp","tcp") char FAR /protocol to use port number is now in sptr>8_port"/ else i Perror occurred-handle it*/
2 哈工大计算机学院 李全龙 Network Application Development Client Software Design 7 Looking up a domain name A client must specify the address of a server using structure of sockaddr_in. Doing so means converting an address in dotted decimal notation (or domain name) into a 32-bit IP address. Function inet_addr takes an ASCII string that contains a dotted decimal address and returns the equivalent IP address in binary. Function gethostbyname takes an ASCII string that contains the domain name for a machine, and returns the address of a hostent structure that contains, among other strings, the host’s IP address in binary. The hostent structure is declared in include file winsock.h. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 8 hostent structure stuct hostent { char FAR* h_name; /*official host name */ char FAR* FAR* h_aliases; /*other aliases */ short h_addrtype; /*address type */ short h_lengty; /*address length */ char FAR* FAR* h_addr_list; /*list of address */ }; #define h_addr h_addr_list[0] Fields that contain names and addresses must be lists because of multiple interfaces. h_addr refer to the first location in the host address list. A program can use h_addr as if it were a field of structure hostent. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 9 Example for gethostbyname stuct hostent *hptr; char *examplenam=“merlin.cs.purdue.edu”; if (hptr=gethostbyname(example)){ /* IP address is now in hptr->h_addr*/ }else { /*error in name-handle it*/ } Successfully call, gethostbyname returns a pointer to a valid hostent struture. Otrerwise, Returns a NULL pointer. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 10 Looking up a well-known port by name Look up the protocol port for specific service. getservbyname Two arguments: A string that specifies the desired service A string that specifies the protocol being used Returns a pointer to a structure of type servent. Defined in include file winsock.h. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 11 servent structure stuct servent { char FAR* s_name; /*official service name */ char FAR* FAR* s_aliases; /*other aliases */ short s_port; /*port for this service */ char FAR* s_proto; /*protocol to use */ }; 哈工大计算机学院 李全龙 Network Application Development Client Software Design 12 Example stuct servent *sptr; if (sptr=getservbyname(“smtp”, “tcp”)){ /* port number is now in sptr->s_port*/ }else { /*error occurred-handle it*/ }
Port numbers and network byte order Looking up a protocol by name getservbyname returns the protocol port getprotobyname returns the address of a for the service in network byte orde structure of type protoent It is sufficient to understand that a Contained in winsock. h. getservbyname returns the port value in exactly the form needed for use in the sockaddr in structure char Far· char FAR'FAR. p aliases /list of aliases allowed " But, the representation may not agree with short official protocol number/ the local machine,'s usual representation. Example The TCP client algorithm stuct protoent*"pptr; ind the IP address and protocol port number if(pptrgetprotobyname("udp")) 2. Allocate a socket /official protocol number is now in pptr>p_proto Specify that the connection needs an arbitrary Perror occurred-handle it"/ Connect the socket to the server s, Communicate with the server using the application-layer protocols Close the connection Choosing a local protocol port Allocating a socket number #include <winsock h The client does not care which port it uses as long as SOCKETs: he port does not conf lict with the ports tl ther applications on the machine are already s=socket(PF_INET, SOCK_STREAM, 0: The port has not been assigned to a well-knowt The choice of a local port that meets the criteria listed above happens as a side effect of the connect call
3 哈工大计算机学院 李全龙 Network Application Development Client Software Design 13 Port numbers and network byte order getservbyname returns the protocol port for the service in network byte order. It is sufficient to understand that getservbyname returns the port value in exactly the form needed for use in the sockaddr_in structure. But, the representation may not agree with the local machine’s usual representation. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 14 Looking up a protocol by name getprotobyname returns the address of a structure of type protoent. Contained in winsock.h. stuct protoent { char FAR* p_name; /*official protocol name */ char FAR* FAR* p_aliases; /*list of aliases allowed*/ short p_proto; /*official protocol number*/ }; 哈工大计算机学院 李全龙 Network Application Development Client Software Design 15 Example stuct protoent *pptr; if (pptr=getprotobyname(“udp”)){ /* official protocol number is now in pptr->p_proto*/ }else { /*error occurred-handle it*/ } 哈工大计算机学院 李全龙 Network Application Development Client Software Design 16 The TCP client algorithm 1. Find the IP address and protocol port number of the server with which communication is desired. 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-layer protocols. 6. Close the connection. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 17 Allocating a socket #include <winsock.h> SOCKET s; s=socket(PF_INET, SOCK_STREAM, 0 ); 哈工大计算机学院 李全龙 Network Application Development Client Software Design 18 Choosing a local protocol port number The client does not care which port it uses as long as : The port does not conflict with the ports that other applications on the machine are already using. The port has not been assigned to a well-known service. The choice of a local port that meets the criteria listed above happens as a sideeffect of the connect call
Choosing a local IP address Choosing a local IP address a client must choose ress as well as a To solve the problem, the socket functions make it possible for an application to leave the local IP address field unfilled and to allow TCP/IP software a The difficulty in choosing an IP address to choose a local IP address automatically at the time the client connects to a serve ations seldom have access to routi Because choosing the correct local IP address software TcP client software usually leaves t equires the application to interact with IP routing network interface over which IP routes the local endpoint address unfilled, and allows TCP/IP software to select the correct local IP address and lipicult and confusing? ing an incorrect address violates the ification an unused local protocol port number automatically. and makes the program Connecting a TCP socket to a Connecting a TCP socket to a server server a connect function connect forces the initial TCP 3-way handshaking Socket is the descriptor for a socket The call to connect does not return until a TCP connection has been established or TCp Remaddr is the address of a structure of reaches timeout threshold and gives up type sockaddr_ in that specifies the remote to which a connection is desired The call returns O if the connection attempt succeeds or socKET ERROR if it fails a Remaddrlen is the length of remaddr Connecting a TCP socket to a Communicating with the server p server using TCP connect performs four Test to ensure that the ed socket is valid Usually request-response interaction. and that it has not already been connected A client call send to transmit each request Fills in the remote endpoint address in the socket and recv to await a response. from the second argumen a For example Chooses a local endpoint address for the connection(IP address and protocol port number) if the socket does not have one Initiates a TcP connection and returns a value to tell the caller whether the connection succeeded
4 哈工大计算机学院 李全龙 Network Application Development Client Software Design 19 Choosing a local IP address A client must choose a local IP address as well as a local port number. Gateways (routers) or multihomed hosts have multiple IP addresses. The difficulty in choosing an IP address arises because the correct choice depends on routing and applications seldom have access to routing information. Problem: the IP source address in an outgoing datagram should match the IP address of the network interface over which IP routes the datagram. Using an incorrect address violates the specification, makes network management difficult and confusing, and makes the program less reliable. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 20 Choosing a local IP address To solve the problem, the socket functions 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. Because choosing the correct local IP address requires the application to interact with IP routing software, TCP client software usually leaves the local endpoint address unfilled, and allows TCP/IP software to select the correct local IP address and an unused local protocol port number automatically. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 21 Connecting a TCP socket to a server connect function connect forces the initial TCP 3-way handshaking. The call to connect does not return until a TCP connection has been established or TCP reaches timeout threshold and gives up. The call returns 0 if the connection attempt succeeds or SOCKET_ERROR if it fails. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 22 Connecting a TCP socket to a server Socket is the descriptor for a socket; Remaddr is the address of a structure of type sockaddr_in that specifies the remote endpoint to which a connection is desired; Remaddrlen is the length of remaddr. connect(socket,remaddr,remaddrlen); 哈工大计算机学院 李全龙 Network Application Development Client Software Design 23 Connecting a TCP socket to a server connect performs four tasks: Test to ensure that the specified socket is valid and that it has not already been connected; Fills in the remote endpoint address in the socket from the second argument; Chooses a local endpoint address for the connection (IP address and protocol port number) if the socket does not have one; Initiates a TCP connection, and returns a value to tell the caller whether the connection succeeded. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 24 Communicating with the server using TCP Usually request-response interaction. A client call send to transmit each request and recv to await a response. For example:
Communicating with the server Reading response from a TCl using TCP connection a TCP is not a block-oriented protocol, is a eqf"request of some /buffer for answer stream-oriented protocol es read *y TCP guarantees to deliver the sequence of bytes intbuflen space left in buffer that the sender writes, but it does not guarantee to deliver them in the same grouping as they were a Because tp does no boundaries, any progr reads from a TCP connection mus while(nl= SOCKET ERROR & nl=o) ared to accept data a few bytes at a nErev(s, bptr buflen, O) This rule holds even if the sending applicatio writes data in large blocks Closing a TCP connection A partial close operation closesocket terminates the connection .n shutdown function takes two parameters gracefully and deallocate the socket. Closing a connection is not simple because of Direction errcode= shutdown( s, direction Closing a connection usually requires coordination among the client and server further input is allowed To solve the connection shutdown problem 1-no further output is allowed most implementations of the socket interface include an additional primitive that permits pplication to shut down a TCP connection in Advantage: end-of-file si one direction arbitrary amounts of nformation in resporse to a request, Programming a UDP client Connected and unconnected UDP algorithm socke ts Find the IP address and protocol port number basic UDP socket mode: of the server with which communication is Specify machine Communicate with the server using the nt 4.Specify the server which messages to UDP sockets can be connected, making it conventional to interact with a specific server, or they can be connected, making it necessary for the application 6. Close the connection to specify the servers address each time it sends a
5 哈工大计算机学院 李全龙 Network Application Development Client Software Design 25 Communicating with the server using TCP #define BLEN 120 /*buffer length to use */ char *req=“request of some sort”; char buf[BLEN]; /*buffer for answer */ char *bptr; /*pointer to buffer */ int n; /*number of bytes read */ int buflen; /*space left in buffer */ Bptr=buf; buflen=BLEN; /*send request*/ send(s,req,strlen(req),0); /*read response (may come in many pieces)*/ n=recv(s,bptr,buflen,0); while(n!=SOCKET_ERROR && n!=0){ bptr+=n; buflen-=n; n=recv(s,bptr,buflen,0); } 哈工大计算机学院 李全龙 Network Application Development Client Software Design 26 Reading response from a TCP connection TCP is not a block-oriented protocol, is a stream-oriented protocol. TCP guarantees to deliver the sequence of bytes that the sender writes, but it does not guarantee to deliver them in the same grouping as they were wirten. Because TCP does not preserve record boundaries, any program that reads from a TCP connection must be prepared to accept data a few bytes at a time. This rule holds even if the sending application writes data in large blocks. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 27 Closing a TCP connection closesocket terminates the connection gracefully and deallocate the socket. Closing a connection is not simple because of full-duplex. Closing a connection usually requires coordination among the client and server. To solve the connection shutdown problem, most implementations of the socket interface include an additional primitive that permits application to shut down a TCP connection in one direction. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 28 A partial close operation shutdown function takes two parameters: A socket descriptor Direction errcode = shutdown( s, direction); Integer: 0-no further input is allowed; 1-no further output is allowed; 2-both direction. Advantage: end-of-file signal. The partial close mechanism removes ambiguity for application protocols that transmit arbitrary amounts of information in response to a request. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 29 Programming a UDP clientalgorithm 1. Find the IP address and protocol port number of the server with which communication is desired. 2. Allocate a socket. 3. Specify that the communication needs an arbitrary, unused protocol port on the local machine, and allow UDP to choose one. 4. Specify the server which messages to sent. 5. Communicate with the server using the application-layer protocol. 6. Close the connection. 哈工大计算机学院 李全龙 Network Application Development Client Software Design 30 Connected and unconnected UDP sockets Two basic UDP socket mode: Connected Unconnected Advantages: Connected: the application need specify the server only once. Unconnected: flexibility, communicate with different servers. UDP sockets can be connected, making it conventional to interact with a specific server, or they can be unconnected, making it necessary for the application to specify the server’s address each time it sends a message