Sunteți pe pagina 1din 54

Unit I

Elementary TCP Sockets

UNIT I
APPLICATION DEVELOPMENT
TCP Echo Server TCP Echo Client Posix Signal handling Server with multiple clients boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and reboots, Server Shutdown I/O multiplexing I/O Models select function shutdown function TCP echo Server (with multiplexing) poll function TCP echo Client (with Multiplexing)
1.1 INTRODUCTION TO SOCKET PROGRAMMING

Computer Network: The term computer network means an interconnected collection of autonomous computers by single technology, capable of exchanging information. Two computers are said to be interconnected if they are able to exchange information. The connection need not be a copper wire; laser, microwaves and communication satellites can also be used. This connection makes distance between users insignificant in sharing resources and information. Most network applications can be divided into two pieces namely Client and Server (Figure 1.1). Common examples in the TCP/IP world are Web Clients (browsers) and Web Servers, and the FTP and TELNET clients and servers.

Figure 1.1 Network Application Client and Server

Client/Server describes the relationship between two computer programs in which one program, the client, makes a service request from another program, the server, which fulfills the request. Although the Client/Server idea can be used by programs within a single computer, it is a more important idea in a network. In a network, the Client/Server model provides a convenient way to interconnect programs that are distributed efficiently across different locations. Computer transactions using the Client/Server model are very common. For example, to check our bank
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

account from our computer, a client program in our computer forwards our request to a server program at the bank. That program may in turn forward the request to its own client program that sends a request to a database server at another bank computer to retrieve our account balance. The balance is returned back to the bank data client, which in turn serves it back to the client in our personal computer, which displays the information. A server can handle single or multiple clients at the same time

Figure 1.2 Client and Server are on the Same Ethernet

The Client / Server Model It is possible for two network applications to begin simultaneously, but it is impractical to require it. Therefore, it makes sense to design communicating network applications to perform complementary network operations in sequence, rather than simultaneously. The server executes first and waits to receive; the client executes second and sends the first network packet to the server. After initial contact, either the client or the server is capable of sending and receiving data. TCP/IP based protocols use an abstract identifier called a socket. A socket is an end point for communication. The application programs request the operating system to create a socket when there is a need for any resource from the network. The system returns a small integer that the application program uses to refer the newly created socket. The port number concatenated with the IP address form a socket.
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

The sequence of system calls that are invoked in order to establish communication between the hosts is shown in the Figure 1.3

Figure 1.3 Outline of a client-server network interaction

The primary socket calls are socket( ) : bind( ) : listen( ) : accept( ) : connect( ) read( ) : write( ) : close( ) : To create a new socket and return its descriptor. To associate a socket with a port and address To establish queue for connection requests To accept a connection request : To initiate a connection to a remote host To read data from a socket descriptor To write data to a socket descriptor To close a socket descriptor

A socket type is uniquely determined by a triple <domain, type, protocol>. The sockets are primarily categorized as Stream socket, Datagram socket and Raw socket. Figure 1.4 shows the various layers of the TCP/IP model. The interface: 1 refers to the interface of Stream socket, 2 refer to the interface of Datagram socket and 3 refer to Raw socket.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

Figure 1.4 Layers of TCP/IP Model

1.2 OVERVIEW OF TCP/IP PROTOCOL Development of Internet & TCP/IP First proposal for ARPANET military & govt research contracted to Bolt, Beranek & Newman ARPANET enters regular use 1973/4 redesign of lower level protocols leads to TCP/IP Berkeley TCP/IP implementation for 4.2BSD public domain code 1980s rapid growth of NSFNET broad academic use 1990s WWW and public access to the Internet The Internet Now Growing commercialization of the Internet 50,000 networks 6 million hosts 30 million users WWW dominating Internet growth

The TCP/IP protocol suite allows computers of all sizes, from many different computer vendors, running totally different operating systems, to communicate with each other. It is quite amazing because its use has far exceeded its original estimates. What started in the late 1960s as a government-financed research project into packet switching networks has, in the 1990s, turned into the most widely used form of networking between computers. It is truly an open system in that the definition of the protocol suite and many of its implementations are publicly available at little or no charge. It forms the basis for what is called the worldwide
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

Internet, or the Internet, a wide area network (WAN) of more than one million computers that literally spans the globe. Layering Networking protocols are normally developed in layers, with each layer responsible for a different facet of the communications. A protocol suite, such as TCP/IP, is the combination of different protocols at various layers. TCP/IP is normally considered to be a 4-layer system, as shown in the Figure 1.5. Application Transport Internet Telnet, FTP, SNMP, SMTP, TFTP DNS TCP, UDP IP, ICMP, IGMP,ARP, RARP

Network Access (NIC) LAN, X.25


Figure 1.5 The four layers of the TCP/IP protocol suite

Each layer has a different responsibility. The network access layer also called as host-to-network layer. This layer includes the device driver in the operating system and the corresponding network interface card in the computer. The functions of this layer are mapping IP addresses to physical hardware addresses and encapsulating IP packets into frames. Together they handle all the hardware details of physically interfacing with the cable. The technologies and the protocols that are used in the network access layer are 2. Ethernet Fast Ethernet SLIP and PPP FDDI ATM, Frame relay and SMDS ARP Proxy ARP RARP

The Internet layer handles the movement of packets around the network. Best path determination and packet switching occur at this layer. The protocols that work at the Internet layer are Internet Protocol (IP) Internet Control Message Protocol (ICMP) Internet Group Management Protocol (IGMP)

3.

The transport layer provides a flow of data between two hosts, for the application layer above. The transport layer services are segmenting, sending segments, establishing end-to-end operations, flow control and reliability. In the TCP/IP protocol suite there are two vastly

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

different transport protocols namely Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). TCP supports all the services mentioned above, whereas UDP supports only segmenting and sending the segments. 4. The application layer of TCP/IP includes OSI session, presentation and application layer details. This layer handles high level protocols and issues of representation, encoding and dialog control. There are many common TCP/IP applications that almost every implementation provides: Terminal Emulation (Telnet) File Transfer Protocol (FTP) Trivial File Transfer Protocol (TFTP) Simple Mail Transfer protocol (SMTP) Simple Network Management Protocol (SNMP) Domain Name System (DNS)

The Internet Protocol suite - like many protocol suites, can be viewed as a set of layers. Each layer solves a set of problems involving the transmission of data, and provides a well-defined service to the upper layer protocols based on using services from some lower layers. Upper layers are logically closer to the user and deal with more abstract data, relying on lower layer protocols to translate data into forms that can eventually be physically transmitted. The TCP/IP reference model consists of four layers. The Internet does not strictly obey the OSI model but rather merges several of the protocols layers together. 1.2.1 BRIEF OVERVIEW OF THE TCP/IP PROTOCOLS

Although the protocol suite is called "TCP/IP," there are more members of this family than just TCP and IP. Figure 1.6 shows an overview of these protocols. Internet Protocol version 4 (IPv4) Internet Protocol version 4 is the fourth iteration of the Internet Protocol (IP) and it is the first version of the protocol to be widely deployed. IPv4 is the dominant network layer protocol. It uses 32-bit addresses. Internet Protocol version 6 (IPv6) Internet Protocol version 6 is an Internet Layer protocol for packet-switched internetworks. IPv4 is currently the dominant Internet Protocol version, and was the first to receive widespread use. The Internet Engineering Task Force (IETF) has designated IPv6 as the successor to version 4 for general use on the Internet.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

Features and differences from IPv4 To a great extent, IPv6 is a conservative extension of IPv4. Most transport- and application-layer protocols need little or no change to work over IPv6 Larger address space : IPv6 features a larger address space than that of IPv4: addresses in IPv6 are 128 bits long versus 32 bits in IPv4.

Figure 1.6-Overview of TCP/IP Protocols

Address scopes :IPv6 introduces the concept of address scopes. An address scope defines the "region" or "span" where an address can be defined as a unique identifier of an interface. These spans are the local link, the site network, and the global network, corresponding to link-local, site-local or unique local unicast, and global addresses Stateless address auto configuration : IPv6 hosts can configure themselves automatically when connected to a routed IPv6 network using ICMPv6 router discovery messages. When first connected to a network, a host sends a linklocal multicast router solicitation request for its configuration parameters; if configured suitably, routers respond to such a request with a router advertisement packet that contains network-layer configuration parameters.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

