1 / 157

Introduction And Course Overview

Introduction And Course Overview. Hochschule Fulda University of Applied Sciences Rumen Stainov e-mail: Rumen.Stainov@informatik.hs-fulda.de Course WEB Page s : http://www2.hs-fulda.de/~stainov/internetworking/index.htm Zimmer: C 106 Tel: +49 661 9640319.

thisbe
Download Presentation

Introduction And Course Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction And Course Overview Hochschule Fulda University of Applied Sciences Rumen Stainov e-mail: Rumen.Stainov@informatik.hs-fulda.de Course WEB Pages: http://www2.hs-fulda.de/~stainov/internetworking/index.htm Zimmer: C 106 Tel: +49 661 9640319 Internetworking 2008

  2. 3 Concurrent Processing In Client-Server Software 3.2 Concurrency In Networks Concurrency  time-sharing and multiprocessing are operating system issues. Internetworking 2008

  3. 3 Concurrent Processing In Client-Server Software 3.3 Concurrency In Servers Internetworking 2008

  4. 3 Concurrent Processing In Client-Server Software 33.4 Terminology And Concepts 33.4.1 The Process ConceptOn a uniprocessor system = apparently simultaneous execution.On a multiprocessor system = simultaneous execution. 33.4.2 Programs vs. ProcessesConventional Program = a piece of code that is executed by one process at a time.Several process can execute a peace of code concurrently each process has an independent copy of the variables. 33.4.3 Procedure CallsIn a procedure-oriented language (like C), when several process execute a peace of code concurrently  each process has an independent run-time stack of procedure activation records. Internetworking 2008

  5. 3 Concurrent Processing In Client-Server Software 3.5 An Example Of Concurrent Process Creation 3.5.1 A Sequential C Example Internetworking 2008

  6. 3 Concurrent Processing In Client-Server Software 3.5.2 A Concurrent Version fork divides the running program into 2 processes executing at the same place the same code. Internetworking 2008

  7. 3 Concurrent Processing In Client-Server Software 3.5.3 Timeslicingattempts to allocate the available processing capacity equally among all available processes. Internetworking 2008

  8. 3 Concurrent Processing In Client-Server Software 3.5.4 Making Processes Diverge In the newly created process, the fork returns 0 In the original process, the fork returns the child-PID a conditional statement can allow the child to execute a different code. Internetworking 2008

  9. 3 Concurrent Processing In Client-Server Software 3.6 Executing New Codeexecveallows to execute an independent, separately-compiled program. 3 arguments: file name of the code, pointer to the arguments, pointer to a list of strings (the environment).execve replaces the code of the currently executing process with the new code  to create a process that executes a new program, a process must call fork and than execve.execveis important for servers that implement different services. 3.7 Context Switching And Protocol Software Designcontext switch = OS stops executing one process and switches to anothercontext switching produces a processing overhead to store the state of the old process and to load the state of the new one. This overhead should be less than the benefits of introducing concurrency into a server. 3.8 Concurrency And Asynchronous I/OConcurrent I/O: select allows a program to ask the OS for ready for use I/O devices, instead of waiting only for one I/O event. Internetworking 2008

  10. 4 Program Interface To Protocols 4.2 Loosely Specified Protocol Software InterfaceApplication interface = interface between the application and the protocol software. TCP/IP operates in a multi-vendor environment . (no vendor’s internal data representation, only API functionality specification)  API is loosely specified; system designers choose the details. 4.2.1 Advantages And Disadvantages + flexibility and tolerance + different APIs - message passing or procedural stile - vendors can design different APIs  applications become less portable In practice - Sockets (Berkeley UNIX) and Transport Layer Interface TLI (AT&T) Internetworking 2008

  11. 4 Program Interface To Protocols 4.3 Interface Functionality Allocate local resources for communication Specify local and remote communication endpoints Server: wait for an incoming connection Send and receive data: determine when data arrives Specify options (e.g. urgent data); handle incoming options Terminate a connection; handle connection termination from the remote side Abort communication; handle error condition or connection abort Release local resources when communication finishes. 4.4 Conceptual Interface SpecificationIs defined by the TCP/IP standards, without specification of data representations and programming details  provides example of one possible interface. Internetworking 2008

  12. 4 Program Interface To Protocols 4.5 System Calls control passes from the application to the system call interface and than to operating system procedures  privileges allowing read and write system data are required. Internetworking 2008

  13. 4 Program Interface To Protocols 4.6 Two Basic Approaches To Network Communication The designer invents entirely new system calls for TCP/IP The designer uses the conventional I/O calls for communications. 4.7 The Basic I/O Functions Available In UNIX Internetworking 2008

  14. 4 Program Interface To Protocols Example with UNIX I/O: int desc; desc = open („filename“, O_RDWR,0) read (desc, buffer,128); close (desc); 44.8 Using UNIX I/O With TCP/IP ·extension of the file descriptors (desc) to be used ·extension of the read and write system calls to work with the new descriptor ·specification of the connection endpoints (IP-address, port); specification of naming primitives ·specification of client/server primitives (initiate transfer, wait for incoming connections, queue management, transfer of datagrams). Internetworking 2008

  15. 5 The Socket Interface 5.2 Berkeley SocketsGoal of the ARPA group: to integrate TCP/IP to UNIX  interface for applications to communicate (API) API: to use the UNIX calls whenever possible and to add new functions Sockets API (first appeared in Berkeley UNIX release 4.1) de facto standard for Internetworking with TCP/IP 5.3 Specifying A Protocol Interface Sockets API provides generalized functions that support network communication using many possible protocols.  TCP/IP represents one possibility for Sockets API (protocol family PF_INET). Sockets functions specify only the type of the protocol service. Internetworking 2008

  16. 5 The Socket Interface 5.4 The Socket Abstraction 5.4.1 Socket Descriptors And File DescriptorsIssue an open call to create a file descriptor. A process maintains a descriptor table to store pointers to internal data structures for already opened files. The OS uses the file or the socket descriptor as an index into the descriptor table. The socket descriptor is created by the socket() call; the file descriptor by the open() call. Internetworking 2008

  17. 5 The Socket Interface 5.4.2 System Data Structures For SocketsAfter calling socket () (with the arguments PF_INET and SOCK_STREAM), a pointer to the corresponding data structure is returned. Internetworking 2008

  18. 5 The Socket Interface 5.4.3 Using Sockets A Server is waiting for incoming connection on a passive socket. A Client initiates a connection on an active socket. 5.5 Specifying An Endpoint AddressA socket does not contain information about the IP addresses or port numbers of either the local or the remote computer.TCP/IP communication endpoint = IP address and port numberA protocol family can choose representation for its address by one or more address families. TCP/IP protocols use a single address family AF_INET. AF_INET and PF_INET have the same numeric value (2), so that one is often used in place of the other. Internetworking 2008

  19. 5 The Socket Interface 5.6 A Generic Address Structure(address family, endpoint address in that family)Generic endpoint address structure in C: struct sockaddr { /* Struc to store most addresses */ u_char sa_len; /* total length */ u_short sa_family; /* address family */ char sa_data[14]; /* up to 14 bytes address */ }; Exception: AF_UNIX address family (named pipe) consists of UNIX path names that can be longer than 14 bytes. Internetworking 2008

  20. 5 The Socket Interface 5.6 A Generic Address Structure A TCP/IP endpoint: struct sockaddr_in { u_char sin_len; /* total length */ u_short sin_family; /* address family(AF_INET) */ u_short sin_port; /* protocol port number */ struct in_addr sin_addr; /* IP address */ char sin_zero[8]; /* unused (set to zero) */ }; or struct sockaddr_in6 { u_char sin6_len; /* length of this struct */ u_char sin6_family; /* AF_INET6 */ u_int16m_t sin6_port; /* transport layer port # */ u_int32m_t sin6_flowinfo; /* IPv6 flow information + priority*/ struct in6_addr sin6_addr; /* 128-bit IPv6 address */ }; Internetworking 2008

  21. 5 The Socket Interface 5.7 Major System Calls Used With Socketsprimary calls for access to the underlying functionality.utility routines that help the programmer. 5.7.1 The Socket Callsocket () returns a descriptor to the newly created socket.Arguments: the protocol family (e.g. PF_INET, or PF_INET6) and protocol or type (e.g. stream or datagram). 5.7.2 The Connect CallA client calls connect to establish an active connection to a remote server.Argument: The remote endpoint. 5.7.3 The Write CallClients and servers use write to send data across a TCP connection.Arguments: The descriptor of a socket, the address of the data to be sent, length of the data. Internetworking 2008

  22. 5 The Socket Interface 5.7.4 The Read CallClients and servers use read to receive (blocking) data across a TCP connection or to receive a datagram sent by UDP.Arguments: The descriptor of a socket, the address of a buffer, length of the buffer. 5.7.5 The Close CallDeallocation of a socket (When several processes share the socket the reference count will be decremented; deallocation when reference count =0)Argument: The descriptor of a socket. 5.7.6 The Bind CallAn application calls bind to specify the local endpoint.Arguments: The descriptor of a socket and the endpoint (in sockaddr_in or sockaddr_in6). 5.7.7 The Listen CallA connection-oriented server calls listen to place the socket in passive mode and makes it ready to accept incoming connections..Argument: Specifies the size of the queue to be used for that socket. Internetworking 2008

  23. 5 The Socket Interface 5.7.8 The Accept CallA Server calls accept to extract the next incoming connection request.Argument: The descriptor of the socket from which the request should be accepted. 5.7.9 Summary Of Socket Calls Used With TCP Internetworking 2008

  24. 5 The Socket Interface 5.8 Utility Routines For Integer Conversionnetwork byte order = integers with the most significant byte first.Conversion routines: (1) htons (host to network short) and ntohs (network to host short) (2) htonl and ntohl 5.9 Using Socket Calls In A Program Internetworking 2008

  25. 5 The Socket Interface 5.10 Symbolic Constants For Socket Call ParametersThe predefined symbolic constants and data structure declarations with the sockets are defined in: #include <sys/types.h>#include <sys/socket.h> Internetworking 2008

  26. Compatibility with IPv4 Applications • Requirements • Systems must continue to support PF_INET sockets and the sockaddr_in address structure. • Applications should be able to hold a combination of IPv4/TCP, IPv4/UDP, IPv6/TCP and IPv6/UDP sockets simultaneously within the same process. • Applications using the original API should continue to operate as they did on systems supporting only IPv4. That is, they should continue to interoperate with IPv4 nodes. Internetworking 2008

  27. Compatibility with IPv4 Applications The ability for IPv6 applications to interoperate with IPv4 applications requires IPv4-mapped IPv6 address format. It allows the IPv4 address of an IPv4 node to be represented as an IPv6 address with a fixed prefix ::FFFF:<IPv4-address> Mapping of IPv4 addresses into IPv6 from the application layer: sendto(s,buffer,sizeof(buffer),0,&clientadr6,sizeof(clientadr6)) These IPv4-mapped IPv6 addresses are often generated automatically by thegethostbyname() function when the specified host has only IPv4 addresses. Mapping of IPv4 addresses into IPv6 from the network: receivefrom(s,buffer,sizeof(buffer),0,&clientadr6,sizeof(clientadr6)) This mapping is transparent, so no knowledge of the socket type is necessarily. If required o know which type of node a application interoperating with, IN6_IS_ADDR_V4MAPPED() can be used. Internetworking 2008

  28. Compatibility with IPv4 Applications Example with IPv6 Internetworking 2008

  29. Converting an IPv4 socket to an IPv6 socket Unix allows open sockets to be passed between processes via the fork () or exec() calls and other means. Thus it is possible for an application using the original API to pass an open PF_INET socket to an application that is expecting to receive a PF_INET6 socket. An IPv6 application that is about to pass an open PF_INET6 socket to a program that is not be IPv6 capable can "downgrade" the socket to PF_INET before calling exec(). (all addresses already associated with the IPv6 socket must be IPv4-mapped IPv6 addresses). For example, to convert a PF_INET6 socket to PF_INET, a program would call: int addrform = PF_INET; if (setsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, sizeof(addrform)) == -1) perror("setsockopt IPV6_ADDRFORM"); Internetworking 2008

  30. Converting an IPv4 socket to an IPv6 socket An application may use IPV6_ADDRFORM with getsockopt() to learn whether an open socket is a PF_INET of PF_INET6 socket. For example: int addrform; size_t len = sizeof(addrform); if (getsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, &len) == -1) perror("getsockopt IPV6_ADDRFORM"); else if (addrform == PF_INET) printf("This is an IPv4 socket.\n"); else if (addrform == PF_INET6) printf("This is an IPv6 socket.\n"); else printf("This system is broken.\n"); Internetworking 2008

  31. Converting an IPv4 socket to an IPv6 socket An application may use IPV6_ADDRFORM with getsockopt() to learn whether an open socket is a PF_INET of PF_INET6 socket. For example: int addrform; size_t len = sizeof(addrform); if (getsockopt(s, IPPROTO_IPV6, IPV6_ADDRFORM, (char *) &addrform, &len) == -1) perror("getsockopt IPV6_ADDRFORM"); else if (addrform == PF_INET) printf("This is an IPv4 socket.\n"); else if (addrform == PF_INET6) printf("This is an IPv6 socket.\n"); else printf("This system is broken.\n"); Internetworking 2008

  32. 6 Algorithms And Issues In Client Software Design 6.2 Learning Algorithms Instead Of DetailsProgrammer’s approach: remember the possible ways in which programs can interact across a network and understand the trade-offs of each possible design. 6.3 Client ArchitectureA client is simpler than a server: It does not explicitly handle concurrent interactions with multiple servers.It does not require special privilege in accessing ports or resources.It does not need to enforce protection.A client can use TCP or UDP 6.4 Identifying The Location Of A Server have the server’s domain name or IP address as a constant  require the user to identify the server  obtain the server address from a file use a naming protocol to find an appropriate server Internetworking 2008

  33. 6 Algorithms And Issues In Client Software Design 6.5 Parsing An Address Argument Domain name of a computer: urd.informatik.hs-fulda.decorresponds to the IP address (dotted decimal notation):193.174.26.41A fully parameterized client software specifies the port of the service:urd.informatik.hs-fulda.de smtpor as a single argument: urd.informatik.hs-fulda.de:smtp 6.6 Looking Up A Domain NameA client specifies the IP address of the server using the structure sockaddr_in by converting the domain name or dotted decimal notation to binary:inet_addr - An ASCII string with the dotted decimal notation is converted to binary.gethostbyname - It takes an ASCII string with the domain name and returns a pointer to the structure hostent with the binary IP address.inet_pton() - An IPv4 or IPv6 alphanumeric address into binary (network order).inet_ntop() - An binary IPv4 or IPv6 address into alphanumeric one.gethostbyname - It takes an ASCII string with the domain name and returns a pointer to the structure hostent with the binary IPv4 or IPv6 address.gethostbyname2(myname, adrtype) - same as above but for IPv6 tailored.gethostbyaddr - It takes binary IPv4 or IPv6 address and returns a string. Internetworking 2008

  34. 6 Algorithms And Issues In Client Software Design +------------------+---------------------------------------------------+ | | RES_USE_INET6 option | | +-------------------------+-------------------------+ | | off | on | +------------------+-------------------------+-------------------------+ | |Search for A records. |Search for AAAA records. | | gethostbyname | If found, return IPv4 | If found, return IPv6 | | (host) | addresses (h_length=4). | addresses (h_length=16).| | | Else error. | Else search for A | | | | records. If found, | | |Provides backward | return IPv4-mapped IPv6 | | | compatibility with all | addresses (h_length=16).| | | existing IPv4 appls. | Else error. | +------------------+-------------------------+-------------------------+ | |Search for A records. |Search for A records. | | gethostbyname2 | If found, return IPv4 | If found, return | | (host, AF_INET) | addresses (h_length=4). | IPv4-mapped IPv6 | | | Else error. | addresses (h_length=16).| | | | Else error. | +------------------+-------------------------+-------------------------+ | |Search for AAAA records. |Search for AAAA records. | | gethostbyname2 | If found, return IPv6 | If found, return IPv6 | | (host, AF_INET6) | addresses (h_length=16).| addresses (h_length=16).| | | Else error. | Else error. | +------------------+-------------------------+-------------------------+ Internetworking 2008

  35. 6 Algorithms And Issues In Client Software Design 6.6 Looking Up A Domain Namestruct 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 */};#define h_addr h_addr_list[0] /* for backward compat */ Example:struct hostent *hptr;char *examplenam = „urd.informatik.hs-fulda.de“;if ( hptr = gethostbyname ( examplenam ) ) { /* IP address is now in hptr->h_addr */} else { /* error in name - handle it */ } switch (hptr->h_addrtype) { case AF_INET: # ifdef AF_INET6 case AF_INET6: ..... Internetworking 2008

  36. 6 Algorithms And Issues In Client Software Design 6.7 Looking Up A Well-Known Port By Namegetservbyname takes two arguments: an ASCII string with the service name and a string with the protocol being used. It returns a pointer to the structure servent with the port for this service.struct servent {char * s_name; /* official service name */char ** s_aliases; /* alias list */int s_port; /* port # */char * s_proto; /* protocol to use */}; Example:struct servent *sptr; if ( sptr = getservbyname ( „smtp“,“tcp“ ) ) { /* port # is now in sptr->s_port */} else { /* error occurred - handle it */ } Internetworking 2008

  37. 6 Algorithms And Issues In Client Software Design 6.8 Port Numbers And Network Byte Ordergetservbyname returns the protocol port in network byte order as required in the sockaddr_in. The internal (local) byte order of the computer may be different. 6.9 Looking Up A Protocol By Namegetprotobyname takes a protocol name and returns a pointer to the structure protoent including an integer constant assigned to that protocol.struct protoent {char *p_name; /* official protocol name */char **p_aliases; /* alias list */int p_proto; /* protocol # */}; Example:struct protoent *pptr; if ( pptr = getprotobyname ( “udp“ ) ) { /* official protocol # is now in pptr->p_proto */} else { /* error occurred - handle it */ } Internetworking 2008

  38. 6 Algorithms And Issues In Client Software Design 6.10 The TCP Client Algorithm Internetworking 2008

  39. 6 Algorithms And Issues In Client Software Design 6.11 Allocating A Socket Clients that use TCP:#include <sys/types.h>#include <sys/socket.h>int s; /* socket descriptor */s = socket (PF_INET, SOCK_STREAM, 0); or s = socket (PF_INET6, SOCK_STREAM, 0); 6.12 Choosing A Local Protocol Port NumberThe client chooses the local port arbitrary as long as it does not conflict with the well-known ports or with the ports used by other processes. Internetworking 2008

  40. 6 Algorithms And Issues In Client Software Design 6.13 A Fundamental Problem In Choosing A Local IP AddressProblem: In a computer with multiple network interfaces (IP addresses). Computer’s IP can select an arbitrary interface to route the traffic. Choosing the correct local IP address requires the application to interact with the IP routing software. Solution: TCP client software usually leaves the local endpoint address unfilled, and allows TCP/IP software to select the correct local IP address and unused port automatically. 6.14 Connecting A TCP Socket To A Serverconnect () allows a TCP client to initiate a connection (TCP 3-way handshake). The call does not return until the connection has been established (return code 0) or TCP reaches the timeout and fails (return code -1)retcode = connect (s, remaddr, remaddrlen) (1) tests that socket s is valid and not already connected, (2) fills the remote endpoint into a data structure of type sockaddr_in or sockaddr_in6 addressed by remaddr, (3) chooses the local endpoint, if necessary, (4) initiates a TCP connection and waits. Internetworking 2008

  41. 6 Algorithms And Issues In Client Software Design 6.15 Communicating With The Server Using TCPThe Client writes a request over a TCP connection and reads a response: Internetworking 2008

  42. 6 Algorithms And Issues In Client Software Design 6.16 Reading A Response From A TCP ConnectionThe single write call above expects a message length less than 120 bytes. The correspondent read must be repeated as long as read returns data. The sender does not guarantee to deliver the data in the same grouping as they are written. TCP may choose to break a data block into pieces or to accumulate many bytes in its output buffer before sending the segment (e.g. to fill the desired MSS). 6.17 Closing A TCP Connection 6.17.1 The Need For Partial CloseAn application can terminate the connection gracefully and after coordination with the remote side deallocate the socket. Before terminating the connection an application must know that all data has arrived from the remote side. 6.17.2 A Partial Close OperationShut down a TCP connection in one direction without deallocating the socket:errcode = shutdown (s, direction);A client issues a partial close after its last request. The server then closes the connection after its last response. Internetworking 2008

  43. 6 Algorithms And Issues In Client Software Design 6.18 Programming A UDP Client Internetworking 2008

  44. 6 Algorithms And Issues In Client Software Design 6.19 Connected And Unconnected UDP SocketsUDP can be used in one of two modes: connected or unconnected. Connected mode = a connect() call specifies a remote endpoint.Advantage: the remote endpoint is specified once, no matter how many datagrams are sent Unconnected mode = the endpoint is specified in each message separatelyAdvantage: flexibility - the client can decide which server to connect. 6.20 Using Connect With UDPA connect() of a socket of type SOCK_DGRAM records the remote endpoint address in the socket data structure. A successful connect call does not mean that the remote endpoint is valid or that the server is available. 6.21 Communicating With A Server Using UDPA connected UDP socket uses write() to sent a complete UDP-Datagram with all the data passed to write. Similarly, a read() call receives all the data from the next UDP-Datagram. Therefore, a UDP application does not need to make re- peated readcalls to obtain a single message. Internetworking 2008

  45. 6 Algorithms And Issues In Client Software Design 6.22 Closing A Socket That Uses UDPA UDP client calls close to a socket. This will release the resources associated with it and will reject further messages that arrive. The remote side will be not informed that the socket is closed. Therefore, the local side must know how long to retain a socket before closing it. 6.23 Partial Close For UDPShutdown with a connected UDP socket stops further transmission in a given direction. The remote side will not receive any indication that the communication has ceased. 6.24 A Warning About UDP UnreliabilityClient Software using UDP sockets must explicitly implement packet sequencing, acknowledgments, time-outs and retransmission. Designing correct, reliable and efficient protocols in Internet requires considerable expertise. Internetworking 2008

  46. 7 Example Client Software 7.2 The Importance Of Small Examples Small program size highlights fundamental algorithms and techniques.Gathering intuition about the relative size of services and their number (especially important for the need of multiprotocol and multiservice design). 7.3 Hiding Details Modular programs are easier to understand, debug and modify than monolithic programs.By choosing the procedures carefully one can make a program easier to port to new computer environment. 7.4 An Example Procedure Library For Client Programs Tasks for establishing connectivity with a server: Choose a protocol (UDP or TCP), look up the server’s machine name, look up and map the service into a port number, allocate a socket and connect it.Hiding details: Choose an abstraction, write the code once, place it in a procedure, and call the procedure from each client program (share code among applications). Internetworking 2008

  47. 7 Example Client Software 7.4 An Example Procedure Library For Client Programs Procedure library with high-level operations (abstractions):socket = connectTCP(mashine, service)socket = connectUDP(mashine, service) 7.5 Implementation Of ConnectTCP New Abstraction: Allocate a socket and fill in basic information (connectsock)/* connectTCP.c - connectTCP */ intconnectsock(const char *host, const char *service, const char *transport); /*------------------------------------------------------------------------ * connectTCP - connect to a specified TCP service on a specified host *------------------------------------------------------------------------ */ int connectTCP(const char *host, const char *service ) /* * Arguments: * host - name of host to which connection is desired * service - service associated with the desired port */ { return connectsock( host, service, "tcp"); } Internetworking 2008

  48. 7 Example Client Software 7.6 Implementation Of ConnectUDP /* connectUDP.c - connectUDP */intconnectsock(const char *host, const char *service, const char *transport); /*---------------------------------------------------------------------- * connectUDP - connect to a specified UDP service on a specified host *----------------------------------------------------------------------*/ int connectUDP(const char *host, const char *service ) /* * Arguments: * host - name of host to which connection is desired * service - service associated with the desired port */ { return connectsock(host, service, "udp"); } Internetworking 2008

  49. 7 Example Client Software 7.7 A Procedure That Forms Connections (connectsock.c - connectsock) /* connectsock.c - connectsock */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <string.h> #include <stdlib.h> #ifndef INADDR_NONE #define INADDR_NONE 0xffffffff #endif /* INADDR_NONE */ extern int errno; int errexit(const char *format, ...); /*------------------------------------------------------------------------ * connectsock - allocate & connect a socket using TCP or UDP *------------------------------------------------------------------------ */ int connectsock(const char *host, const char *service, const char *transport /* * Arguments: * host - name of host to which connection is desired * service - service associated with the desired port * transport - name of transport protocol to use: tcp or udp */ { struct hostent*phe; /* pointer to hostinformation entry*/ struct servent*pse; /* pointer toservice information entry */ struct protoent *ppe; /* pointer to protocol information entry*/ struct sockaddr_in sin; /* an Internet endpoint address */ ints, type; /* socket descriptor and socket type */   memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; Internetworking 2008

  50. 7 Example Client Software 7.7 A Procedure That Forms Connections (connectsock.c – connectsock) /* Map service name to port number */ if ( pse = getservbyname(service, transport) ) sin.sin_port = pse->s_port; else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0 ) errexit("can't get \"%s\" service entry\n", service); /* Map host name to IP address, allowing for dotted decimal */ if ( phe = gethostbyname(host) ) memcpy(&sin.sin_addr, phe->h_addr,phe->h_length); else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE ) errexit("can't get \"%s\" host entry\n", host);  /* Map transport protocol name to protocol number */ if ( (ppe = getprotobyname(transport)) == 0) errexit("can't get \"%s\" protocolentry\n",transport); /* Use protocol to choose a socket type */ if (strcmp(transport, "udp") == 0) type = SOCK_DGRAM; else type = SOCK_STREAM; /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s < 0) errexit("can't create socket: %s\n", strerror(errno));  /* Connect the socket */ if (connect(s, (struct sockaddr *)&sin,sizeof(sin)) < 0) Internetworking 2008 errexit("can't connect to %s.%s: %s\n", host, service, strerror(errno)); return s; }

More Related