SlideShare a Scribd company logo
1 of 58
SIKHINAM NAGAMANI(T547)
ASSISTANT PROFESSOR
IT,LBRCE.
UNIT II
Sockets
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
1
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
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
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
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
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
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
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
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
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
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
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
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
Client and Server Interaction
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
14
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
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
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
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
Socket Address Structures(cont’d)
 IPv6 Socket Address Structure:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
19
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
Comparison of Socket Address Structures:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
21
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
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
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
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
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
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
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
Byte Ordering Functions(cont’d)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
29
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
Byte Manipulation Functions (cont’d)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
31
Byte Manipulation Functions (cont’d)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
32
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
Elementary TCP Sockets Client Server scenario
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
34
Elementary TCP Sockets Client Server scenario
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
35
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
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
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
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
Bind(cont’d)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
40
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
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
listen Function(cont’d)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
43
listen Function(cont’d)
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
44
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
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
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
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
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
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
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
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
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
Visualizing the sockets and connection
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
54
Visualizing the sockets and connection
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
55
Visualizing the sockets and connection
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
56
close Function
 close Function:
5/12/2021 9:36 AM
Sikhinam Nagamani, T547,Assistant
Professor,IT, LBRCE.
57
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

More Related Content

What's hot (20)

Osi model
Osi modelOsi model
Osi model
 
Basics of tcp ip
Basics of tcp ipBasics of tcp ip
Basics of tcp ip
 
Application layer protocols
Application layer protocolsApplication layer protocols
Application layer protocols
 
computer network NCC l4dc assingment
computer network NCC l4dc assingment computer network NCC l4dc assingment
computer network NCC l4dc assingment
 
Session Layer
Session LayerSession Layer
Session Layer
 
Slides for protocol layering and network applications
Slides for protocol layering and network applicationsSlides for protocol layering and network applications
Slides for protocol layering and network applications
 
Application Layer
Application LayerApplication Layer
Application Layer
 
19 20 cn q bf inal
19 20 cn q bf inal19 20 cn q bf inal
19 20 cn q bf inal
 
The osi model
The osi modelThe osi model
The osi model
 
Unit 3
Unit 3Unit 3
Unit 3
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 
CS6551 COMPUTER NETWORKS
CS6551 COMPUTER NETWORKSCS6551 COMPUTER NETWORKS
CS6551 COMPUTER NETWORKS
 
Mod3
Mod3Mod3
Mod3
 
network administration directory access and remote access
network administration directory access and remote accessnetwork administration directory access and remote access
network administration directory access and remote access
 
Application Layer
Application LayerApplication Layer
Application Layer
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 
Remote Login and File Transfer Protocols
Remote Login and File Transfer ProtocolsRemote Login and File Transfer Protocols
Remote Login and File Transfer Protocols
 
TCP-IP PROTOCOL
TCP-IP PROTOCOLTCP-IP PROTOCOL
TCP-IP PROTOCOL
 
Data Link Layer
Data Link LayerData Link Layer
Data Link Layer
 
Technical paper
Technical paperTechnical paper
Technical paper
 

Similar to Sockets

Unit 3 Assignment 1 Osi Model
Unit 3 Assignment 1 Osi ModelUnit 3 Assignment 1 Osi Model
Unit 3 Assignment 1 Osi ModelJacqueline Thomas
 
Networking Related
Networking RelatedNetworking Related
Networking RelatedZunAib Ali
 
Basic ccna interview questions and answers ~ sysnet notes
Basic ccna interview questions and answers ~ sysnet notesBasic ccna interview questions and answers ~ sysnet notes
Basic ccna interview questions and answers ~ sysnet notesVamsi Krishna Kalavala
 
BSA 385 Week 3 Individual Assignment Essay
BSA 385 Week 3 Individual Assignment EssayBSA 385 Week 3 Individual Assignment Essay
BSA 385 Week 3 Individual Assignment EssayTara Smith
 
Bangladesh Computer Council Networking Project
Bangladesh Computer Council Networking ProjectBangladesh Computer Council Networking Project
Bangladesh Computer Council Networking ProjectMDMusab
 
Design Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring toolDesign Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring toolanuj_rakheja
 
The Network Ip Address Scheme
The Network Ip Address SchemeThe Network Ip Address Scheme
The Network Ip Address SchemeErin Rivera
 
