Docsity

Socket Programming: Understanding the Application Layer and Socket Interface, Lecture notes of Communication

Toggle sidebar
bg1
Application Layer 1
Socket Programming
Socket Programming
Srinidhi Varadarajan
Client
Client-
-server paradigm
server paradigm
Client:
initiates contact with server
(“speaks first”)
typically requests service
from server,
for Web, client is
implemented in browser; for
e-mail, in mail reader
Server:
provides requested service
to client
e.g., Web server sends
requested Web page, mail
server delivers e-mail
application
transport
network
data link
physical
application
transport
network
data link
physical
request
reply
Application Layer Programming
Application Layer Programming
API: application programming interface
defines interface between application and
transport layer
sockets: Internet API
–two processes communicate by sending data
into socket, reading data out of socket
Socket Interface. What is it?
Socket Interface. What is it?
Gives a file system like abstraction to the
capabilities of the network.
Each transport protocol offers a set of
services. The socket API provides the
abstraction to access these services
The API defines function calls to create,
close, read and write to/from a socket.
Socket Abstraction
Socket Abstraction
The socket is the basic abstraction for network
communication in the socket API
–Defines an endpoint of co mmunication for a process
–Operating system mai ntains information about the
socket and its connect ion
–Application references t he socket for sends, receives,
etc.
Process
B
Process
A
Ports (Sockets)
Network
What do you need for socket communication ?
What do you need for socket communication ?
Basically 4 parameters
–Source Identifier (IP address)
–Source Port
–Destination Identifier
–Destination Port
In the socket API, this information is
communicated by binding the socket.
pf3
pf4

Partial preview of the text

Download Socket Programming: Understanding the Application Layer and Socket Interface and more Lecture notes Communication in PDF only on Docsity!

Socket ProgrammingSocket Programming

Srinidhi Varadarajan

ClientClient--server paradigmserver paradigm

Client:  initiates contact with server (“speaks first”)  typically requests service from server,  for Web, client is implemented in browser; for e-mail, in mail reader Server:  provides requested service to client  e.g., Web server sends requested Web page, mail server delivers e-mail

applicationtransport networkdata link physical

applicationtransport networkdata link physical

request

reply

Application Layer ProgrammingApplication Layer Programming

API: application programming interface

 defines interface between application and

transport layer

 sockets: Internet API

  • two processes communicate by sending data into socket, reading data out of socket

Socket Interface. What is it?Socket Interface. What is it?

 Gives a file system like abstraction to the

capabilities of the network.

 Each transport protocol offers a set of

services. The socket API provides the

abstraction to access these services

 The API defines function calls to create,

close, read and write to/from a socket.

Socket AbstractionSocket Abstraction

 The socket is the basic abstraction for network communication in the socket API

  • Defines an endpoint of communication for a process
  • Operating system maintains information about the socket and its connection
  • Application references the socket for sends, receives, etc.

Process B

Process A

Ports (Sockets)

Network

What do you need for socket communication ?What do you need for socket communication?

 Basically 4 parameters

  • Source Identifier (IP address)
  • Source Port
  • Destination Identifier
  • Destination Port

 In the socket API, this information is

communicated by binding the socket.

Creating a socketCreating a socket

int socket(int domain, int type, int protocol)

The call returns a integer identifier called a

handle

Protocol Family: PF_INET or PF_UNIX Communication semantics: SOCK_STREAM or SOCK_DGRAM

Usually UNSPEC

Binding a socketBinding a socket

int bind (int socket, struct sockaddr *address, int addr_len)

 This call is executed by:

  • Server in TCP and UDP

 It binds the socket to the specified address. The address parameter specifies the local component of the address, e.g. IP address and UDP/TCP port

Socket DescriptorsSocket Descriptors

 Operating system maintains a set of

socket descriptors for each process

– Note that socket descriptors are shared

by threads

 Three data structures

– Socket descriptor table

– Socket data structure

– Address data structure

Socket DescriptorsSocket Descriptors

Socket Descriptor Table 0: 1: 2: .. .

proto family: PF_INET

Socket Data Structure

service: SOCK_STREAM local address:

remote address:

address family: AF_INET

Address Data Structure

host IP: 128.173.88. port: 80

TCP Server Side: ListenTCP Server Side: Listen

int listen (int socket, int backlog)

 This server side call specifies the number

of pending connections on the given

socket.

 When the server is processing a

connection, “backlog” number of

connections may be pending in a queue.

TCP Server Side: Passive OpenTCP Server Side: Passive Open

int accept (int socket, struct sockaddr *address, int *addr_len)

 This call is executed by the server.

 The call does not return until a remote

client has established a connection.

 When it completes, it returns a new socket

handle corresponding to the just-

established connection

Some Other “Utility” FunctionsSome Other “Utility” Functions

 inet_addr() -- convert “dotted”

character string form of IP address to

internal binary form

 inet_ntoa() -- convert internal binary

form of IP address to “dotted”

character string form

Address Data StructuresAddress Data Structures

 sockaddr is a generic address structure

 sockaddr_in is specific instance for the Internet address family

struct sockaddr { u_short sa_family; // type of address char sa_data[14]; // value of address }

struct sockaddr_in { u_short sa_family; // type of address (AF_INET) u_short sa_port; // protocol port number struct in_addr sin_addr; // IP address char sin_zero[8]; // unused (set to zero) }