0% found this document useful (0 votes)
5 views22 pages

Lec 04

The document outlines the course on Computer Networks, focusing on socket programming and its components. It describes sockets as endpoints for communication between programs, detailing types such as datagram and stream sockets, and provides a step-by-step guide on socket creation and connection processes for both server and client nodes. Key functions like socket(), bind(), listen(), and accept() are explained, highlighting their roles in establishing network communication.

Uploaded by

INFINITY KING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views22 pages

Lec 04

The document outlines the course on Computer Networks, focusing on socket programming and its components. It describes sockets as endpoints for communication between programs, detailing types such as datagram and stream sockets, and provides a step-by-step guide on socket creation and connection processes for both server and client nodes. Key functions like socket(), bind(), listen(), and accept() are explained, highlighting their roles in establishing network communication.

Uploaded by

INFINITY KING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Course: Computer Networks

(CSL 302, Core)


5th Semester

Topics Covered
 Sockets in Computer Networks
 Intro to Socket Programming
Sockets in Computer Networks
 Socket
 One endpoint of a two-way communication link
between two programs running on the network
 Bound to a port number so that TCP layer can identify
the application that data is destined to be sent to
 A combination of an IP address and a port number

 Every TCP connection can be uniquely identified by its


two endpoints
 i.e. can have multiple connections between host and server
Cont…
 Sockets allows a computer to send and receive data
over the network
 For example
 Consider a client computer that wants to send a request to a
server to access a webpage.
 The client creates a socket and sends the request to the
server through the socket.
 The server receives the request and sends a response back
to the client through the same socket.
 This allows the two computers to communicate with each
other and exchange data
Cont…
 Types of Sockets
 Datagram Socket
 Stream Socket
Cont…
 Datagram Socket
A type of network socket that allows data to be sent
and received in a connectionless manner
 There is no established connection between the sender and
receiver before data is sent, and each datagram (packet)
of data is treated as a separate unit
 Allows for more flexibility and faster communication, but
also means that there is a higher risk of data loss or
corruption due to lack of error checking and retransmission.
 Commonly used in real-time applications where latency is
important, such as streaming audio or video
Cont…
 Stream Socket
A stream socket is a type of network socket that
provides a connection-oriented, full-duplex
communication channel between two network devices
 It allows for the exchange of data between the devices
in a continuous flow, similar to a stream of water
 Stream sockets use the Transmission Control Protocol
(TCP) to establish a connection and transmit data
 Stream sockets can be used in a variety of applications
 web browsers, email clients, and online games
 commonly used in client-server architectures
Socket Programming
 Socket Programming is a method to connect two
nodes over a network to establish a means of
communication between those two nodes
A node represents a computer or a physical device with
an internet connection
 A socket is the endpoint used for connecting to a
node
 The signals required to implement the connection
between two nodes are sent and received using the
sockets on each node respectively
State Diagram: Client-Server Model
Cont…
Function Call Description
Socket() To create a socket
It’s a socket identification like a telephone number to
Bind()
contact
Listen() Ready to receive a connection
Connect() Ready to act as a sender
Confirmation, it is like accepting to receive a call
Accept()
from a sender
Write() To send data
Read() To receive data
Close() To close a connection
Cont…
 The nodes are divided into two types, server node and
client node
 Client node sends the connection signal
 Server node receives the connection signal sent by the client node
 The connection between a server and client node is
established using the socket over the transport layer of the
internet
 After a connection has been established, the client and
server nodes can share information between them using the
read and write commands
 After sharing of information is done, the nodes terminate the
connection
Cont…
 Stages for Server
 A. Socket Creation
 B. Setsockopt
 C. Bind
 D. Listen
 E. Accept
 F. Read/Write
 G. Close
 Stages for Client
 A. Socket Connection
 B. Connect
 C. Read/Write
 D. Close
Socket Creation
 The first stage deals with the creation of a socket,
which is the basic component for sending or
receiving signals between nodes
 A socket can be created by the socket() function
with syntax
 intsocket(int domain, int type, int protocol);
 The socket() function
 Creates a socket and returns a file descriptor
Cont…
 Domain
 Theaddress family over which the communication will
be performed
 AF_LOCAL or AF_UNIX is used for local communication or in
the case where the client and server are on the same node.
 AF_INET is used to represent the IPv4 address of the client
to which a connection should be made. Similarly AF_INET6 is
used for IPv6 addresses.
 AF_BLUETOOTH is used for low-level Bluetooth connection.
Cont…
 Type
 The type of communication used in the socket
 SOCK_STREAM uses the TCP(Transmission Control Protocol)
to establish a connection. This type provides a reliable byte
stream of data flow and is a connection-based protocol.
These sockets are called stream sockets.
 SOCK_DGRAM uses the UDP(User Datagram Protocol) which
is unreliable and a connectionless protocol. These sockets are
also called datagram sockets.
Cont…
 Protocol
 The protocol used in the socket.
 This is represented by a number
 When there is only one protocol in the protocol family, the
protocol number will be 0, or else the specific number for
the protocol has to be specified
Setsockopt
 Used to specify some options for the socket to control
the behavior of the socket.
 int setsockopt(int socket_descriptor, int level, int
option_name, const void *value_of_option, socklen_t
option_length);
 The socket is the file descriptor returned by the socket()
function.
 The level parameter represents the level at which the option
for the socket must be applied.
 The SOL_SOCKET represents the socket level
 IPPROTO_TCP represents the TCP level.
Cont…
 The option_name specifies the rules or options that
should be modified for the socket.
 SO_DEBUG is used to enable the recording of debugging
information.
 SO_REUSEADDR is used to enable the reusing of local
addresses in the bind() function.
 SO_SNDBUF is used to set the maximum buffer size that can
be sent using the socket connection.
 The option_value is used to specify the value for the
options set in the option_name parameter.
 The option_length is the length of the variable used to
set the option value.
Bind
 The bind() function is used to assign an address to a
socket created using the socket() function
 int bind(int socket_descriptor, const struct
sockaddr *address, socklen_t length_of_address);
 The socket_descriptor is the value of the file descriptor
returned by the socket() function.
 The address is a structure of type sockaddr.

 The length_of_address represents the size of the


address passed as the second parameter.
Listen
 The listen() function in socket programming is used to
make the server node wait and listen for connections
from the client node on the port and address
specified by the bind() function. The syntax is,
 int listen(int socket_descriptor, int back_log);
 The socket_descriptor represents the value of the file
descriptor returned by the socket() function.
 The back_log marks the maximum number of connection
requests that can be made to the server by client nodes
at a time.
Accept
 The accept() function is used to establish a connection
between the server and the client nodes for the transfer
of data.
 int accept(int socket_descriptor, struct sockaddr
*restrict address, socklen_t *restrict
length_of_address);
 The socket_descriptor represents the value of the file
descriptor returned by the socket() function.
 The address is the variable of the sockaddr_in structure in
which the address of the socket returned from the function
will be stored.
 The length_of_address depicts the size of the address
parameter.
Stages for Client
 Socket Connection
 Similar to the server-side, the client-side also needs to
create a socket using the socket() function and bind the
socket to an address using the bind() function. This will
create a socket that can send the connection request to
the server.
Cont…
 Connect
 The connect() function is used to send the connection request
and connect to the server node
 int connect(int socket_descriptor, const struct sockaddr
*address, socklen_t length_of_address);
 The socket_descriptor represents the value of the file descriptor
returned by the socket() function during the creation of a socket on
the client-side.
 The address represents the structure with the information of the
address and port number of the server node to which the
connection is to be made.
 The length_of_address is the size of the address structure used in
the second parameter.

You might also like