Multicast :Multicast, the ability to send a single packet to multiple destinations, is part of the base specification in IPv6. This is unlike IPv4, where it is optional (but usually implemented). IPv6 does not implement broadcast, the ability to send a packet to all hosts on the attached link. The same effect can be achieved by sending a packet to the link-local all hosts multicast group. Mandatory network layer security: Internet Protocol Security (IPsec), the protocol for IP encryption and authentication, forms an integral part of the base protocol suite in IPv6. IP packet header support is mandatory in IPv6; this is unlike IPv4, where it is optional Router handling for delivery prioritization : The IPv6 packet header contains a new "Flow Label" field for prioritizing packet delivery by routers. The Flow Label replaces the "Service Type" field in IPv4. The specific properties and utility of this header field are not well defined at present. Hop-Limit vs. TTL: The Time-to-Live field of IPv4 has been replaced by a HopLimit field. Options Extensibility :IPv4 has a fixed size (40 bytes) of option parameters. In IPv6, options are implemented as additional extension headers after the IPv6 header, which limits their size only by the size of an entire packet. Transmission Control Protocol (TCP) TCP is described in RFC 793. Using TCP, applications on networked hosts can create connections to one another, over which they can exchange streams of data using Stream Sockets. o TCP also provides reliability. When TCP sends data to the other end, it requires an acknowledgment in return. If an acknowledgment is not received, TCP automatically retransmits the data and waits a longer amount of time. o TCP contains algorithms to estimate the round-trip time (RTT) between a client and server dynamically so that it knows how long to wait for an acknowledgment. o TCP also sequences the data by associating a sequence number with every byte that it sends. o TCP provides flow control. TCP always tells its peer exactly how many bytes of data it is willing to accept from the peer at any one time. This is called the advertised window. At any time, the window is the amount of space currently available in the receive buffer, guaranteeing that the sender cannot overflow the receive buffer. o The TCP checks that no bytes are corrupted by using a checksum; one is computed at the sender for each block of data before it is sent, and checked at the receiver. User Datagram Protocol (UDP) UDP is a simple transport-layer protocol. It is described in RFC 768. The application writes a message to a UDP socket, which is then
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

Elementary TCP Sockets

encapsulated in a UDP datagram, which is then further encapsulated as an IP datagram, which is then sent to its destination. There is no guarantee that a UDP datagram will ever reach its final destination, that order will be preserved across the network, or that datagrams arrive only once. The problem that we encounter with network programming using UDP is its lack of reliability. If a datagram reaches its final destination but the checksum detects an error, or if the datagram is dropped in the network, it is not delivered to the UDP socket and is not automatically retransmitted. If we want to be certain that a datagram reaches its destination, we can build lots of features into our application: acknowledgments from the other end, timeouts, retransmissions, and the like. Each UDP datagram has a length. The length of a datagram is passed to the receiving application along with the data. We have already mentioned that TCP is a byte-stream protocol, without any record boundaries at all, which differs from UDP. We also say that UDP provides a connectionless service, as there need not be any long-term relationship between a UDP client and server. For example, a UDP client can create a socket and send a datagram to a given server and then immediately send another datagram on the same socket to a different server. Similarly, a UDP server can receive several datagrams on a single UDP socket, each from a different client. Internet Control Message Protocol (ICMP) ICMP handles error and control information between routers and hosts. These messages are normally generated by and processed by the TCP/IP networking software itself, not user processes, although we show the ping and traceroute programs, which use ICMP. ICMP is one of the simplest protocols in the TCP/IP protocol suite. As its name suggests, it is a protocol that defines control messages. Various message types are defined in ICMP that allow different types of information to be exchanged. These are usually either generated for the purpose of reporting errors, or for exchanging important information of different sorts that is needed to keep IP operating smoothly. The ICMP is chiefly used by networked computers operating systems to send error messages indicating, for instance, that a requested service is not available or that a host or router could not be reached. ICMP differs in purpose from TCP and UDP in that it is not used to send and receive data between end systems. The Internet Group Management Protocol (IGMP)

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

10

Elementary TCP Sockets

The IGMP is a communications protocol used to manage the membership of Internet Protocol multicast groups. IGMP is used by IP hosts and adjacent multicast routers to establish multicast group memberships. It is an integral part of the IP multicast specification, operating above the network layer. IGMP can be used for online streaming video and gaming, and allows more efficient use of resources when supporting these types of applications. There are three versions of IGMP, as defined by "Request for Comments" (RFC) documents of the Internet Engineering Task Force (IETF). IGMP v1 is defined by RFC 1112, IGMP v2 is defined by RFC 2236 and IGMP v3 is defined by RFC 3376 Address Resolution Protocol (ARP) Address Resolution Protocol (ARP) is the method for finding a host's hardware address when only its Network Layer address is known. ARP is not an IPonly or Ethernet-only protocol; it can be used to resolve many different network-layer protocol addresses to hardware addresses. ARP is primarily used to translate IP addresses to Ethernet MAC addresses. It is also used for IP over other LAN technologies, such as Token Ring, FDDI, or IEEE 802.11, and for IP over ATM. It is not needed on point-to-point networks. Reverse Address Resolution Protocol (RARP) Reverse Address Resolution Protocol (RARP) is a Link layer networking protocol used by a host computer to obtain its IPv4 address given only its link-layer address (such as an Ethernet address). It has been rendered obsolete by Bootstrap Protocol and the modern Dynamic Host Configuration Protocol, which both support a much greater feature set than RARP. It is sometimes used when a diskless node is booting. Internet Control Message Protocol Version 6 (ICMPv6) ICMPv6 or ICMP for IPv6 is a new version of ICMP. ICMPv6 operates in the Internet Layer of the TCP/IP model and performs error reporting for the Internet Protocol, as well as some other diagnostic functions (such as "ping"). ICMPv6 combines the functionality of ICMPv4, IGMP, and ARP. BPF: BSD packet filter. This interface provides access to the data link layer. It is normally found on Berkeley-derived kernels. DLPI: Data Link provider interface. This interface also provides access to the data link layer. It is normally provided with SVR4.

File Transfer Protocol (FTP)

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

11

Elementary TCP Sockets

FTP is a network protocol used to transfer data from one computer to another through a network such as the Internet. FTP is a file transfer protocol for exchanging and manipulating files over a TCP computer network. A FTP client may connect to a FTP server to manipulate files on that server. The objectives of FTP, as outlined by its RFC, are: 1. To promote sharing of files (computer programs and/or data). 2. To encourage indirect or implicit use of remote computers. 3. To shield a user from variations in file storage systems among different hosts. 4. To transfer data reliably, and efficiently. Trivial File Transfer Protocol (TFTP) TFTP is a very simple file transfer protocol, with the functionality of a very basic form of FTP; it was first defined in 1980. It is easy to implement in a very small amount of memory. TFTP is therefore useful for booting computers such as routers which did not have any data storage devices. It is used to transfer small amounts of data between hosts on a network, such as IP phone firmware or operating system images when a remote X Window System terminal or any other thin client boots from a network host or server. The initial stages of some network based installation systems (such as Solaris Jumpstart, Red Hat Kickstart and SUSE AutoYAST) use TFTP to load a basic kernel that performs the actual installation. TELecommunication NETwork (TELNET) Telnet is a network protocol used on the Internet or local area network (LAN) connections. It was developed in 1969. Typically, telnet provides access to a command-line interface on a remote machine. The term telnet also refers to software which implements the client part of the protocol. Telnet clients are available for virtually all platforms. Telnet is a client-server protocol, based on a reliable connection-oriented transport. Typically this protocol is used to establish a connection to TCP port 23. Simple Network Management Protocol (SNMP) SNMP forms part of the internet protocol suite as defined by the Internet Engineering Task Force (IETF). SNMP is used in network management systems to monitor network-attached devices for conditions that warrant administrative attention. It consists of a set of standards for network management, including an Application Layer protocol, a database schema, and a set of data objects. In typical SNMP usage, there are number of systems to be managed, and one or more systems managing them. A software component called an agent runs on each managed system and reports information via SNMP to the managing systems. Simple Network Management Protocol (SMTP)

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

12

Elementary TCP Sockets

SMTP is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks. While electronic mail server software uses SMTP to send and receive mail messages, user-level client mail applications typically only use SMTP for sending messages to a mail server for relaying. For receiving messages, client applications usually use either the Post Office Protocol (POP) or the Internet Message Access Protocol (IMAP) to access their mail box accounts on a mail server. Hypertext Transfer Protocol (HTTP) HTTP is a communications protocol. Its use for retrieving inter-linked text documents (hypertext) led to the establishment of the World Wide Web. HTTP is a request/response standard between a client and a server. A client is the end-user; the server is the web site. The client making a HTTP request - using a web browser, spider, or other end-user tool - is referred to as the user agent. The responding server which stores or creates resources such as HTML files and images - is called the origin server. In between the user agent and origin server may be several intermediaries, such as proxies, gateways, and tunnels. HTTP is not constrained to using TCP/IP and its supporting layers, although this is its most popular application on the Internet. An HTTP client initiates a request. It establishes a Transmission Control Protocol (TCP) connection to a particular port on a host (port 80 by default).An HTTP server listening on that port waits for the client to send a request message. Upon receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is perhaps the requested file, an error message, or some other information. HTTP uses TCP and not UDP because much data must be sent for a webpage, and TCP provides transmission control, presents the data in order, and provides error correction. Each Internet protocol is defined by one or more documents called a Request for Comments (RFC), which are their formal specifications.The term "IPv4/IPv6 host" and "dual-stack host" to denote hosts that support both IPv4 and IPv6. 1.2.2 TCP Connection Establishment and Termination Three-Way Handshake The following scenario occurs when a TCP connection is established: The server must be prepared to accept an incoming connection. This is normally done by calling socket, bind, and listen and is called a passive open. 2. The client issues an active open by calling connect. This causes the client TCP to send a "synchronize" (SYN) segment, which tells the server the client's initial sequence number for the data that the client will send on the connection. Normally, there is no data sent with the SYN; it just contains an IP header, a TCP header, and possible TCP options (which we will talk about shortly). 3. The server must acknowledge (ACK) the client's SYN and the server must also send its own SYN containing the initial sequence number for the data that
1. IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

13

Elementary TCP Sockets

