Sockets Introduction
Sockets Introduction
to Socket
Programming
Unit 2 Test Syllabus
• Application layer –I (Chapter 21)
• Socket Programming: Client-Server Model Of Interaction(21.1), The Client-Server Model
(21.2), A Trivial Example (21.3), : UDP Echo Server , Time And Date Service (21.4),
,Sequential And Concurrent Servers ,Server Complexity (21.5) , Broadcasting Requests
,Client-Server Alternatives And Extensions. (21.7),
• The Socket API (Chapter 22): Introduction (22.1),, Versions Of The Socket API (22.2), ,
The UNIX I/O Paradigm And Network I/O (22.3), ,Adding Network I/O to UNIX ,The
Socket Abstraction And Socket Operations, (22.4), Obtaining And Setting Socket Options
(22.12), ,How A Server Accepts TCP Connections (22.14), Servers That Handle Multiple
Services (22.15)
Inter process Communication
•
Client-Server Model Of
Interaction
• The term server applies to any program that
offers a service that can be reached over a
network.
• A server accepts a request over the network,
performs its service, and returns the result to
the requester.
• For the simplest services, each request
arrives in a single IP datagram and the server
returns a response in another datagram.
• An executing program becomes a client
when it sends a request to a server and waits
for a response.
• Because the client-server model is a
convenient and natural extension of
interprocess communication on a single
machine, it is easy to build programs that use
the model to interact.
• Servers can perform simple or complex
tasks.
• For example, a time-of-day server merely
returns the current time whenever a client
sends the server a packet.
• A web server receives requests from a
browser to fetch a copy of a Web page; the
server obtains a copy of the file for the page
and returns it to the browser.
• servers are implemented as application programs.
• The advantage of implementing servers as application
programs is that they can execute on any computing system
that supports TCP/IP communication.
• Thus, the server for a particular service can execute on a
timesharing system along with other programs, or it can
execute on a personal computer.
• Multiple servers can offer the same service, and can
execute on the same machine or on multiple machines.
A Simple Example: UDP Echo
Server
• The simplest form of client-server interaction uses unreliable datagram delivery
to convey messages from a client to a server and back.
• Consider, for example, a UDP echo server. The mechanics are straightforward
as Figure shows.
• At the server site, a UDP echo server process begins by negotiating with its
operating system for permission to use the UDP port ID reserved for the echo
service, the UDP echo port. Once it has obtained permission, the echo server
process enters an infinite loop that has three steps: (1) wait for a datagram to
arrive at the echo port, (2) reverse the source and destination addresses
(including source and destination IP addresses as well as UDP port ids), and (3)
return the datagram to its original sender. At some other site, a program
becomes a UDP echo client when it allocates an unused UDP protocol port,
sends a UDP message to the UDP echo server, and awaits the reply. The client
• A server waits for requests at a well-known port that has been reserved
for the service it offers. A client allocates an arbitrary, unused
nonreserved port for its communication.
• In a client-server interaction, only one of the two ports needs to be
reserved. Assigning a unique port identifier to each service makes it
easy to build both clients and servers.
Time And Date Service
The Complexity of Servers
The Socket Interface
• What is socket?
• A socket is one endpoint of a two-way communication link between two
programs running on the network. The application creates a socket.
Socket is an interface between application layer and transport layer.
• It is an interface (a “door”) into which an application process can both
send and receive message to/from another application process
(remote/local application process). Socket is also referred as the
application programmer’s interface (API) between the application and
the network.
• Need of Socket
• When we desire a communication between two applications possibly running
on different machines, we need sockets. Requirement of socket is to build any
Network Application i.e., Web browsers, FTP etc…
How Socket Works
• The listen() system call establishes the size of the incoming request queue.
Although the listen() call is not strictly necessary, it is commonly used. The
function prototype is as follows:
• where s is the socket and backlog is the size of the queue (i.e. the maximum
queued connection requests). Here is a typical listen() call:
• This call specifies that the socket sockfd will queue up to MAXCONNECT
requests.
SERVER
Create socket
TCP Client/Server
bind a port to the
socket
CLIENT
listen for incoming
Create socket
connections
accept an
connect to server's
incoming
port
connection
loop loop
close connection
TCP Server
• Sequence of calls
close() shutdown
TCP Client
• Sequence of calls
close() Shutdown
SERVER
Server Side Socket Details
Create socket
int socket(int domain, int type, int protocol)
sockfd = socket(PF_INET, SOCK_STREAM, 0);
bind a port to the int bind(int sockfd, struct sockaddr *server_addr, socklen_t length)
socket
bind(sockfd, &server, sizeof(server));
accept an
incoming
int accept(int sockfd, struct sockaddr *incoming_address, socklen_t length)
connection newfd = accept(sockfd, &client, sizeof(client)); /* BLOCKS */
read from the int read(int sockfd, void * buffer, size_t buffer_size)
connection
read(newfd, buffer, sizeof(buffer));
connect to Server int connect(int sockfd, struct sockaddr *server_address, socklen_t length)
socket
connect(sockfd, &server, sizeof(server));
read from the int read(int sockfd, void * buffer, size_t buffer_size)
connection
read(sockfd, buffer, sizeof(buffer));
UDP Clients and Servers
• Connectionless clients and servers create a socket using SOCK_DGRAM
instead of SOCK_STREAM
• Connectionless servers do not call listen() or accept(), and usually do not call
connect()
close() Shutdown
UDP Client
•socket_init()
Sequence of calls
Create socket
close() Shutdown
The Socket Abstraction
• The basis for network I/0 in the socket API centers on an
abstraction known as the socket.
• The chief difference between file descriptors and socket
descriptors is that the operating system binds a file
descriptor to a specific file or device when the application
calls open, but it can create sockets without binding them to
specific destination addresses.
• The application can choose to supply a destination address
each time it uses the socket (e.g., when sending datagrams),
or it can choose to bind the destination address to the socket
and avoid specifying the destination repeatedly
Socket types define the communication properties visible to a user.
The Internet domain sockets provide access to the TCP/IP
transport protocols. The Internet domain is identified by the value
AF_INET. Sockets exchange data only with sockets in the same
domain. Three types of sockets are supported:
1. Stream sockets allow processes to communicate using TCP. A stream socket
provides bidirectional, reliable, sequenced, and unduplicated flow of data with no
record boundaries. Once the connection has been established, data can be read from
and written to these sockets as a byte stream. The socket type is SOCK_STREAM.
2. Datagram sockets allow processes to use UDP to communicate. A datagram socket
supports bidirectional flow of messages. A process on a datagram socket may
receive messages in a different order from the sending sequence and may receive
duplicate messages. Record boundaries in the data are preserved. The socket type is
SOCK_DGRAM.
3. Raw sockets provide access to ICMP. These sockets are normally datagram
oriented, although their exact characteristics are dependent on the interface
provided by the protocol. Raw sockets are not for most applications. They are
provided to support developing new communication protocols or for access to more
esoteric facilities of an existing protocol. Only superuser processes may use raw
sockets
Declaration for socket function
Socket types
Binding Local Names
sockfd is the usual socket file descriptor from the socket() system call.
backlog is the number of connections allowed on the incoming queue.
Declaration for connect function
sockfd is the socket file descriptor, as returned by the socket() call. But can
be used to communicate with server after successfully called connect().
serveraddr is a struct sockaddr containing the destination port and IP address.
serveraddrlen can be set to sizeof(struct sockaddr).
Declaration for accept function
sockfd is the socket descriptor to read from, buf is the buffer to read the information
into, buflen is the maximum length of the buffer, flags can again be set to 0.
Set flag=MSG_OOB to read out-of-band data.
Declaration for sendto function
2. Assign transport
endpoint an 3. Determine address
address: bind() of server