2. PART I
Sockets:
Address structures,
value – Result Arguments,
Byte Ordering Function and Manipulation
Function ,
Related Functions ,
Elementary TCP Sockets – Socket, connect, bind,
listen, and accept, fork and exec functions,
concurrent servers.
Close function and Related function.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
2
3. What is a Socket?
Sockets allow communication between two
different processes on the same or different
machines.
It's a way to talk to other computers using
standard Unix file In Unix, every I/O action is
done by writing or reading a file descriptor.
Sockets were first introduced in 2.1BSD and
subsequently refined into their current form
with 4.2BSD. The sockets feature is now
available with most current UNIX system
releases.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
3
4. Where is Socket Used?
A Unix Socket is used in a client-server
application framework. A server is a process
that performs some functions on request
from a client.
Most of the application-level protocols like
FTP, SMTP, and POP3 make use of sockets
to establish connection between client and
server and then for exchanging data.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
4
5. Socket Types
There are four types of sockets available to the
users.
The first two are most commonly used and the
last two are rarely used.
1. Stream Sockets
2. Datagram Sockets
3. Raw Sockets
4. Sequenced Packet Sockets
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
5
6. Socket Types
Stream Sockets − Delivery in a networked
environment is guaranteed. If you send through
the stream socket three items "A, B, C", they will
arrive in the same order − "A, B, C". These
sockets use TCP (Transmission Control Protocol)
for data transmission. If delivery is impossible, the
sender receives an error indicator.
Datagram Sockets − Delivery in a networked
environment is not guaranteed. They're
connectionless because you don't need to have
an open connection as in Stream Sockets − you
build a packet with the destination information
and send it out. They use UDP (User Datagram
Protocol). 5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
6
7. Socket Types(Cont’d)
Raw Sockets −
These provide users access to the underlying
communication protocols, which support socket
abstractions.
These sockets are normally datagram oriented,
though their exact characteristics are dependent on
the interface provided by the protocol
Sequenced Packet Sockets −
They are similar to a stream socket, Sequenced-
packet sockets allow the user to manipulate the
Sequence Packet Protocol (SPP) or
Internet Datagram Protocol (IDP)
headers on a packet or a group of packets, either by
writing a prototype header along with whatever data is
to be sent.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
7
8. IP Addresses
The IP host address, or more commonly just IP address, is
used to identify hosts connected to the Internet. IP stands
for Internet Protocol and refers to the Internet Layer of the
overall network architecture of the Internet.
Each IP address uniquely identifies the participating user
network, the host on the network, and the class of the
user network.
An IP address is usually written in a dotted-decimal
notation of the form N1.N2.N3.N4, where each Ni is a
decimal number between 0 and 255 decimal (00 through
FF hexadecimal).
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
8
9. Address Classes
Class Leftmost bits Start address Finish address
A 0xxx 0.0.0.0 127.255.255.255
B 10xx 128.0.0.0 191.255.255.255
C 110x 192.0.0.0 223.255.255.255
D 1110 224.0.0.0 239.255.255.255
E 1111 240.0.0.0 255.255.255.255
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
9
10. Client Process and Server Process
Client Process
This is the process, which typically makes a request
for information. After getting the response, this
process may terminate or may do some other
processing.
Example, Internet Browser works as a client
application, which sends a request to the Web Server
to get one HTML webpage.
Server Process
This is the process which takes a request from the
clients. After getting a request from the client, this
process will perform the required processing, gather
the requested information, and send it to the
requestor client. Once done, it becomes ready to
serve another client. Server processes are always
alert and ready to serve incoming requests.5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
10
11. Types of Server
There are two types of servers you can have −
Iterative Server − This is the simplest form of
server where a server process serves one client
and after completing the first request, it takes
request from another client. Meanwhile, another
client keeps waiting.
Concurrent Servers − This type of server runs
multiple concurrent processes to serve many
requests at a time because one process may take
longer and another client cannot wait for so long.
The simplest way to write a concurrent server
under Unix is to fork a child process to handle
each client separately.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
11
12. How to Make Client
The system calls for establishing a connection are
somewhat different for the client and the server, but
both involve the basic construct of a socket. Both the
processes establish their own sockets.
The steps involved in establishing a socket on the client
side are as follows −
Create a socket with the socket() system call.
Connect the socket to the address of the server using
the connect() system call.
Send and receive data. There are a number of ways
to do this, but the simplest way is to use
the read() and write() system calls.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
12
13. How to make a Server
The steps involved in establishing a socket on the
server side are as follows −
Create a socket with the socket() system call.
Bind the socket to an address using
the bind() system call. For a server socket on the
Internet, an address consists of a port number on the
host machine.
Listen for connections with the listen() system call.
Accept a connection with the accept() system call.
This call typically blocks the connection until a client
connects with the server.
Send and receive data using
the read() and write() system calls.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
13
14. Client and Server Interaction
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
14
15. Socket Structures
Various structures are used in Unix Socket
Programming to hold information about the
address and port, and other information.
Sockaddr:
The first structure is sockaddr that holds the
socket information
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
15
16. Socket Address Structures
The name of socket address structures begin
with sockaddr_ and end with a unique suffix for
each protocol suite.
IPv4 Socket Address Structure:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
16
17. Socket Address Structures(cont’d)
The five socket functions that pass a socket address
structure from the kernel to the process,
accept, recvfrom, recvmsg, getpeername,
and getsockname, all set the sin_len member before
returning to the process.
POSIX requires only three members in the structure:
sin_family,
sin_addr, and
sin_port.
The sin_zero member is unused. By convention, we
always set the entire structure to 0 before filling it in.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
17
18. Socket Address Structures(cont’d)
Generic Socket Address Structure
A generic socket address structure in
the <sys/socket.h> header:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
18
20. Socket Address Structures(cont’d)
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 sin6_flowinfo member is divided into two fields:
The low-order 20 bits are the flow label
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
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
20
21. Comparison of Socket Address Structures:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
21
22. Value Result Arguments
value result argument the kernel is able to make changes
to the argument passed.
When a socket address structure is passed to any socket
function, it is always passed by reference.
The length of the structure is also passed as an argument.
The way in which the length is passed depends on which
direction the structure is being passed:
1.process to the kernel
2.kernel to the process
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
22
23. Value Result Arguments
1.From process to kernel:
bind, connect, and sendto functions pass a socket address
structure from the process to the kernel.
Arguments to these functions:
The pointer to the socket address structure
The integer size of the structure
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
23
24. Value Result Arguments(cont’d)
The three functions
1. bind()
2. connect()
3. sendto()
pass a socket address
structure from the process to
the kernal.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
24
25. Value Result Arguments(con’d)
2.From kernel to process:
accept, recvfrom, getsockname, and getpeername functions
pass a socket address structure from the kernel to the
process.
Arguments to these functions:
The pointer to the socket address structure
The pointer to an integer containing the size of the
structure.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
25
26. Value Result Arguments(cont’d)
It tells the process how much
information the kernal actually
stored in the structure).
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
26
27. Value Result Arguments(cont’d)
Value-result argument :the size changes from an integer
to be a pointer to an integer because the size is both a value
when the function is called and a result when the function
returns.
As a value: it tells the kernel the size of the structure
As a result: it tells the process how much information the
kernel actually stored in the structure
If the socket address structure is fixed-length, the value
returned by the kernel will always be that fixed size
With variable length socket address structure, the value
returned can be less than the maximum size of the structure.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
27
28. Byte Ordering Functions
For a 16-bit integer that is made up of 2 bytes, there are two ways to
store the two bytes in memory:
Little-endian order: low-order byte is at the starting address.
Big-endian order: high-order byte is at the starting address.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
28
30. Byte Manipulation Functions
Dealing with sockets address structures as we need to manipulate fields
such as IP addresses
There are two groups of functions that operate on multi byte fields,
without interpreting the data, and
without assuming that the data is a null terminated C string.
The functions whose names
begin with b (for byte) (from 4.2BSD)
begin with mem (for memory) (from ANSI C)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
30
33. Related Functions
These two functions are new with IPv6 and work with both IPv4 and IPv6
addresses. We use these two functions throughout the text. The letters "p" and "n"
stand 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.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
33
34. Elementary TCP Sockets Client Server scenario
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
34
35. Elementary TCP Sockets Client Server scenario
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
35
36. Elementary TCP Sockets Client Server scenario
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.).
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
36
37. Connect
The connect function is used by a TCP client to establish a connection
with a TCP server.
sockfd is a socket descriptor returned by the socket function.
The servaddr and addrlen arguments are a pointer to a socket address
structure (which contains the IP address and port number of the server)
and its size
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
37
38. Connect
Examples:
1. Nonexistent host on the local subnet
2. No server process running
3. Destination not reachable on the Internet
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
38
39. Bind
The bind function assigns a local protocol address to a socket. 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.
The second argument myaddr is a pointer to a protocol-specific addres
The third argument addrlen is the size of this address structure.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
39
41. listen Function
The listen function is called only by a TCP server and it performs two
actions:
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.
When a socket is created by the socket function (and before
calling listen), it is assumed to be an active socket, that is, a client
socket that will issue a connect.
The second argument backlog to this function specifies the maximum
number of connections the kernel should queue for this socket.
This function is normally called after both the socket and bind functions
and must be called before calling the accept function.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
41
42. listen Function(Cont’d)
Connection queues:
To understand the backlog argument, we must realize that for a
given listening socket, the kernel maintains two queues:
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_RCVD state.
A completed connection queue, which contains an entry for
each client with whom the TCP three-way handshake has
completed. These sockets are in the ESTABLISHED state
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
42
45. Elementary TCP Sockets Client Server scenario
The backlog, defines the maximum length to which the queue .If a
connection request arrives when the queue is full, the client may receive an
error with an indication of ECONNREFUSED.
At this point, connection is established between client and server, and
they are ready to transfer data.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
45
46. Elementary TCP Sockets Client Server scenario
accept Function:
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.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
46
47. Elementary TCP Sockets Client Server scenario
If successful, accept returns a new descriptor
automatically created by the kernel. This new
descriptor refers to the TCP connection with the
client.
The listening socket is the first argument
(sockfd) to accept.
The connected socket is the return value from
accept the connected socket.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
47
48. Elementary TCP Sockets Client Server scenario
It is important to differentiate between these two sockets:
A given server normally creates only one listening socket,
which then exists for the lifetime of the server.
The kernel creates one connected socket for each client
connection that is accepted (for which the TCP three-way
handshake completes).
When the server is finished serving a given client, the
connected socket is closed.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
48
49. Elementary TCP Sockets Client Server scenario
This function returns up to three values:
An integer return code that is either a new socket descriptor
or an error indication,
The protocol address of the client process (through the
cliaddr pointer),
The size of this address (through the addrlen pointer).
Note:If we are not interested in having the protocol address
of the client returned, we set both cliaddr and addrlen to
null pointers
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
49
50. fork and exec Functions
To create new process.
fork() creates a new process by duplicating the calling
process, The new process, referred to as child, is an exact
duplicate of the calling process, referred to as parent,
except for the following:
The child has its own unique process ID, and this PID does
not match the ID of any existing process group.
The child’s parent process ID is the same as the parent’s
process ID.
The child does not inherit its parent’s memory locks and
semaphore adjustments.
The child does not inherit outstanding asynchronous I/O
operations from its parent nor does it inherit any
asynchronous I/O contexts from its parent.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
50
51. fork and exec Functions
Return value of fork()
On success, the PID of the child process is returned in the
parent, and 0 is returned in the child. On failure, -1 is
returned in the parent, no child process is created, and errno
is set
The exec() family of functions replaces the current process
image with a new process image. It loads the program into
the current process space and runs it from the entry point.
appropriately.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
51
52. fork vs exec
fork starts a new process which is a copy of the one that
calls it, while exec replaces the current process image with
another (different) one.
Both parent and child processes are executed
simultaneously in case of fork() while Control never returns
to the original program unless there is an exec() error.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
52
53. Concurrent Servers
The server described in is an iterative server. But when a client
request can take longer to service, we do not want to tie up a single
server with one client; we want 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.
When a connection is established, accept returns, the server calls fork,
and the child process services the client (on connfd, the connected
socket) and the parent process waits for another connection (on listenfd,
the listening socket).
The parent closes the connected socket since the child handles the new
client.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
53
54. Visualizing the sockets and connection
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
54
55. Visualizing the sockets and connection
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
55
56. Visualizing the sockets and connection
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
56
57. close Function
close Function:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
57
58. getsockname and getpeername Functions:
getsockname and getpeername Functions:
getsockname returns the local protocol address associated
with a socket.
getpeername returns the foreign protocol address associated
with a socket.
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
58