the server will send on the connection. The server sends its SYN and the ACK of the client's SYN in a single segment. 4. The client must acknowledge the server's SYN. The minimum number of packets required for this exchange is three; hence, this is called TCP's three-way handshake. We show the three segments in Figure 1.7.

Figure 1.7 Three-way Handshake

It shows the client's initial sequence number as J and the server's initial sequence number as K. The acknowledgment number in an ACK is the next expected sequence number for the end sending the ACK. Since a SYN occupies one byte of the sequence number space, the acknowledgment number in the ACK of each SYN is the initial sequence number plus one. Similarly, the ACK of each FIN is the sequence number of the FIN plus one.
TCP options

Each SYN can contain TCP options. Commonly used options include the following: MSS option. With this option, the TCP sending the SYN announces its maximum segment size, the maximum amount of data that it is willing to accept in each TCP segment, on this connection. The sending TCP uses the receiver's MSS value as the maximum size of a segment that it sends. Window scale option. Window is the amount of space available in the receive buffer, guaranteeing that the sender cannot overflow the receivers buffer. The maximum window that either TCP can advertise to the other TCP is 65,535, because the corresponding field in the TCP header occupies 16 bits. Timestamp option. This option is needed for high-speed connections to prevent possible data corruption caused by old, delayed, or duplicated segments.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

14

Elementary TCP Sockets

TCP Connection Termination


While it takes three segments to establish a connection, it takes four to terminate a connection. One application calls close first, and we say that this end performs the active close. This end's TCP sends a FIN segment, which means it is finished sending data. 2. The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an end-of-file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection. 3. Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN. 4. The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.
1.

Since a FIN and an ACK are required in each direction, four segments are normally required. We use the qualifier "normally" because in some scenarios, the FIN in Step 1 is sent with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive close and could be combined into one segment. We show these packets in Figure 1.8.

Figure 1.8 TCP Connection Termination

When closing the connection among two parties, A party wishing to close its side of the TCP connection sends a FIN . The other party sends an ACK for the FIN, it receives from other end. And after the other party ACKs, the connection is half-closed. The party which sent the FIN can no longer send data -- but the other party may continue to send data.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

15

Elementary TCP Sockets

It is possible for data to flow from the end doing the passive close to the end doing the active close (between step 2 and step 3). This is called a half-close. To close the TCP stream fully, both sides must send a FIN for "their" half of the connection. Port Numbers At any given time, multiple processes can be using any given transport: UDP, SCTP, or TCP. All three transport layers use 16-bit integer port numbers to differentiate between these processes. When a client wants to contact a server, the client must identify the server with which it wants to communicate. TCP and UDP define a group of well-known ports to identify well-known services. For example, every TCP/IP implementation that supports FTP assigns the well-known port of 21 (decimal) to the FTP server. Trivial File Transfer Protocol (TFTP) servers are assigned the UDP port of 69. Clients, on the other hand, normally use ephemeral ports, that is, short-lived ports. These port numbers are normally assigned automatically by the transport protocol to the client. Clients normally do not care about the value of the ephemeral port; the client just needs to be certain that the ephemeral port is unique on the client host. The transport protocol code guarantees this uniqueness. The Internet Assigned Numbers Authority (IANA) maintains a list of port number assignments. The port numbers are divided into three ranges: The well-known ports: 0 through 1023. These port numbers are controlled and assigned by the IANA. When possible, the same port is assigned to a given service for TCP, UDP, and SCTP. For example, port 80 is assigned for a Web server, for both TCP and UDP, even though all implementations currently use only TCP. 2. The registered ports: 1024 through 49151. These are not controlled by the IANA, but the IANA registers and lists the uses of these ports as a convenience to the community. When possible, the same port is assigned to a given service for both TCP and UDP. 3. The dynamic or private ports, 49152 through 65535. The IANA says nothing about these ports. These are what we call ephemeral ports. (The magic number 49152 is three-fourths of 65536.)
1.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

16

Elementary TCP Sockets

Well known port numbers

Service Echo discard daytime chargen ftp telnet smtp daytime tftp finger http login who

Port number 7 9 13 19 21 23 25 37 69 79 80 513 513

Protocol UDP/TCP UDP/TCP UDP/TCP UDP/TCP TCP TCP TCP UDP/TCP UDP TCP TCP TCP UDP

Description sends back what it receives throws away input returns ASCII time returns characters file transfer remote login email returns binary time trivial file transfer info on users World Wide Web remote login different info on users

1.3 INTRODUCTION TO SOCKETS Socket: Socket can be easily defined as a way to talk to other computers using standard Unix file descriptor. File descriptor is just an integer associated with an open file and it can be a network connection, a terminal or something else. In Unix every I/O actions are done by writing or reading to a file descriptor. In TCP/IP an addressable point that consists of an IP address and a TCP or UDP port member that provides application with access to TCP/IP protocol is called Socket.
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

17

Elementary TCP Sockets

A socket is a communications connection point (endpoint) that you can name and address in a network. The processes that use a socket can reside on the same system or on different systems of different networks. Sockets are useful for both stand-alone and network applications. Sockets allow us to exchange information between processes on the same machine or across a network, distribute work to the most efficient machine, and allow access to centralized data easily. Socket application program interfaces (APIs) are the network standard for TCP/IP. A wide range of operating systems support socket APIs. OS/400 sockets support multiple transport and networking protocols. Socket system functions and the socket network functions are threadsafe. The two values that identify each endpoint, an IP address and a port number, are often called a socket. The socket pair for a TCP connection is the four-tuple that defines the two endpoints of the connection: the local IP address, local port, foreign IP address, and foreign port. A socket pair uniquely identifies every TCP connection on a network. Socket programming shows how to use socket APIs to establish communication links between remote and local processes. Programmers who use Integrated Language Environment (ILE) C can use the information to develop socket applications. We can also code the sockets API from other ILE languages, such as RPG. What are sockets? A socket is an abstraction that represents an endpoint of communication. Most applications that consciously use TCP and UDP do so by creating a socket of the appropriate type and then performing a series of operations on that socket. The operations that can be performed on a socket include control operations (such as associating a port number with the socket, initiating or accepting a connection on the socket, or destroying the socket) data transfer operations (such as writing data through the socket to some other application, or reading data from some other application through the socket) and status operations (such as finding the IP address associated with the socket). Types of Internet Sockets There are many types of sockets. Based on the type of transport layer protocol being used they are categorized as Stream Sockets (SOCK_STREAM) or Datagram Sockets (SOCK_DGRAM). Stream Sockets These sockets are error free; if we send three items A,B,C through the stream socket, they will arrive in the same order A,B,C . They use TCP and hence connection is established between the

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

18

Elementary TCP Sockets

sockets and then the data transfer occurs. Thus these sockets assure the items order. Datagram Sockets These sockets use UDP and hence build a packet with the destination information and send it out. Since they are connectionless there is no need to open a connection as in the case of Stream Sockets. Sockets share the following characteristics: A socket is represented by an integer. That integer is called a socket descriptor. A socket exists as long as the process maintains an open link to the socket. You can name a socket and use it to communicate with other sockets in a communication domain. Sockets perform the communication when the server accepts connections from them, or when it exchanges messages with them. You can create sockets in pairs (only for sockets in the AF_UNIX address family).

The first argument of the socket call is the domain. The following are the domains supported, the Internet Domain Version 4 (AF-INET), the Internet Domain Version 6 (AF_INET6) and the Unix Domain (AF_LOCAL or AF_UNIX). AF_LOCAL sets up a socket connection between two processes on the same host. It is similar to a pipe except that it is full duplex and the two ends of communication do not need to have a common ancestor. The second argument to the socket call is the socket type. The possible socket types are SOCK_STREAM for TCP, SOCK_DGRAM for UDP and SOCK_RAW (raw socket). A raw socket is one that skips the Transport layer completely. The protocol can be specified. If protocol is zero, all packets go to the socket. If protocol is specified, then only packets with that protocol are received. There is no address structure so there is no binding and no port numbers. The connection that a socket provides can be connection-oriented or connectionless. Connection-oriented communication implies that a connection is established, and a dialog between the programs will follow. The program that provides the service (the server program) establishes the available socket which is enabled to accept incoming connection requests. Optionally, the server can assign a name to the service that it supplies which allows clients to identify where to obtain and how to connect to that service. The client of the service (the client program) must request the
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

19

Elementary TCP Sockets

service of the server program. The client does this by connecting to the distinct name or to the attributes associated with the distinct name that the server program has designated. It is similar to dialing a telephone number (an identifier) and making a connection with another party that is offering a service (for example, a plumber). When the receiver of the call (the server, in this example, a plumber) answers the telephone, the connection is established. The plumber verifies that you have reached the correct party, and the connection remains active as long as both parties require it. Connectionless communication implies that no connection is established over which a dialog or data transfer can take place. Instead, the server program designates a name that identifies where to reach it (much like a post office box). If you send a letter to a post office box, you cannot be absolutely sure the receiver got the letter. You may need to wait for a response to your letter. There is no active, real time connection in which data is exchanged. How do sockets work? Sockets are commonly used for client/server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events. In a connection-oriented clientto-server model, the socket on the server process waits for requests from a client. To do this, the server first establishes (binds) an address that clients can use to find the server. When the address is established, the server waits for clients to request a service. The client-to-server data exchange takes place when a client connects to the server through a socket. The server performs the clients request and sends the reply back to the client. As shown in the Figure 1.9, the socket APIs are located in the communications model between the application layer and the transport layer. The socket APIs are not a layer in the communication model. Socket APIs allow applications to interact with the transport or networking layers of the typical communications model. The arrows in the following figure show the position of a socket, and the communication layer that the socket provides. Typically, a network configuration does not allow connections between a secure internal network and a less secure external network. However, you can enable sockets to communicate with server programs that run on a system outside a firewall (a very secure host). Figure 1.10 illustrates the client/server relationship of the sockets API for a connection-oriented protocol.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