A Deep Dive in the World of IT Networking (Part 2)
A Deep Dive in the World of IT Networking (Part 2)A Deep Dive in the World of IT Networking (Part 2)
A Deep Dive in the World of IT Networking (Part 2)Tuan Yang
 
Running head network design 1 netwo
Running head network design                             1 netwoRunning head network design                             1 netwo
Running head network design 1 netwoAKHIL969626
 
OSI and TCPIP Model
OSI and TCPIP ModelOSI and TCPIP Model
OSI and TCPIP ModelTapan Khilar
 
layering.pdf
layering.pdflayering.pdf
layering.pdfBoostHub
 

Similar to Sockets (20)

Unit 3 Assignment 1 Osi Model
Unit 3 Assignment 1 Osi ModelUnit 3 Assignment 1 Osi Model
Unit 3 Assignment 1 Osi Model
 
Bcs 052 solved assignment
Bcs 052 solved assignmentBcs 052 solved assignment
Bcs 052 solved assignment
 
Network Testing ques
Network Testing quesNetwork Testing ques
Network Testing ques
 
Ccna day1
Ccna day1Ccna day1
Ccna day1
 
Ccna day1
Ccna day1Ccna day1
Ccna day1
 
C C N A Day1
C C N A  Day1C C N A  Day1
C C N A Day1
 
TCP/IP 3RD SEM.2012 AUG.ASSIGNMENT
TCP/IP 3RD SEM.2012 AUG.ASSIGNMENTTCP/IP 3RD SEM.2012 AUG.ASSIGNMENT
TCP/IP 3RD SEM.2012 AUG.ASSIGNMENT
 
Networking Related
Networking RelatedNetworking Related
Networking Related
 
Ccna day1
Ccna day1Ccna day1
Ccna day1
 
Basic ccna interview questions and answers ~ sysnet notes
Basic ccna interview questions and answers ~ sysnet notesBasic ccna interview questions and answers ~ sysnet notes
Basic ccna interview questions and answers ~ sysnet notes
 
BSA 385 Week 3 Individual Assignment Essay
BSA 385 Week 3 Individual Assignment EssayBSA 385 Week 3 Individual Assignment Essay
BSA 385 Week 3 Individual Assignment Essay
 
Bangladesh Computer Council Networking Project
Bangladesh Computer Council Networking ProjectBangladesh Computer Council Networking Project
Bangladesh Computer Council Networking Project
 
Networking slide
Networking slideNetworking slide
Networking slide
 
Design Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring toolDesign Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring tool
 
The Network Ip Address Scheme
The Network Ip Address SchemeThe Network Ip Address Scheme
The Network Ip Address Scheme
 
A Deep Dive in the World of IT Networking (Part 2)
A Deep Dive in the World of IT Networking (Part 2)A Deep Dive in the World of IT Networking (Part 2)
A Deep Dive in the World of IT Networking (Part 2)
 
Running head network design 1 netwo
Running head network design                             1 netwoRunning head network design                             1 netwo
Running head network design 1 netwo
 
OSI and TCPIP Model
OSI and TCPIP ModelOSI and TCPIP Model
OSI and TCPIP Model
 
Project report,nowrin
Project report,nowrinProject report,nowrin
Project report,nowrin
 
layering.pdf
layering.pdflayering.pdf
layering.pdf
 

Recently uploaded

INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 

Recently uploaded (20)

INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 

Sockets

  • 1. SIKHINAM NAGAMANI(T547) ASSISTANT PROFESSOR IT,LBRCE. UNIT II Sockets 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 1
  • 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
  • 19. Socket Address Structures(cont’d)  IPv6 Socket Address Structure: 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 19
  • 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
  • 29. Byte Ordering Functions(cont’d) 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 29
  • 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
  • 31. Byte Manipulation Functions (cont’d) 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 31
  • 32. Byte Manipulation Functions (cont’d) 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 32
  • 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
  • 40. Bind(cont’d) 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 40
  • 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
  • 43. listen Function(cont’d) 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 43
  • 44. listen Function(cont’d) 5/12/2021 9:36 AM Sikhinam Nagamani, T547,Assistant Professor,IT, LBRCE. 44
  • 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