Chapter - 1
Chapter - 1
Chapter - 1
UNP - Introduction
Client
Server
Network application: client & server
Client
Client
Server
Client
Server handling multiple clients at the same time
User
process
Protocol
stack
within
kernel
Web Client
TCP
IP
Ethernet Driver
Application Protocol
TCP Protocol
IP Protocol
Ethernet Protocol
Web Server
TCP
IP
Ethernet driver
Application
Layer
Transport
Layer
Network Layer
Datalink Layer
Sockets are a way to talk with other machines on the network using UNIX file
descriptors.
A file descriptor in UNIX is just a number that is associated with an open file and
is used to access it, similarly socket file descriptor lets us access different
machines on the network.
And this file descriptor is provided by the function call socket( ).
Socket has a generic address structure, so that all protocols can understand it.
sockfd is the socket file descriptor.
char recvline[MAXLINE + 1] set to maxline +1 for the null terminator that is
required for char array.
struct sockaddr_in {
uint_8
sin_len;
/* length of structure */
sa_family_t
sin_family;
/* address family is AF_INET for IP */
in_port_t
sin_port;
/* 16-bit port number of UDP/TCP
network byte ordered */
struct in_addr
sin_addr;
/* 32-bit IP address
network byte ordered */
char
sin_zero[8];
/* unused */
};
struct in_addr{
in_addr_t
};
s_addr;
/* 32-bit IP address
network byte ordered */
Address A
Address A+1
Address A+1
AF_INET
IPv4 protocol (AF stands for address family)
AF_INET6
IPv6 protocol
AF_LOCAL UNIX domain protocols
AF_ROUTE Routing sockets
AF_KEY
key sockets (used for encryption-key exchange)
TYPE:
SOCK_STREAM
SOCK_DGRAM
SOCK_RAW
IP Address
Port Number
Result
Wildcard
Wildcard
Non-zero
IP address
IP address
Non-zero
Listen function is called after socket and bind functions and must be called before
calling the accept function.
After these three function calls, socket, bind and listen, on the server side, the
socket descriptor is said to be listening descriptor.
A new descriptor is returned by accept for each client that connects to the server.
time( ) function returns the number of seconds that have elapsed since 00:00:00
january 1, 1970.
Then ctime function returns the time in human readable format:
Fri Jan 12 14:27:24 2008
snprintf is same as sprintf, except that sprintf does not check for destination buffer
overflow, which snprintf does.
When error occurs in any Unix function, the global variable errno is set to a
positive value indicating the type of error.
The value of errno is set only if an error occurs, its value is undefined if the
function does not return an error.
All the positive error values are constants with an all upper-case name beginning
with E and are defined in <sys.errno.h> header.
No error has the value 0.
Storing error in a global variable does not work with multiple threads that share all
global variables.
Application Details
Application
Presentation
User
process
Application
Session
Transport
TCP
UDP
Network
IPv4, IPv6
Datalink
Physical
Device Driver
&
Hardware
OSI Model
IP suite
Sockets
XTI
kernel
Communication details
A gap is shown between TCP and UDP to indicate that IPv6 and IPV4
can directly communicate with the application layer. Such a case is called
a raw socket.
Both sockets and XTI are interfaces from the application layer into the
transport layer.
Why both sockets and XTI are provided as an interface between layer 4
and 5?
There are two reasons for this:
The upper three layers handle all the details of the application
(FTP, Telnet, HTTP) and know little about communication details.
The lower 4 layers know little about application details but handle
the communication details (sending data, waiting for an
acknowledgement, sequencing data that arrives out of order,
calculating and verifying checksum)
The second reason is that the upper three layers form a user
process while the lower three layers are normally provided as part
of the operating system kernel.