20
A p p

Elementary TCP Sockets

li c a t io

c k e t

T r a n s p o T C P , U D

r t L a y e r P , R A W

Figure 1.9 Location of socket APIs in the communication model

Socket flow of events: Connection-oriented server The following sequence of the socket calls provides a description of the graphic. It also describes the relationship between the server and client application in a connection-oriented design. Each set of flows contain links to usage notes on specific APIs. A connection-oriented server uses the following sequence of function calls: 1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM) will be used for this socket.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

21

Elementary TCP Sockets

Figure 1.10 Relationship of Sockets API for Connection-oriented protocol

2. 3.

The setsockopt() function allows the local address to be reused when the server is restarted before the required wait time expires. After the socket descriptor is created, the bind() function gets a unique name for the socket. In this example, the user sets the s_addr to zero, which allows connections to be established from any IPv4 client that specifies port 3005. The listen() allows the server to accept incoming client connections. In this example, the backlog is set to 10. This means that the system will queue 10 incoming connection requests before the system starts rejecting the incoming requests. The server uses the accept() function to accept an incoming connection request. The accept() call will block indefinitely waiting for the incoming connection to arrive. The select() function allows the process to wait for an event to occur and to wake up the process when the event occurs. In this example, the system notifies the process only when data is available to be read. A 30 second timeout is used on this select call.

4.

5.

6.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I 7.

22

Elementary TCP Sockets

The recv() function receives data from the client application. In this example we know that the client will send 250 bytes of data over. Knowing this, we can use the SO_RCVLOWAT socket option and specify that we do not want our recv() to wake up until all 250 bytes of data have arrived. The send() function echoes the data back to the client. The close() function closes any open socket descriptors.

8. 9.

Socket flow of events: Connection-oriented client A connection-orientated client uses the following sequence of function calls: 1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM) will be used for this socket. In the client example program, if the server string that was passed into the inet_addr() function was not a dotted decimal IP address, then it is assumed to be the hostname of the server. In that case, use the gethostbyname() function to retrieve the IP address of the server. After the socket descriptor is received, the connect() function is used to establish a connection to the server. The send() function sends 250 bytes of data to the server. The recv() function waits for the server to echo the 250 bytes of data back. In this example, we know that the server is going to respond with the same 250 bytes that we just sent. In client example, the 250 bytes of the data may arrive in separate packets, so we will use the recv() function over and over until all 250 bytes have arrived. The close() function closes any open socket descriptors.

2.

3. 4. 5.

6.

Connectionless sockets do not establish a connection over which data is transferred. Instead, the server application specifies its name where a client can send requests. Connectionless sockets use User Datagram Protocol (UDP) instead of TCP/IP. A connectionless server and client illustrate the socket APIs that are written for User Datagram Protocol (UDP). Figure 1.11 illustrates the client/server relationship from the socket APIs used in the code examples for a connectionless socket design. Socket flow of events: Connectionless server The following sequence of the socket calls provides a description of the graphic and the following example programs. It also describes the relationship between the server and client application in a connectionless design. Each set of flows contain links to usage notes on specific APIs. If you need more details on the use of a particular API, you can use these links. A connectionless server uses the following sequence of function calls:

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

23

Elementary TCP Sockets

Figure 1.11 Relationship of Sockets API for Connectionless protocol

1.

The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the UDP transport (SOCK_DGRAM) will be used for this socket. After the socket descriptor is created, a bind() function gets a unique name for the socket. In this example, the user sets the s_addr to zero, which means that the UDP port of 3555 will be bound to all IPv4 addresses on the system. The server uses the recvfrom() function to receive that data. The recvfrom() function waits indefinitely for data to arrive. The sendto() function echoes the data back to the client. The close() function ends any open socket descriptors.

2.

3. 4. 5.

Socket flow of events: Connectionless client A connectionless client uses the following sequence of function calls: 1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the UDP transport (SOCK_DGRAM) will be used for this socket. In the client example program, it the server string that was passed into the inet_addr() function was not a dotted decimal IP address, then it is assumed to be the hostname of the server. In that case, use the gethostbyname() function to retrieve the IP address of the server. Use the sendto() function to send the data to the server. Use the recvfrom() function to receive the data back from the server. The close() function ends any open socket descriptors.

2.

3. 4. 5.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

24

Elementary TCP Sockets

