IT2351 SYLLABUSUNIT I ELEMENTARY TCP SOCKETS 9 Introduction to
Socket Programming Overview of TCP/IP Protocols Introduction to
Sockets Socket address Structures Byte ordering functions address
conversion functions Elementary TCP Sockets socket, connect, bind,
listen, accept, read, write, close functions Iterative Server
Concurrent Server. UNIT II APPLICATION DEVELOPMENT 9 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). UNIT III SOCKET OPTIONS, ELEMENTARY UDP SOCKETS 9
Socket options getsocket and setsocket functions generic socket
options IP socket options ICMP socket options TCP socket options
Elementary UDP sockets UDP echo Server UDP echo Client Multiplexing
TCP and UDP sockets Domain name system gethostbyname function Ipv6
support in DNS gethostbyadr function getservbyname and
getservbyport functions. UNIT IV ADVANCED SOCKETS 9 Ipv4 and Ipv6
interoperability threaded servers thread creation and termination
TCP echo server using threads Mutexes condition variables raw
sockets raw socket creation raw socket output raw socket input ping
program trace route program. UNIT V SIMPLE NETWORK MANAGEMENT 9
SNMP network management concepts SNMP management information
standard MIBs SNMPv1 protocol and Practical issues introduction to
RMON, SNMPv2 and SNMPv3.
1
STUDY MATERIAL QUESTION BANKUNIT I ELEMENTARY TCP SOCKETSTOPIC :
Introduction to Socket Programming Overview of TCP/IP Protocols
1. What are sockets? A socket is defined as an endpoint for
communication. A network socket is an endpoint of an inter-process
communication flow across a computer network. A socket is an
endpoint of a two-way communication link between two programs
running on the network. Sockets provide an interface for
programming networks at the transport layer. A socket is one of the
most fundamental technologies of computer networking. A socket is
used to allow one process to speak to another, very much like the
telephone is used to allow one person to speak to another. Many of
today's most popular software packages -- including Web Browsers,
Instant Messaging and File Sharing -- rely on sockets. Sockets were
introduced in 1981 as the Unix BSD 4.2 generic interface for Unix
to Unix communications over networks. Socket-based communication is
programming language independent. That means, a socket program
written in Java language can also communicate to a program written
in Java or non-Java socket program. A socket address is the
combination of an IP address and a port number, much like one end
of a telephone connection is the combination of a phone number and
a particular extension. Based on this address, internet sockets
deliver incoming data packets to the appropriate application
process. Clients and servers communicate with each by reading from
and writing to socket descriptors. To the kernel, a socket is an
endpoint of communication. To an application, a socket is a file
descriptor that lets the application read/write from/to the
network.
2. Explain socket communication.a) A client making a connection
request to the server : A server (program) runs on a specific
computer and has a socket that is bound to a specific port. The
server waits and listens to the socket for a client to make a
connection request.
2
b) Session established with temporary ports used for two way
communication: If everything goes well, the server accepts the
connection. Upon acceptance, the server gets a new socket bounds to
a different port. It needs a new socket (consequently a different
port number) so that it can continue to listen to the original
socket for connection requests while serving the connected
client.
3. What is socket programming?In computing, network programming,
essentially identical to socket programming or clientserver
programming, involves writing computer programs that communicate
with other programs across a computer network. The program or
process initiating the communication is called a client process,
and the program waiting for the communication to be initiated is
the server process.
4. What is a protocol ? A protocol is a set of rules of
communication. Protocols are the building blocks of a network
architecture. When writing programs that communicate across a
computer network, one must first invent a protocol, an agreement on
how those programs will communicate.
5. Explain TCP. TCP is a transport layer protocol used by
applications that require guaranteed delivery. Connection oriented:
An application requests a connection to destination and uses
connection to transfer data. Point-to-point: A TCP connection has
two endpoints (no broadcast/multicast). Reliability: TCP guarantees
that data will be delivered without loss, duplication or
transmission errors.3
Full duplex: Endpoints can exchange data in both directions
simultaneously. Delivering TCP: TCP segments travel in IP
datagrams. Internet routers only look at IP header to forward
datagrams. Each segment contains a sequence number. Flow Control:
Flow control is necessary when a computer in the network transmits
data too fast for another computer to receive it .Flow control
requires some form of feedback from the receiving peer. This is
executed effectively due to the receivers buffer i.e., Window. 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. For example, the RTT on a LAN can be
milliseconds while across a WAN, it can be seconds. Furthermore,
TCP continuously estimates the RTT of a given connection, because
the RTT is affected by variations in the network traffic.
6. Explain UDP. The User Datagram Protocol (UDP) provides a
connectionless, unreliable transport service. Connectionless means
that a communication session between hosts is not established
before exchanging data. UDP is often used for communications that
use broadcast or multicast Internet Protocol (IP) packets. The UDP
connectionless packet delivery service is unreliable because it
does not guarantee data packet delivery or send a notification if a
packet is not delivered. Because delivery of UDP packets is not
guaranteed, applications that use this protocol must supply their
own mechanisms for reliability if necessary. Although UDP appears
to have some limitations, it is useful in certain situations. Each
UDP datagram has a length. The length of a datagram is passed to
the receiving application along with the data.
7. Differentiate TCP & UDP.Transmission Control Protocol
(TCP) and User Datagram Protocol (UDP) is a transportation protocol
that is one of the core protocols of the Internet protocol suite.
Both TCP and UDP work at transport layer.
TCPReliability: TCP is connection-oriented protocol. When a file
or message is send it will get delivered unless connections fails.
If connection lost, the server will request the lost part. There is
no corruption while transferring a message. Ordered: If you send
two messages along a connection, one after the other, you know the
first message will get there first. You don't have to worry about
data arriving in the wrong order Heavyweight: - when the low level
parts of the
UDPReliability: UDP is connectionless protocol. When you a send
a data or message, you don't know if it'll get there, it could get
lost on the way. There may be corruption while transferring a
message. Ordered: If you send two messages out, you don't know what
order they'll arrive in i.e. not ordered.
Lightweight: No ordering of messages, no 4
TCP "stream" arrive in the wrong order, resend requests have to
be sent, and all the out of sequence parts have to be put back
together, so requires a bit of work to piece together. Streaming:
Data is read as a "stream," with nothing distinguishing where one
packet ends and another begins. There may be multiple packets per
read call. Examples: World Wide Web (Apache TCP port 80), e-mail
(SMTP TCP port 25 Postfix MTA), File Transfer Protocol (FTP port
21) and Secure Shell (OpenSSH port 22) etc.
tracking connections, etc. It's just fire and forget! This means
it's a lot quicker, and the network card / OS have to do very
little work to translate the data back from the packets. Datagrams:
Packets are sent individually and are guaranteed to be whole if
they arrive. One packet per one read call. Examples: Domain Name
System (DNS UDP port 53), streaming media applications such as IPTV
or movies, Voice over IP (VoIP), Trivial File Transfer Protocol
(TFTP) and online multiplayer games etc
8. Explain TCP connection establishment.Three-Way Handshake The
following scenario occurs when a TCP connection is established: 1.
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 the server will send on the connection.
The server sends its SYN and the ACK of the client's SYN in a
single segment. J is the initial sequence number of client and K is
that of server. ACK number is the initial sequence number plus 1.
4. The client must acknowledge the server's SYN. As the minimum
number of packets required is 3, it is called three way
handshake.
5
9.
Explain TCP connection termination.While it takes three segments
to establish a connection, it takes four to terminate a connection.
1. 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.
10. Expalin TCP options.TCP SYN can contain TCP options. Common
options are: MSS (Maximum Segment Size), Window Scale Option, Time
Stamp Option. MSS Option: With this option the TCP sending SYN
announces its maximum segment size, the maximum amount of data that
it is willing to accept in each TCP segment, on this connection.
This option is set by TCP_ MAXSEG socket option.
6
Window scale option: The maximum window that either TCP can
advertise to the other TCP is 65535 as the corresponding field in
the TCP header occupies 16 bits. But high speed connections (45
Mbps/sec) or long delay paths require larger window which can be
set by left shifting (scaling) by 0-14 bits giving rise to one
gigabyte. This is effected with SO_RCVBUF socket option. Timestamp
option: This option is needed for high speed connections to prevent
possible data corruption caused by lost packets that then
reappears. The latter two are sometimes called the "RFC 1323
options," as that RFC [Jacobson, Braden, and Borman 1992] specifies
the options. They are also called the "long fat pipe options,"
since a network with either a high bandwidth or a long delay is
called a long fat pipe.
11. Explain the concept of port in computer networking.In
computer networking a port is an application-specific or
process-specific software construct serving as a communications
endpoint in a computer's host operating system. A port is
associated with an IP address of the host, as well as the type of
protocol used for communication. The purpose of ports is to
uniquely identify different applications or processes running on a
single computer and thereby enable them to share a single physical
connection to a packet-switched network like the Internet. The
protocols that primarily use ports are the Transport Layer
protocols, such as the Transmission Control Protocol (TCP) and the
User Datagram Protocol (UDP) of the Internet Protocol Suite.
12.Explain the concept of port numbers.Port Numbers A port is
identified for each address and protocol by a 16-bit number,
commonly known as the port number. The port number, added to a
computer's IP address, completes the destination address for a
communications session. That is, data packets are routed across the
network to a specific destination IP address, and then, upon
reaching the destination computer, are further routed to the
specific process bound to the destination port number. Both TCP and
UDP use 16-bit integer port numbers to differentiate between these
processes.
7
When a client wants to contact a server, the client must
identify the server with which it wants to communicate. Both TCP
and UDP define a group of well-known ports to identify well-known
services. Clients, on the other hand, use ephemeral ports, that is,
short-lived ports. The Internet Assigned Numbers Authority (IANA)
maintains a list of port number assignments.
The port numbers are divided into three ranges: 1. 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. For example, ports 6000 through
6063 are assigned for an X Window server for both protocols, even
though all implementations currently use only TCP. 3. The dynamic
or private ports: 49152 through 65535. The IANA says nothing about
these ports. These are what we call ephemeral ports.
TOPIC : Introduction to Sockets Socket address Structures
1)What is Socket Address Structure? There are various structures
which are used in Unix Socket Programming to hold information about
the address and port and other information. Most socket functions
require a pointer to a socket address structure as an argument.
Each supported protocol suite defines its own socket address
structure Socket address structures are an integral part of every
network program. We allocate them, fill them in, and pass pointers
to them to various socket functions. Sometimes we pass a pointer to
one of these structures to a socket function and it fills in the
contents.8
We always pass these structures by reference (ie. we pass a
pointer to the structure, not the structure itself ), and we always
pass the size of the structure as another argument. When a socket
function fills in a structure, the length is also passed by
reference, so that its value can be updated by the function. We
call these value-result arguments. Always set the structure
variables to NULL (i.e. '\0' ) by using memset() or bzero()
functions otherwise it may get unexpected junk values in your
structure.
2)Explain the different types of socket address Structures ?Most
socket functions require a pointer to a socket address structure as
an argument. Each supported protocol suite defines its own socket
address structure. IPv4 Socket Address Structure An IPv4 socket
address structure, commonly called an "Internet socket address
structure," is named sockaddr_in and is defined by including the
header. The POSIX definition of IPv4 SAS(Socket Address Structure)
is shown below. struct in_addr { in_addr_t s_addr; }; 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]; }; sin_len,
added in 4.3 BSD, is not normally supported by many vendors. It
facilitates handling of variable length socket address structures.
Various data types that are commonly used are listed below:
Description Signed 8 bit integer Unsigned 8 bit integer Header
9
/* 32-bit IPv4 address */ /* network byte ordered */
/* length of structure (16) */ /* AF_INET */ /* 16-bit TCP/UDP
port number */ /* network byte ordered */ /* 32-bit IPv4 address */
/* network byte ordered */ /* unused */
DataType int8_t uint8_t
int16_t uint16_t int32_t uint32_t sa_family_t socklen_t
in_addr_t in_port_t
Signed 16 bit integer Unsigned 8 bit integer Signed 32 bit
integer Unsigned 32 bit integer Address family of socket address
structure Length of socket address,normally uint32_t IPv4 address,
normally uint32_t TCP or UDP port normally uint16_t
The POSIX specification requires only three members in the
structure: sin_family, sin_addr, and sin_port. Both the IPv4
address and the TCP or UDP port number are always stored in the
structure in network byte order. The 32-bit IPv4 address can be
accessed in two different ways. For example, if serv is defined as
an Internet socket address structure, then serv.sin_addr references
the 32-bit IPv4 address as an in_addr structure, while
serv.sin_addr.s_addr references the same 32-bit IPv4 address as an
in_addr_t (typically an unsigned 32-bit integer).
The sin_zero member is unused, but we always set it to 0 when
filling in one of these structures.sin_family refers to the
protocol family and the containd constant values .For eg AF_INET
refers to IPv4 protocols.
Generic Socket Address Structure A socket address structure is
always passed by reference when passed as an argument to any socket
functions. 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. The solution chosen was to
define a generic socket address structure in the header, which is
as shown below :
struct sockaddr { uint8_t sa_family_t char };
sa_len; sa_family; sa_data[14];
/* address family: AF_xxx value */ /* protocol-specific address
*/
10
The socket functions are then defined as taking a pointer to the
generic socket address structure, as shown here in the ANSI C
function prototype for the bind function: int bind(int, struct
sockaddr *, socklen_t);
This requires that any calls to these functions must cast the
pointer to the protocol-specific socket address structure to be a
pointer to a generic socket address structure. For example, struct
sockaddr_in serv; /* IPv4 socket address structure */ * fill in
serv{} */ bind(sockfd, (struct sockaddr *) &serv,
sizeof(serv));
From an application programmer's point of view, the only use of
these generic socket address structures is to cast pointers to
protocol-specific structures. From the kernel's perspective,
another reason for using pointers to generic socket address
structures as arguments is that the kernel must take the caller's
pointer, cast it to a struct sockaddr *, and then look at the value
of sa_family to determine the type of the structure.
IPv6 Socket Address Structure Defined by # include header. The
structure is shown below: struct in6_addr { uint8_t
s6_addr[16];
/* 128-bit IPv6 address */ /* network byte ordered */
#define
}; SIN6_LEN
/* required for compile-time tests */
struct sockaddr_in6 { uint8_t sin6_len; / sa_family_t
sin6_family; in_port_t sin6_port; uint32_t sin6_flowinfo; struct
in6_addr sin6_addr; uint32_t sin6_scope_id; };
* length of this struct (28) */ /* AF_INET6 */ /* transport
layer port# */ /* network byte ordered */ /* flow information,
undefined */ /* IPv6 address */ /* network byte ordered */ /* set
of interfaces for a scope */
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.
11
The members in this structure are ordered so that if the
sockaddr_in6 structure is 64bit 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 two fields: o The low-order 20
bits are the flow label o The high-order 12 bits are reserved The
sin6_scope_id identifies the scope zone in which a scoped address
is meaningful, most commonly an interface index for a link-local
address.
New Generic Socket Address Structure A new generic socket
address structure was defined as part of the IPv6 sockets API, to
overcome some of the shortcomings of the existing struct sockaddr.
Unlike the struct sockaddr, the new struct sockaddr_storage is
large enough to hold any socket address type supported by the
system. The sockaddr_storage structure is defined by including the
header, which is as show below :
struct sockaddr_storage { uint8_t ss_len; /* length of this
struct (implementation dependent) */ sa_family_t ss_family; /*
address family: AF_xxx value */ /* implementation-dependent
elements to provide: * a) alignment sufficient to fulfill the
alignment requirements of * all socket address types that the
system supports. * b) enough storage to hold any type of socket
address that the * system supports. */ }; The sockaddr_storage type
provides a generic socket address structure that is different from
struct sockaddr in two ways: If any socket address structures that
the system supports have alignment requirements, the
sockaddr_storage provides the strictest alignment requirement. The
sockaddr_storage is large enough to contain any socket address
structure that the system supports. The sockaddr_storage must be
cast or copied to the appropriate socket address structure for the
address given in ss_family to access any other fields
12
3.Compare the different Socket Address Structures.The figure
below shows a comparison of the five socket address structures:
IPv4, IPv6, Unix domain, datalink , and storage. In this figure, we
assume that the socket address structures all contain a one-byte
length field, that the family field also occupies one byte, and
that any field that must be at least some number of bits is exactly
that number of bits.
Two of the socket address structures are fixed-length, while the
Unix domain structure and the datalink structure are
variable-length. To handle variable-length structures, whenever we
pass a pointer to a socket address structure as an argument to one
of the socket functions, we pass its length as another
argument.
4.Explain Value-Result Arguments. 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 so that its
value can be updated by the function. The way in which the length
is passed depends on which direction the structure is being passed:
from the process to the kernel, or vice versa.
Socket address structure passed from process to kernel Three
functions bind, connect, and sendto pass a socket address structure
from the process to the kernel.13
One argument to these three functions is the pointer to the
socket address structure and another argument is the integer size
of the structure. 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 : Socket address structure passed from process to
kernel
Socket address structure passed from kernel to process Four
functions, accept, recvfrom, getsockname, and getpeername, pass a
socket address structure from the kernel to the process. 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. 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 :Socket address structure passed from kernel to
process
14
TOPIC : Byte Ordering Functions
1.Explain little-endian byte order and big-endian byte
order.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
bigendian byte order.
Figure :Little-endian byte order and big-endian byte order for a
16-bit integer
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.
2.Explain the need for byte ordering functions.What are the four
functions used to convert between the 2 byte orders. There is no
standard between the two byte orderings- "little-endian" and
"bigendian" and there are systems that use both formats.The byte
ordering used by a given system is referred to as the host byte
order. Network programmers must deal with these byte ordering
differences because networking protocols must specify a network
byte order. For example, in a TCP segment, there is a 16-bit port
number and a 32-bit IPv4 address. The15
sending protocol stack and the receiving protocol stack must
agree on the order in which the bytes of these multi byte fields
will be transmitted. POSIX specification say that certain fields in
the socket address structures must be maintained in network byte
order. The following four functions to convert between these two
byte orders.
TOPIC : Byte Manipulation Functions
1.What are Byte Manipulation Functions?There are two groups of
functions that operate on multibyte fields, without interpreting
the data, and without assuming that the data is a null-terminated C
string. We need these types of functions when dealing with socket
address structures because we need to manipulate fields such as IP
addresses, which can contain bytes of 0, but are not C character
strings. The first group of functions, whose names begin with b
(for byte), are from 4.2BSD and are still provided by almost any
system that supports the socket functions. The second group of
functions, whose names begin with mem (for memory), are from the
ANSI C standard and are provided with any system that supports an
ANSI C library. First Berkeley derived functions are shown.
#include void bzero(void *dest, size_tnbytes); void bcopy(const
void *src, void *dest, size_tnbytes); int bcmp(const void *ptr1,
const void *ptr2, size_tnbytes); Returns: 0 if equal, nonzero if
unequal const qualifier indicates that the pointer with this
qualification, src, ptr1, ptr2 are not modified by the function..
That is memory pointed to by the cost pointer is read but not
modified by the function. bzero ( ) sets the specified number of
bytes to 0 in the destination. This function is often used to
initialize a socket address structure to 0. bcopy ( ) moves the
specified number of bytes16
from the source to the destination. bcmp ( ) compares two
arbitrary byte strings . The return value is zero if the two byte
strings are identical; otherwise it is nonzero. The following
functions are the ANSI C functions: #include void *memset(void
*dest, intc, size_tlen); void *memcpy(void *dest, const void *src,
size_tnbytes); int memcmp(const void *ptr1, const void *ptr2,
size_tnbytes); Returns: 0 if equal, 0 if unequal (see text) memset
sets the specified number of bytes to the value c in the
destination. memcpy is similar to bcopy, but the order of the two
pointer arguments is swapped. bcopy correctly handles overlapping
fields, while the behavior of memcpy is undefined if the source and
destination overlap. The ANSI C memmove function must be used when
the fields overlap. memcmp compares two arbitrary byte strings and
returns 0 if they are identical. If not identical,the return value
is either greater than 0 or less than 0, depending on whether the
first unequal byte pointed to by ptr1 is greater than or less than
the corresponding byte pointed to by ptr2.The comparison is done
assuming the two unequal bytes are unsigned chars.
TOPIC : Address Conversion Functions
1.Explain the Address Conversion Functions.There are two groups
of address conversion function that convert the Internet address
between ASCII strings (readable form) to network byte ordered
binary values (values that are stored in socket address structures)
and vice versa. 1) inet_aton, inet_ntoa, and inet_addr convert an
IPv4 address from a dotted-decimal string (e.g., "206.168.112.96")
to its 32-bit network byte ordered binary value. You will probably
encounter these functions in lots of existing code. 2) The newer
functions, inet_pton and inet_ntop, handle both IPv4 and IPv6
addresses. inet_aton( ) : converts the C character strings pointed
to by the strptr into its 32 bit binary network byte ordered value
which is stored through the pointer addptr. If successful 1 is
returned otherwise a 0. #include int inet_aton (const * strptr,
strut in_addr * addptr);
inet_addr( ) : does the same conversion, returning the 32 bit
binary network byte ordered value as the return value. Although the
IP address (0.0.0.0 through17
255.255.255.255) are al valid addresses, the functions returns
the constant INADDR_NONE 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. This is
deprecated and the new code should use inet_aton instead
inet_ntoa ( ) : The function 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. This
function takes structure as its arguments, not a pointer to a
structure. (This is rare). inet_pton ( ) and inet_ntop( ) : These
two functions are new with the IPv6 and work with both IPv4 and
IPv6 addresses. The letter p and n stands for presentation and
numeric. Presentation format for an address is often ASCII string
and the numeric format is the binary value that goes into a socket
address structure.
# include 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, cost void
*addrptr, char *strptr, size_t len); Returns: pointer to result if
OK, NULL on error The family argument for both function is either
AF-INET or AF_ INET6. If family is not supported, both functions
return an error with errno set to EAFNOSUPPORT. inet_pton ( ) : The
first functions tries to convert the string pointed to by strptr,
storing the binary results through the pointer addrptr. IF
successful, the return value is 1. If the input string is not valid
presentation format for the specified family, 0 is returned.
inet_ntop( ) : This function 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, following two
definitions are defined by including the header:
#define INET_ADDRSTRLEN 16 /*for IPv4 dotted-decimal */ #define
INET6_ADDRSTRLEN 46 /*for IPv6 hex string */18
If LEN is too small to hold the resulting presentation format
including the terminating null, a null pointer is returned and
errno is set to ENOSPC. The strptr argument to inet_ntop cannot be
a null pointer. The caller must allocate memory for the destination
and specify its size. On success this pointer is the return value
of the function. Following figure shows the summary of address
conversion functions.
2.Explain sock_ntop function.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. To solve this problem, sock_ntop() is used which
takes pointer to a socket address structure as an argument, calls
the appropriate function and the presentation address is
returned.
#include "unp.h" char *sock_ntop(const struct sockaddr
*sockaddr, socklen_t addrlen); Returns: non-null pointer if OK,
NULL on error
19
TOPIC : Elementary TCP Sockets socket, connect, bind, listen,
accept, read, write,close functions
1.Explain readn, writen, and readline FunctionsStream sockets
(e.g., TCP sockets) exhibit a behavior with the read and write
functions that differ from normal file I/O. A read or write on a
stream socket might input or output fewer bytes than requested, but
this is not an error condition. The reason is that buffer limits
might be reached for the socket in the kernel. All that is required
to input or output the remaining bytes is for the caller to invoke
the read or write function again. Some versions of UNIX also
exhibit this behavior when writing more than 4,096 bytes to a pipe.
This scenario is always a possibility on a stream socket with read,
but is normally seen with write only if the socket is nonblocking.
Nevertheless, we always call our writen function instead of write,
in case the implementation returns a short count. We use the
following three functions when ever we read from or write to a
stream socket : #include "unp.h" ssize_t readn(int filedes, void
*buff, size_t nbytes); ssize_t writen(int filedes, const void
*buff, size_t nbytes); ssize_t readline(int filedes, void *buff,
size_t maxlen); All return: number of bytes read or written, 1 on
error
2.What are the elementary socket functions required to write a
complete TCP client and server.The following figures shows the
Socket functions for elementary TCP
client/serversocket(),connect(),bind(),listen(),accept(),read(),write(),close()
functions. First server is started, then sometimes later a client
is started that connects to the server. The client sends a request
to the server, the server processes the request, and the server
sends back reply to the client. This continues until the client
closes its end of the connection, which sends an end of file
notification to the server. The server then closes its end of the
connections and either terminates or waits for a new
connection.
20
3.Explain the socket() function.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
int socket (int family, int type, int protocol); Returns:
non-negative descriptor if OK, -1 on error family specifies the
protocol family and is one of the constants AF_INET , AF_INET6 ,
AF_LOCAL, AF_ROUTE or AF_KEY . socket type is one of the constants
SOCK_STREAM ,SOCK_DGRAM ,SOCK_SEQPACKET,SOCK_RAW . The protocol
argument to the socket function should be set to the specific
protocol type (IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP) or 0 to
select the system's default for the given combination of family and
type.21
4.Explain the connect() function.The connect function is used by
a TCP client to establish a connection with a TCP server. #include
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t
addrlen); Returns: 0 if OK, -1 on error sockfd is a socket
descriptor returned by the socket function. The second and third
arguments are a pointer to a socket address structure and its size.
The socket address structure must contain the IP address and port
number of the server. In the case of a TCP socket, the connect
function initiates TCP's three-way handshake. The function returns
only when the connection is established or an error occurs. There
are several different error returns possible.
5.What are the different types of errors possible when a TCP
socket Initiates a connect() function. If the client TCP receives
no response to its SYN segment, ETIMEDOUT is returned. Some systems
provide administrative control over this timeout. If the server's
response to the client's SYN is a reset (RST), this indicates that
no process is waiting for connections on the server host at the
port specified (i.e., the server process is probably not running).
This is a hard error and the error ECONNREFUSED is returned to the
client as soon as the RST is received. An RST is a type of TCP
segment that is sent by TCP when something is wrong. Three
conditions that generate an RST are: when a SYN arrives for a port
that has no listening server , when TCP wants to abort an existing
connection, and when TCP receives a segment for a connection that
does not exist.
If the client's SYN elicits an ICMP "destination unreachable"
from some intermediate router, this is considered a soft error. The
client kernel saves the message but keeps sending SYNs with the
same time between each SYN as in the first scenario. If no response
is received after some fixed amount of time , the saved ICMP error
is returned to the process as either EHOSTUNREACH or ENETUNREACH.
It is also possible that the remote system is not reachable by any
route in the local system's forwarding table, or that the connect
call returns without waiting at all.
6.Explain the bind() function. 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 int bind (intsockfd, const struct sockaddr
*myaddr, socklen_taddrlen); Returns: 0 if OK,-1 on error22
bind assigns a protocol address to a socket, and what that
protocol address means depends on the protocol. The second argument
is a pointer to a protocol-specific address, and the third argument
is the size of this address structure. With TCP, calling bind lets
us specify a port number, an IP address, both, or neither. Servers
bind their well-known port when they start.If a TCP client or
server does not do this, the kernel chooses an ephemeral port for
the socket when either connect or listen is called. It is normal
for a TCP client to let the kernel choose an ephemeral port, unless
the application requires a reserved port but it is rare for a TCP
server to let the kernel choose an ephemeral port, since servers
are known by their well-known port.
7.Explain the listen() function.The listen function is called
only by a TCP server and it performs two actions: When a socket is
created by the socket function, it is assumed to be an active
socket, that is, a client socket that will issue a connect. The
listen function converts an unconnected socket into a passive
socket, indicating that the kernel should accept incoming
connection requests directed to this socket. In terms of the TCP
state transition diagram , the call to listen moves the socket from
the CLOSED state to the LISTEN state. The second argument to this
function specifies the maximum number of connections the kernel
should queue for this socket.
#include #int listen (intsockfd, intbacklog); Returns: 0 if OK,
-1 on error This function is normally called after both the socket
and bind functions and must be called before calling the accept
function. The kernel maintains two queues and the backlog argument
to the listen function has historically specified the maximum value
for the sum of both queues.These queues are : An incomplete
connection queue, which contains an entry for each SYN that has
arrived from a client for which the server is awaiting completion
of the TCP three way handshake. These sockets are in the SYN_RECD
state. A Completed Connection Queue which contains an entry for
each client with whom three handshake has completed. These sockets
are in the ESTABLISHED state.
8.Explain the 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, the23
process is put to sleep (assuming the default of a blocking
socket). #include int accept (intsockfd, struct sockaddr *cliaddr,
socklen_t *addrlen); Returns: non-negative descriptor if OK, -1 on
error The cliaddr and addrlen arguments are used to return the
protocol address of the connected peer process (the client).
addrlen is a value-result argument : before the call, we set the
integer value pointed to by *addrlen to the size of the socket
address structure pointed to by cliaddr and on return this integer
value contains the actual number of bytes stored by the kernel in
the socket address structure. If accept is successful, its return
value is a brand new descriptor that was automatically created by
the kernel. This new descriptor refers to the TCP connection with
the client.
9.Explain the fork() function.: fork is the function that
enables the Unix to create a new process #inlcude pid_t fork
(void); Returns 0 in child, process ID of child in parent, -1 on
error There are two typical uses of fork function: 1. A process
makes a copy of itself so that one copy can handle one operation
while the other copy does another task. This is normal way of
working in a network servers. 2. A process wants to execute another
program. Since the only way to create a new process is by calling
fork, the process first calls fork to make a copy of itself, and
then one of the copies(typically the child process) calls exec
function to replace itself with a the new program. This is typical
for program such as shells. 3. fork function although called once,
it returns twice. It returns once in the calling process (called
the parent) with a return value that is process ID of the newly
created process (the child). It also returns once in the child,
with a return value of 0. Hence the return value tells the process
whether it is the parent or the child. 4. The reason fork returns 0
in the child, instead of parents process ID is because a child has
only one parent and it can always obtain the parents process ID by
calling getppid .A parent, on the other hand, can have any number
of children, and there is no way to obtain the process Ids of its
children. If the parent wants to keep track of the process Ids of
all its children, it must record the return values form fork.
10.Explain the exec() function.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. 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. The process that calls the exec is the calling
process and the newly executed program as the new program. The
differences in the six exec functions are: a. whether the program
file to execute is specified by a file name or a pathname. b.
Whether the arguments to the new program are listed one by one or
reference through an24
array of pointers, and c. Whether the environment of the calling
process is passed to the new program or whether a new environment
is specified. #include int execl (const char *pathname, const char
arg 0, / (char *) 0 */); int execv (const char *pathname, char
*const argv[ ]); int execle (const char *pathname, const char *arg
0, ./ * (char *)0,char *const envp[] */); int execve (const char
*pathname, char *const arg [], char *const envp[]); int execlp
(const char *filename, const char arg 0, / (char *) 0 */); int
execvp (const char *filename, char *const argv[]); These functions
return to the caller only if an error occurs. Otherwise control
passes to the start of the new program, normally the main
function.
11.Explain the close() function.The normal Unix close function
is also used to close a socket and terminate a TCP connection.
#include int close (intsockfd); Returns: 0 if OK, -1 on error
The default action of close with a TCP socket is to mark the
socket as closed and return to the process immediately. The socket
descriptor is no longer usable by the process: It cannot be used as
an argument to read or write. But, TCP will try to send any data
that is already queued to be sent to the other end, and after this
occurs, the normal TCP connection termination sequence takes
place
TOPIC : Iterative Server Concurrent Server
1.Differentiate Iterative & Concurrent servers.An iterative
server handles both the connection request and the transaction
involved in the call itself. Iterative servers are fairly simple
and are suitable for transactions that do not last long. However,
if the transaction takes more time, queues can build up quickly. In
the following figure , after Client A starts a transaction with the
server, Client B cannot make a call until A has finished
25
Figure : An iterative server
For lengthy transactions, a different sort of server is needed
the concurrent server, as shown in the figure below. Here, Client A
has already established a connection with the server, which has
then created a child server process to handle the transaction. This
allows the server to process Client B's request without waiting for
A's transaction to complete. More than one child server can be
started in this way.
Figure : A concurrent server
********************************************************
26
27