1.4 SOCKET ADDRESS STRUCTURES Structure is a data type that is an aggregate, that is, it contains other data types, which are grouped together into a single user-defined type. Structures are used in socket programming to hold information about the address. The first structure is struct sockaddr that holds socket information. struct in_addr { in_addr_t s_addr ; // 32-bit, IPv4 network byte order (unsigned) } struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_ port ; struct in_addr sin_addr; char sin _zero[8]; } sockaddr_in is a parallel structure to deal with struct sockaddr for IPv4 addresses. sin_port contains the port number and must be in Network Byte Order. sin_family corresponds to sa_family (in a structure sockaddr) and contains the type of address family (AF_INET for IPv4). As sin_port also sin_family must be in Network Byte Order. sin_addr represents Internet address (for IPv4). sin_zero is included to pad the structure to the length of a struct sockaddr and should be set to all zero using the bzero() or memset() functions. A socket address structure always passed by reference when passed as an argument to any socket function Generic Socket Address Structure A socket address structures is always passed by reference when passed as an argument to any socket functions. But any socket function that takes one of these pointers as an argument must deal with socket address structures from any of the supported protocol families. A problem arises in how to declare the type of pointer that is passed. With ANSI C, the solution is simple: void * is the generic pointer type. But, the socket functions predate ANSI C and the solution chosen in 1982 was to define a generic socket address structure in the <sys/socket.h> header as given below
IT2351-Network Programming & Management Dept. of IT/RMKEC

/*unsigned 8 bit integer*/ /*AF_INET*/ /* 16 bit TCP or UDP port number */ /* 32 bit IPv4 address */ /*unused*/

Unit I

25

Elementary TCP Sockets

The generic socket address structure: sockaddr. struct sockaddr { uint8_t char }; IPv6 Socket Address Structure struct in6_addr { uint8_t s6_addr[16]; // 128bit IPv6 address (Network Byte Ordered.) }; struct sockaddr_in6 { unit8_t sin6_len; //length of this struct sa_family_t sin6_family; //AF_INET6 (8bit) in_port_t sin6_port; //transport layer port # (N.B.O.) uint32_t sin6_flowinfo; //IPv6 flow information (N.B.O.) struct in6_addr sin6_addr; // IPv6 address }; The SIN6_LEN constant must be defined if the system supports the length member for socket address structures. The IPv6 family is AF_INET6, whereas the IPv4 family is AF_INET. The members in this structure are ordered so that if the sockaddr_in6 structure is 64-bit aligned, so is the 128-bit sin6_addr member. On some 64-bit processors, data accesses of 64-bit values are optimized if stored on a 64-bit boundary. The sin6_flowinfo member is divided into three fields: The low-order 24 bits are the flow label, The next 4 bits are the priority, The high-order 12 bits are reserved sa_len; /* address family: AF_xxx value */ /* protocol-specific address */ sa_data[14]; sa_family_t sa_family;

The use of the flow label field is still a research topic.

1.4.1 Value Result Argument


We mentioned that when a socket address structure is passed to any socket function, it is always passed by reference. That is, a pointer to the structure is passed. The length of the structure is also passed as an argument. But the way in which the
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

26

Elementary TCP Sockets

length is passed depends on which direction the structure is being passed: from the process to the kernel, or vice versa. 1. Three functions - bind, connect, and sendto - pass a socket address structure from the process to the kernel. One argument to these three functions is the pointer to the socket address structure and another argument is the integer size of the structure, as in struct sockaddr_in serv; /* fill in serv{} */ connect (sockfd, (SA *) &serv, sizeof(serv)); Since the kernel is passed both the pointer and the size of what the pointer points to, it knows exactly how much data to copy from the process into the kernel. Figure 1.12 shows this scenario.

Figure 1.12 Socket address structure passed from process to kernel.

2. Four functions, accept, recvfrom, getsockname, and getpeername, pass a socket address structure from the kernel to the process, the reverse direction from the previous scenario. Two of the arguments to these four functions are the pointer to the socket address structure along with a pointer to an integer containing the size of the structure, as in struct sockaddr_un cli; /* Unix domain */ socklen_t len; len = sizeof(cli); /* len is a value */

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

27 getpeername(unixfd, (SA *) &cli, &len); /* len may have changed */

Elementary TCP Sockets

The reason that the size changes from an integer to be a pointer to an integer is because the size is both a value when the function is called (it tells the kernel the size of the structure so that the kernel does not write past the end of the structure when filling it in) and a result when the function returns (it tells the process how much information the kernel actually stored in the structure). This type of argument is called a value-result argument. Figure 1.13 shows this scenario.

Figure 1.13 Socket address structure passed from kernel to process

1.5 BYTE ORDERING FUNCTIONS Consider a 16-bit integer that is made up of 2 bytes. There are two ways to store the two bytes in memory: with the low-order byte at the starting address, known as little-endian byte order, or with the high-order byte at the starting address, known as big-endian byte order. These two formats are shown in Figure 1.14. The Figure 1.14 shows increasing memory addresses going from right to left in the top, and from left to right in the bottom. Also shows the most significant bit (MSB) as the leftmost bit of the 16-bit value and the least significant bit (LSB) as the rightmost bit. The terms little-endian and big-endian indicate which end of the multibyte value, the little end or the big end, is stored at the starting address of the value. Unfortunately, there is no standard between these two byte orderings and we encounter systems that use both formats. We refer to the byte ordering
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

28

Elementary TCP Sockets

used by a given system as the host byte order. The program given below prints the host byte order.
in c r e a s in g m e m o r y a d d r e s s

li tt l e - e n d

ia n

a d d r e s s A + 1a d d r e s s A b y h t ie g h -r od re d r e: r b l oy tw e - o r d e r b o

y te

1 6 - b

it

v a lu e

L S

ig

- e n d

ia n

y t e

o r d

e r : m e m o r y a d d r e s s

in c r e a s in g

Figure 1.14 Little-endian byte order and big-endian byte order for a 16-bit integer

Program to determine host byte order intro/byteorder.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 } // sample Outputs for various operating system
IT2351-Network Programming & Management Dept. of IT/RMKEC

# include int

unp.h

main(int argc, char **argv) { union { short } s; char un; un.s = 0x0102; printf(%s: , CPU_VENDOR_OS); if (sizeof(short) == 2) { if (un.c[0] == 1 && un.c[1] == 2) printf(big-endian\n); else if (un.c[0] == 2 && un.c[1] == 1) printf(little-endian\n); else printf(unknown\n); } else printf(sizeof(short) = %d\n, sizeof(short)); exit(0); c[sizeof(short)];

Unit I
linux % byteorder i586-pc-linux-gnu: little-endian solaris % byteorder sparc-sun-solaris2.9: big-endian

29

Elementary TCP Sockets

The byte ordering used by the networking protocols is called as network byte order. The sending protocol stack and the receiving protocol stack must agree on the order in which the bytes of these multibyte fields will be transmitted. the following four functions to convert between these two byte orders.
#include <netinet/in.h> uint16_t htons(uint16_t host16bitvalue) ; uint32_t htonl(uint32_t host32bitvalue) ; Both return: value in network byte order uint16_t ntohs(uint16_t net16bitvalue) ; uint32_t ntohl(uint32_t net32bitvalue) ; Both return: value in host byte order // Note: h host, n network, s short, l long

1.6 ADDRESS CONVERSION FUNCTIONS inet_aton, inet_addr, and inet_ntoa Functions We will describe two groups of address conversion functions. They convert Internet addresses between ASCII strings (what humans prefer to use) and network byte ordered binary values (values that are stored in socket address structures). 1. inet_aton, inet_ntoa, and inet_addr convert an IPv4 address from a dotteddecimal string (e.g., 206.168.112.96) to its 32-bit network byte ordered binary value. The newer functions, inet_pton and inet_ntop, handle both IPv4 and IPv6 addresses. #include <arpa/inet.h> int inet_aton(const char *strptr, struct in_addr *addrptr); //Returns: 1 if string was valid, 0 on error in_addr_t inet_addr(const char *strptr); //Returns: 32-bit binary network byte ordered IPv4 address; INADDR_NONE if error char *inet_ntoa(struct in_addr inaddr); //Returns: pointer to dotted-decimal string

2.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

30

Elementary TCP Sockets

The first of these, inet_aton, converts the C character string pointed to by strptr into its 32-bit binary network byte ordered value, which is stored through the pointer addrptr. If successful, 1 is returned; otherwise, 0 is returned. inet_addr does the same conversion, returning the 32-bit binary network byte ordered value as the return value. The problem with this function is that all 232 possible binary values are valid IP addresses (0.0.0.0 through 255.255.255.255), but the function returns the constant INADDR_NONE (typically 32 one-bits) on an error. This means the dotted-decimal string 255.255.255.255 cannot be handled by this function since its binary value appears to indicate failure of the function. Today, inet_addr is deprecated and any new code should use inet_aton instead. The inet_ntoa function converts a 32-bit binary network byte ordered IPv4 address into its corresponding dotted-decimal string. The string pointed to by the return value of the function resides in static memory.Finally, notice that this function takes a structure as its argument, not a pointer to a structure. inet_pton and inet_ntop Functions These two functions are new with IPv6 and work with both IPv4 and IPv6 addresses. The letters p and n stands for presentation and numeric. The presentation format for an address is often an ASCII string and the numeric format is the binary value that goes into a socket address structure. #include <arpa/inet.h> int inet_pton(int family, const char *strptr, void *addrptr); //Returns: 1 if OK, 0 if input not a valid presentation format, -1 on error const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len); //Returns: pointer to result if OK, NULL on error The family argument for both functions is either AF_INET or AF_INET6. If family is not supported, both functions return an error with errno set to EAFNOSUPPORT. The first function tries to convert the string pointed to by strptr, storing the binary result through the pointer addrptr. If successful, the return value is 1. If the input string is not a valid presentation format for the specified family, 0 is returned. inet_ntop does the reverse conversion, from numeric (addrptr) to presentation (strptr). The len argument is the size of the destination, to prevent the function from overflowing the callers buffer. To help specify this size, the following two definitions are defined by including the <netinet/in.h> header

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I sock_ntop and Related Functions

31

Elementary TCP Sockets

A basic problem with inet_ntop is that it requires the caller to pass a pointer to a binary address. This address is normally contained in a socket address structure, requiring the caller to know the format of the structure and the address family. That is, to use it, we must write code of the form for IPv4: struct sockaddr_in addr;

inet_ntop(AF_INET, &addr.sin_addr, str, sizeof(str)); for IPv6: struct sockaddr_in6 addr6;

inet_ntop(AF_INET6, &addr6.sin6_addr, str, sizeof(str)); This makes our code, protocol-dependent. To solve this, we will write our own function named sock_ntop that takes a pointer to a socket address structure, looks inside the structure, and calls the appropriate function to return the presentation format of the address. #include unp.h char *sock_ntop(const struct sockaddr *sockaddr, socklen_t addrlen); //Returns: non-null pointer if OK, NULL on error sockaddr points to a socket address structure whose length is addrlen. The function uses its own static buffer to hold the result and a pointer to this buffer is the return value. 1.7 ELEMENTARY TCP SOCKETS Introduction This describes the elementary socket functions required to write a complete TCP client and server. We will first discuss all the elementary socket functions that we will be using and then develop the client and server. We will also discuss concurrent servers, a common Unix technique for providing concurrency when numerous clients are connected to the same server at the same time. Each client connection causes the server to fork a new process just for that client. Figure 1.15 illustrates the socket as an interface between the application and the other communication protocols that exist in the communication of the information across the network.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I
s o c k eA t p p in t e r f a c e l ic a t io u s e r

32
n 1 A

Elementary TCP Sockets


p p s o c k e t l ic a t io n 2 in t e r f a c e

u s e r k e r n e l S o c k e t

k e r n e l S o c k e t

U n d e r ly i n g c o m m u n ic a t io n P r o t o c o l s

U n d e r ly i n g c o m m u n ic a t io n P r o t o c o l s

Figure 1.15 Interface using sockets

Figure 1.16 Socket Functions

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

33

Elementary TCP Sockets

The Figure 1.16 shows the functioning of the socket in the process of information communication between the server and the client applications. socket() Function As represented in the Figure 1.16, in order to perform network I/O, the first thing a process must do is call the socket( ) function, specifying the type of communication protocol desired (TCP using IPv4, UDP using IPv6, Unix domain stream protocol, etc.). #include <sys/socket.h> int socket (int family, int type, int protocol); //Returns: non-negative descriptor if OK, -1 on error family: specifies the protocol family {AF_INET for TCP/IP} type: indicates communications semantics SOCK_STREAM stream socket SOCK_DGRAM SOCK_RAW datagram socket raw socket TCP UDP

protocol: set to 0 except for raw sockets Example: if (( sd= socket (AF_INET, SOCK_STREAM, 0)) < 0) err_sys(socket call error); connect() Function The connect( ) function is used by a TCP client to establish a connection with a TCP server. #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); //Returns: 0 if OK, -1 on error sockfd: a socket descriptor returned by the socket function *servaddr: a pointer to a socket address structure addrlen: the size of the socket address structure The socket address structure must contain the IP address and the port number for the connection wanted. In TCP connect( ) initiates a three-way handshake and returns only when the connection is established or when an error occurs. Example: if ( connect (sd, (structsockaddr*) &servaddr, sizeof(servaddr)) != 0)
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I err_sys(connectcall error); bind() Function

34

Elementary TCP Sockets

The bind( ) function assigns a local protocol address to a socket. With the Internet protocols, the protocol address is the combination of either a 32-bit IPv4 address or a 128-bit IPv6 address, along with a 16-bit TCP or UDP port number. #include <sys/socket.h> int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); //Returns: 0 if OK,-1 on error protocol address is a 32 bit IPv4 address and a 16 bit TCP or UDP port number. sockfd: a socket descriptor returned by the socket function. *myaddr: a pointer to a protocol-specific address. addrlen: the size of the socket address structure. Servers bind their well-known portwhen they start. Example: if (bind (sd, (structsockaddr*) &servaddr, sizeof(servaddr)) != 0) errsys(bind call error); listen() Function listen( ) is normally called before the accept function. It is called only by a TCP server and performs two actions: 1. 2. Converts an unconnected socket (sockfd) into a passive socket. Specifies the maximum number of connections (backlog) that the kernel should queue for this socket.

#include <sys/socket.h> int listen (intsockfd , int backlog); // Returns: 0 if OK,-1 on error Example: if (listen (sd, 2) != 0) errsys(listen call error); accept() Function The accept( ) is called by a TCP server to return the next completed connection from the front of the completed connection queue. If the completed connection queue is empty, the process is put to sleep (assuming the default of a blocking socket). The server will have one connected socket for each client connection accepted. When the server is finished with a client, the connected socket must be closed.
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I #include <sys/socket.h>

35

Elementary TCP Sockets

int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); //Returns: non-negative descriptor if OK, -1 on error sockfd: This is the same socket descriptor as in listen call. *cliaddr: used to return the protocol address of the connected peer process (i.e., the client process). *addrlen: {this is a value-result argument} before the accept call: We set the integer value pointed to by *addrlen to the size of the socket address structure pointed to by *cliaddr; on return from the accept call: This integer value contains the actual number of bytes stored in the socket address structure. Example: sfd= accept (sd, NULL, NULL); if (sfd== -1) err_sys (accept error); Stream sockets (e.g., TCP sockets) exhibit a behavior with the read( ) and write( ) functions that differs from normal file I/O. read( ) and write( ) are the same functions used with files but we can use them with sockets as well. However, it is extremely important to understand how they work. write() Function #include<unistd.h> #include<sys/types.h> int write(int fd, char *Buff, int NumBytes); int write(int file_descriptor, const void * buf, size_t message_length); fd: *Buff: NumBytes: file_descriptor: *buf: message_length:// Example: int fd; char Buff[]=V8 cars are coool; write(fd, Buff, strlen(Buff)+1);

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

36

Elementary TCP Sockets

The return value is the number of bytes written. The number of bytes written may be less than the message_length. What this function does is transfer the data from your application to a buffer in the kernel on your machine, it does not directly transmit the data over the network. TCP is in complete control of sending the data and this is implemented inside the kernel. Due to network congestion or errors, TCP may not decide to send your data right away, even when the function call returns. read( ) Function #include<unistd.h> #include<sys/types.h> int read(int fd, char *Buff, int NumBytes); int read(int file_descriptor, const void * buf, size_t buffer_length); fd: *Buff: NumBytes: file_descriptor: *buf: buffer_length:// Example: int fd; char Buff[50]; read(fd, Buff, sizeof(Buff)); The value returned is the number of bytes read which may not be buffer_length. As with write( ), read( ) only transfers data from a buffer in the kernel to your application , you are not directly reading the byte stream from the remote host, but rather TCP is in control and buffers the data for your application. Whenever we read from or write to a stream socket we can make use of the following three functions. #include unp.h ssize_t readn(int filedes, void *buff, size_t nbytes); //Returns number of bytes read, -1 on error. ssize_t writen(int filedes, const void *buff, size_t nbytes); //Returns number of bytes written, -1 on error. ssize_t readline(int filedes, void *buff, size_t maxlen); //Returns number of bytes read, -1 on error.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I close() Function

37

Elementary TCP Sockets

The normal Unix close( )function is also used to close a socket and terminate a TCP connection. It marks the socket as closed and returns to the process immediately. #include <unistd.h> int close (int sockfd); //Returns: 0 if OK, -1 on error sockfd : This socket descriptor is no longer useable. Note TCP will try to send any data already queued to the other end before the normal connection termination sequence. Example: close (sd); fork() and exec() Functions Before describing how to write a concurrent server in the next section, we must discuss the Unix fork( ) function. This function (including the variants of it provided by some systems) is the only way in Unix to create a new process. All descriptors open in the parent before the call to fork are shared with the child after fork returns. #include <unistd.h> pid_t fork(void); //Returns: 0 in child, process ID of child in parent, -1 on error The only way in which an executable program file on disk is executed by Unix is for an existing process to call one of the six exec( ) functions. In general the exec( ) replaces the current process image with the new program file and this new program normally starts at the main function. The process ID does not change. #include <unistd.h> execl(const char *pathname, const char *arg0, /* (char *) 0 */ ) ; execv(const char *pathname, char *const argv[ ]) ; execle(const char *pathname, const char *arg0, /* (char *) 0, char *const envp[ ] */ ) ; execve(const char *pathname, char *const argv[ ], char *const envp[ ]) ;v execlp(const char *filename, const char *arg0, /* (char *) 0 */ ) ; execvp(const char *filename, char *const argv[ ]) ; All the six exec( ) functions return to the caller only if an error occurs. Otherwise the control passes to the start of the new program.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

38

Elementary TCP Sockets

execve is the system call within the kernel and all the other are library functions that invoke execve execpl, execl and execle specify each argument string as a separate argument to the exec function. Whereas execvp, execv and exeve have an argv array, containing the pointers to the argument strings. Null pointer is used to terminate the variable number of arguments. execpl and execvp specify a filename argument. This is converted into pathname by using the current PATH environment variable. If / is used in the filename then PATH variable is not used. execl, execle, execv and execve use a fully qualified pathname argument. execl, execlp, execv and execvp do not specify an explicit environment pointer. The current value of external variable environ is used for building an environment list that is passed to the new program. Whereas execle and execve specify an explicit environment list.

Figure 1.17 Relationship among six exec functions

TCP/IP echo Server: #include #include #include #include <sys/socket.h> <sys/types.h> <arpa/inet.h> <unistd.h> /* socket definitions /* socket types /* inet (3) funtions */ /* misc. UNIX functions */ */ */

#include <stdlib.h> #include <stdio.h> /* Global constants */ #define MAX_LINE (1000)

int main(int argc, char *argv[]) { int list_s; /* listening socket */


IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

39

Elementary TCP Sockets /*

int conn_s; connection socket */ short int port; /* port number */ struct sockaddr_in servaddr; /* socket address structure */ char buffer[MAX_LINE]; /* character buffer */ char *endptr; /* for strtol() */ printf(\n Enter Server Port No to Listen ); scanf(%d,&port); if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { fprintf(stderr, ECHOSERV: Error creating listening socket.\n); exit(EXIT_FAILURE); } /* Set all bytes in socket address structure to zero, and fill in the relevant data members */ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(port); /* Bind our socket addresss to the listening socket, and call listen() */ if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) { fprintf(stderr, ECHOSERV: Error calling bind()\n); exit(EXIT_FAILURE); } if ( listen(list_s, 5) < 0 ) { fprintf(stderr, ECHOSERV: Error calling listen()\n); exit(EXIT_FAILURE); } /* Enter an infinite loop to respond to client requests and echo input */ while ( 1 ) { /* Wait for a connection, then accept() it */ if ( (conn_s = accept(list_s, NULL, NULL) ) < 0 ) { fprintf(stderr, ECHOSERV: Error calling accept()\n); exit(EXIT_FAILURE); } /* Retrieve an input line from the connected socket then simply write it back to the same socket. */
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

40

Elementary TCP Sockets

strcpy(buffer,); read(conn_s,buffer,20); printf(\n Message Received & Echoed %s ,buffer); write(conn_s,buffer,strlen(buffer)); /* Close the connected socket */ if ( close(conn_s) < 0 ) { fprintf(stderr, ECHOSERV: Error calling close()\n); exit(EXIT_FAILURE); } } } TCP/IP echo Client: #include #include #include #include <sys/socket.h> <sys/types.h> <arpa/inet.h> <unistd.h> /* socket definitions /* socket types /* inet (3) funtions */ /* misc. UNIX functions */ */ */

#include <stdlib.h> #include <string.h> #include <stdio.h> /* Global constants */ #define MAX_LINE /* main() */ int main(int argc, char *argv[]) { int conn_s; /* connection socket */ short int port; /* port number */ struct sockaddr_in servaddr; /* socket address structure */ char buffer[MAX_LINE]; /* character buffer */ char *szAddress; /* Holds remote IP address */ char *szPort; /* Holds remote port */ char *endptr; /* for strtol() */ /* Get command line arguments */ printf(\n Remote Host Address : %s \n Remote Port No : %s \n,argv[1],argv[2]); szAddress = argv[1]; szPort = argv[2]; /* Set the remote port */ port = strtol(szPort, &endptr, 0); if ( *endptr ) {
IT2351-Network Programming & Management Dept. of IT/RMKEC

(1000)

Unit I

41

Elementary TCP Sockets

printf(ECHOCLNT: Invalid port supplied.\n); exit(EXIT_FAILURE); } /* Create the listening socket */ if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { printf(ECHOCLNT: Error creating listening socket.\n); exit(EXIT_FAILURE); } /* Set all bytes in socket address structure to zero, and fill in the relevant data members */ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); /* Set the remote IP address */ if ( inet_aton(szAddress, &servaddr.sin_addr) <= 0 ) { printf(ECHOCLNT: Invalid remote IP address.\n); exit(EXIT_FAILURE); } /* connect() to the remote echo server */ if ( connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { printf(ECHOCLNT: Error calling connect()\n); exit(EXIT_FAILURE); } /* Get string to echo from user */ printf(Enter the string to echo: ); fgets(buffer, MAX_LINE, stdin); /* Send string to echo server, and retrieve response */ write(conn_s,buffer,strlen(buffer)+1); strcpy(buffer,); read(conn_s,buffer,20); /* Output echoed string */ printf(Echo response: %s\n, buffer); return EXIT_SUCCESS; }

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

42

Elementary TCP Sockets

1.8 DIFFERENT TYPES OF SERVERS Possible Server Designs

Iterative server; each client request is completely processed before moving on to the next client. Concurrent server with fork, one process per client Concurrent server with threads, one thread per client Server using select Preforking server - create a pool of child processes at startup, one process from the pool handles each client Prethreading - create a pool of threads at startup, one thread from the pool handles each client

Iterative server is best if the response from the server is quick because there is minimal overhead. Concurrent server with fork is best if there will be few connections but each connection will do extensive reading and writing over an extended period. You have to deal with zombies. Concurrent servers with threads are generally better than a concurrent server with fork, because the overhead of creating a new thread is much less than that for creating a new process. Server with select This design is best for a server which wants to listen on many sockets simultaneously but where connections are relatively rare Preforking server This is a new concept, but it is worth studying because it is probably the best design for servers which have to handle many requests and response time is important. For example, file servers or web servers usually use a preforking model. These receive many requests and have to respond very quickly. The overhead associated with creating a new process or even a new thread for each request would be prohibitive if the server gets heavy use. To do this, the server calls socket, bind and listen exactly as we have seen, and then calls fork several times to create a number of identical processes. Each of these processes then enters its infinite loop and each calls accept. Recall that when a call to fork creates a new child process, all of the file descriptor information is duplicated. This means that process is listening on the same port. Each process goes to sleep. What happens when a connection occurs is somewhat system dependent; here is how it works on Berkeley Unix. When a connection arrives, all N processes are awakened. This is because all have been put to sleep on the same wait channel. Exactly one of these will accept the connection (accept will return). The others will go back to sleep.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

43

Elementary TCP Sockets

The code should be written such that each connection is handled concurrently. The process that accepted the connection will read the request and supply the response. Meanwhile, if other connections arrive, another process will accept and handle it. When a particular process completes a request, it closes the socket on which it received and sent the data, and goes back to the accept statement again. One issue is how many processes to create. If there are too few processes for the number of connections, clients may still be forced to wait if all of the processes are busy handling other connections. However, there is some minimal overhead associated with waking up many processes, and so it is inefficient to create too many processes. This may not work on other Unix implementations. One solution is to put a lock or some other mutual exclusion primitive around the accept statement so that only one process will be able to accept at any given instance. The apache web server uses preforking with an additional twist. It can change the number of processes based on load. It periodically checks to see how many processes are busy. If most are busy, it creates more processes; if most are idle, it can kill some of the processes. The system administrator can set a minimum and maximum number of child processes. Prethreaded server This works in much the same way as a preforking server, and has more or less the same advantages and disadvantages. One potential problem with this is that if a fatal exception occurs, such as a segmentation fault, it will kill the entire process including all the threads, while if it happens in a preforked server, it will kill that process, but the other processes can continue. Of course if code is well written, this should never happen. Depending on whether we are interested in connectionless communication (UDP) or connection oriented communication (TCP), and whether we want our server to process one request at a time (iterative server) or whether to process each request in parallel (concurrent server), we arrive at four possibilities:

Sequential Iterative connectionless server Sequential Iterative connection-oriented server Concurrent connectionless server Concurrent connection-oriented server Iterative Server

1.8.1

An iterative TCP server processes each clients request completely before moving on to the next client. That is an iterative server process handles all clients. Iterative TCP servers are rare but a simple example could be as shown in the code below:
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I #include upn.h #include <time.h> int main(int argc, char **argv) { int listenfd, connfd; socketlen_t len;

44

Elementary TCP Sockets

struct sockaddr_in servaddr, cliaddr; char buff[MAXLINE]; time_t ticks; listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13);

bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

listen(listenfd, LISTENQ);

for( ; ; ) { len = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &len); printf(connection from %s, port %d\n, Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff), ntohs(clientaddr.sin_port) ); ticks = time(NULL); snprintf(buff, sizeof(buff), %.24s\r\n, ctime(&ticks)); write(connfd, buff, strlen(buff));

close(connfd); }

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I } If we run the client as

45

Elementary TCP Sockets

%client 206.62.226.36 8888 1 5000 4000 to an iterative server, we get the same number of TCP connections and the same amount of data transferred across each connection. But since the server is iterative, there is no process control whatsoever performed by the server. The following sequence shows the function calls for an iterative server application: 1. The socket() function returns a socket descriptor representing an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM) will be used for this socket. After the socket descriptor is created, the bind() function gets a unique name for the socket. The listen() allows the server to accept incoming client connections. The server uses the accept() function to accept an incoming connection request. The accept() call will block indefinitely waiting for the incoming connection to arrive. The recv() function receives data from the client application. The send() function echoes the data back to the client. The close() function closes any open socket descriptors. Concurrent Server A concurrent server can handle multiple client requests at one time. Large or variable size requests Harder to program Typically uses more system resources

2. 3. 4.

5. 6. 7. 1.8.2

Most client software achieves concurrent operation because the underlying OS allows users to execute client programs concurrently or because user can execute client software simultaneously. When a client request can take longer to service, we need not have to tie up a single server with one client; to handle multiple clients at the same time. The simplest way to write a concurrent server under UNIX is to fork a child process to handle each client. Following segment shows the outline for a typical concurrent server. With the fork() command we create another separate process for each request. The fork() command splits the current process into two processes: a parent and a child. How does the program determine whether it is the parent or the child after the split? Well the fork() command returns 0 if you are the child, so all you must do is check the return value from fork(). From this point you
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

46

Elementary TCP Sockets

can program the child to process the clients request and the parent can continue on to accept other requests. However, when a child finishes and exits it needs to notify the parent that it is done. Outline code for typical concurrent server. pid_t pid; int listenfd, connfd;

listenfd = Socket( ... ); /* fill in sockaddr_in{} with servers well-known port */ bind(listenfd, ... ); listen(listenfd, LISTENQ); for ( ; ; ) { connfd = Accept (listenfd, ... ); if( (pid = Fork()) == 0) { close(listenfd); doit(connfd); close(connfd); exit(0); } close(connfd); } The function doit does whatever is required to service the client. When this function returns, we explicitly close the connected socket in the child. This is not required since the next statement calls exit, and part of process termination is to close all open descriptors by the kernel. Concurrent Server model works as a Master-Slave model. The server is not connected to client directly. The accept command makes the server to wait at a particular port for the request to arrive. Upon its arrival master server process creates a slave process to handle the connection. Also the master process is blocked to accept another call. More than one slave process can be created and can be allowed to operate concurrently Concurrent, Connectionless Server: Parent Step 1: Create a socket and bind, leave the socket unconnected. Parent Step 2: Repeatedly call recvfrom( ) to receive the next request from a client, and create a new slave process (either through thr_create() or fork( )) to handle the response. /* parent closes connected socket */ /* child closes listening socket */ /* process the request */ /* done with this client */ /* child terminates */ /* probably blocks */

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

47

Elementary TCP Sockets

Slave Step 1: Receive a specific request upon creation as well as access to the socket. Slave Step 2: Form a reply according to the application protocol and send it back to the client using sendto( ) API. Slave Step 3: Exit (i.e. a slave process terminates after handling one request).

Because process creation is usually expensive, few connectionless servers have concurrent implementations.

Concurrent, Connection-oriented Server: Connection-oriented servers use a connection as the basic paradigm for communication. They allow a client to establish a connection to a server, communicate over that connection, and then discard it after finishing. In most cases, the connection between clients and a server handles more than a single request, thus: Connection-oriented protocols implement concurrency among connections rather than individual requests. Parent Step 1: Create a socket and bind, leave the socket unconnected. Parent Step 2: Place the socket in a passive mode, making it ready for use by a server, e.g. listen(s, 3). Parent Step 3: Repeatedly call accept() to receive the next request from a client, and create a new slave process to handle the response. Slave Step 1: Receive a connection request upon creation, i.e., socket for the connection. Slave Step 2: Interact with the client using the connection, read and send back the responses. Slave Step 3: Close the connection and exit. The slave process exits after handling all requests from clients. The Algorithm for the concurrent server

Master server calls accept to wait for connection request from client. The command, accept creates a socket and returns a socket descriptor. Master server creates a slave process using fork command to handle connection. The parent process closes the socket. The above loop is repeated close command by the master process for the new connection, closes connection (socket) for the master process. close command by the slave process, closes connection (socket) for the slave process. Slave process continues to have access to new socket until it exits.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

48

Elementary TCP Sockets

Master continues to retain access to the socket that corresponds to the well known port. The slave closes the master socket and provides echo service. read and write repeatedly executed, returning number of bytes read and written. In this case, negative number indicates error and 0 indicates EOF condition. exit command is used to terminate the process. When slave exits the system automatically closes open descriptors.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

49

Elementary TCP Sockets

2 MARK QUESTION AND ANSWERS


1. What is a socket? A socket is a logical endpoint for communication between two hosts on a TCP/IP network. A socket is uniquely identified by 3 attributes:

The hosts IP address The type of service needed. eg. (TCP/UDP) The port number

2.

State the differences between TCP and UDP. UDP (User Datagram Protocol)

TCP (Transmission Control Protocol)


Connection-Oriented Sophisticated Reliable Byte-Stream

Connection-less Simple Unreliable Datagram protocol

protocol

3.

What are the services provided by TCP? 1. 2. 3. 4. 5. Connections between clients and servers. Reliability Sequencing Flow Control Full-duplex communication

4.

What is Passive open? The server prepares to accept an incoming connection by calling socket, bind and listen functions. This is called passive open.

5.

What is Active open? The client connects to the server by calling the connect function. It causes the client to send a SYN segment which consists of initial sequence number for the data. This is called Active open.

6.

What are the steps in three way Handshake?

The TCP three way handshake is the process for establishing a TCP connection. A TCP connection is established as shown in example given below. In this example, it is assumed that a client computer is contacting a server to send it some information. 1. The client sends a packet with the SYN bit set and a sequence number of N. 2. The server sends a packet with an ACK number of N+1, the SYN bit set and a sequence number of X. 3. The client sends a packet with an ACK number of X+1 and the connection is established. 4. The client sends the data.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

50

Elementary TCP Sockets

The first three steps in the above process is called the three way handshake.

7.

What are various TCP options? The TCP options are as follows: 1. 2. 3. MSS option - Maximum Segment Size Window Scale option Time stamp option

8.

What is a long fat pipe network? Data transfer over long distance and high bandwidth network is called Long Fat pipe Network.

9.

What options are called long fat pipe options? Windows scale options and Timestamp options are called long fat pipe options.

10.

What is active close? The side (client/server) which calls the close function first, is performing active close.

11.

What is passive close? The side (client/server) that receives the FIN segment from the other end performs the passive close.

12.

What is half-close? When closing the connection among two parties, A party wishing to close its side of the TCP connection sends a FIN . The other party sends an ACK for the FIN, it receives from other end. And after the other party ACKs, the connection is half-closed. The party which sent the FIN can no longer send data -- but the other party may continue to send data.

It is possible for data to flow from the end doing the passive close to the end doing the active close. This is called a half-close.
13. What are the three different categories of ports? 1. 2. 3. 14. The well-known ports (0 to 1023) The registered ports (1024 to 49151) The dynamic or private ports (49152 to 65535)

What are well-known ports? Well-known ports are used to identify well-known services. Port numbers from 0 to 1023 are well known ports. For example: Port 21 is used to identify FTP service, port 80 is used to identify web service.

15.

What are registered ports?

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I

51

Elementary TCP Sockets

Registered ports are not controlled by IANA. Port numbers from 1024 through 49151 are registered ports. These ports are used according to the convenience of the network community.

16.

What are Dynamic/Private/Ephemeral ports?

Client processes don't use well-known or registered ports. Instead, each client process is assigned a temporary port number for its use. This is commonly called an ephemeral port number. The dynamic port numbers are in the highest range, from 49152 through 65535.These ports are used only by
TCP/UDP client.

17.

What is a reserved port?

The Well Known Ports are those from 0 through 1023 and are referred to as reserved port numbers. Well Known ports SHOULD NOT be used without IANA registration. 18. 19. 20. What is the maximum size of IPv4 datagram? 65535 bytes including header. What is the maximum size of IPv6 datagram? 65575 bytes including header. What is the port number for Echo and Daytime?

Echo 7 Daytime 13

21.

What is the TIME_WAIT State? The side (client/server) which is performing the active-close has to wait until it gets the FIN signal from the other end and after this it closes the connection. This waiting state is called TIME_WAIT state.

22. 23. 24.

Who is performing active-close in echo server? Client performs the active-close in echo server. Who is performing the active-close in daytime server? Server performs the active-close in daytime server. What are socketing Address structures? Socket Address Structure is passed as argument to the socket function. It is used to set the properties of socket, such as 1. 2. 3. 4. length family port number IP address

25.

What is host byte order? The byte ordering followed by a given system can be in two ways: little endian byte order or big endian byte order. This is called host byte order.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I 26. What is network byte order?

52

Elementary TCP Sockets

In a network, the sending protocol stack and the receiving protocol stack must agree or the order in which bytes is transmitted. This is called network byte order. It uses big-endian byte ordering. 27. What is the use of htons, htonl, ntohs, ntohl functions?

htons Converts 16-bit value from host byte order to network byte order htonl Converts 32-bit value from host byte order to network byte order

ntohs Converts 16-bit value from network byte order to host byte order ntohl Converts 32-bit value from network byte order to host byte order

Note: h host, n network, s short, l long 28. What is the use of inet_aton function? The syntax of the function is int inet_aton(const char *cp, struct in_addr *addr). It converts the specified string, in the Internet standard dot notation, to a network address, and stores the address in the structure provided (addr). 29. What is the use of inet_addr function? The syntax of the function is in_addr_t inet_addr (const char *strptr). It converts the string pointed to by strptr, in the Internet standard dot notation, to an integer value suitable for use as an Internet address. 30. What is the use of inet_ntoa function? The syntax of the function is char *inet_ntoa (struct in_addr inaddr). It converts a network address in a struct in_addr to a dots-and-numbers format string. The "n" in "ntoa" stands for network, and the "a" stands for ASCII. 30. What is the use of inet_aton function? The syntax of the function is int inet_aton(const char *cp, struct in_addr *inp). The function inet_aton( ) is to convert from a dots-and-numbers string into network address. 31. What is the use of inet-pton function? The syntax of the function is int inet_pton (int family, const char *strptr, void *addrptr). It converts an address from presentation format to network format. 32. What is the use of inet_ntop function? The syntax of the function is const char *inet_ntop (int family, const void *addrptr, char *strptr, size_t len). It converts an address from network format to presentation format.
IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I 33.

53

Elementary TCP Sockets

What are various Elementary TCP client socket functions? 1. 4. socket read 2. 5. Connect close 3. write

34.

What are various elementary TCP server socket functions? 1. 3. 5. socket listen read 2. 4. 6. bind accept Write 7.

close

35.

What is the use of socket function? Socket function is used to create a socket that is bound to a specific transport service provide. It returns a file descriptor that can be used in later function calls that operate on sockets.

36.

What is the use of the connect function? The connect function establishes a connection to a specified socket. The syntax of the function is int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen). If the socket has not already been bound to a local address, connect( ) shall bind it to an address which, is an unused local address.

37.

What is a soft error? If the client wants to connect to the server but if the destination is unreachable from some intermediate router, it is called a soft error.

38.

What is the need for bind system call? The bind system call associates a local network transport address with a socket. In case of client it isn't necessary to call bind before the process attempts to communicate; the kernel selects and implicitly binds a local address to the socket as needed. A server process must call bind before accepting connections (TCP) or receiving datagrams (UDP), because the clients establish connections or send datagrams to the well-known address.

39.

What is the need for listen system call? It notifies a protocol that the process is prepared to accept incoming connections on the socket. It also specifies a limit on the number of connections that can be queued on the socket, after which the socket layer refuses to queue additional connection requests.

40.

What is an incomplete connection queue? Contains an entry for each client for which Three-way handshake is not yet completed. These sockets are in the SYN_RCVD state.

41.

What is a completed connection queue? Contains an entry for each client with whom three-way handshake has completed. These are the sockets in the ESTABLISHED state.

42.

What is SYN-RCVD state? The sockets in incomplete connection queue are in SYN_RCVD state. The sockets in this state cannot send or receive data.

IT2351-Network Programming & Management Dept. of IT/RMKEC

Unit I 43. What is ESTABLISHED state?

54

Elementary TCP Sockets

The sockets in completed connected queue are in ESTABLISHED state. Sockets in this state only, can send and receive data. 44. What is the need for accept system call? A process waits for incoming connections by calling accept, which returns a descriptor that references a new socket connected to a client. The original socket remains unconnected and ready to receive additional connections. accept returns the address of the foreign system if name points to a valid buffer. 45. What is the need of fork system call? fork function is used to create a new process. The new process (child) is an exact copy of the calling process (parent). 46. What are the uses of fork system call? 1. 2. 47. When a process need to make a copy of self, so that one copy can handle one operation while the other copy does another task.

fork is of use when we need to write daemon processes

What is the need of exec system call? exec replaces the current process image with the new program file which starts at the main function.

48.

What is an Iterative server? Iterative server will tie up a server with a single client. We would not be able to serve the multiple numbers of clients simultaneously. It is used for simple, short services.

49.

What is a concurrent server? Concurrent servers service many clients simultaneously using timesharing method or by having different processors. It can be implemented in UNIX by using fork system call.

50.

What is the use of close function? The close function is used to close a socket and terminate a TCP connection.

51.

What is the use of getsockname function?

getsockname function returns the local protocol address associated with a socket. 52. What is the use of getpeername function? getpeername function returns the foreign protocol address associated with a socket.

IT2351-Network Programming & Management Dept. of IT/RMKEC

S-ar putea să vă placă și