socket programming

15
SOCKET PROGRAMMING Presented By : Divya Sharma

Upload: divya-sharma

Post on 24-Jun-2015

525 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Socket programming

SOCKET PROGRAMMING

Presented By :

Divya Sharma

Page 2: Socket programming

Overview

What are sockets ?What is file descriptor ?ProtocolsClient – Server paradigmSocket APITCP(Transmission Control Protocol)UDP(User Datagram Protocol)

Page 3: Socket programming

What are Sockets ?

• A socket is defined as an endpoint of communication

• Service access point of TCP/IP protocol stack • between Application layer and Transport layer

• A file descriptor that lets an application read/write data from/to the network

• Once configured the application can • Send data to the socket • Receive data from the socket

Page 4: Socket programming

What is file descriptor ?

Keyboard

Display

Program Internet

Text Terminal

#x sock

#stdin

#1stdout

#2stden

• E.g. cout writes data to stdout #(1) cin read data from stdin #(0)

Page 5: Socket programming

A socket is a file descriptor that lets an application read/write from/to the network

int fd; /*socket descriptor*/if ((fd = socket(AF_INET, SOCK-STREAM, 0)) < 0) }fd = socket(AF_INET,SOCK_STREAM, 0)) < 0) }perror(“socket”);exit(1);

• socket returns an integer(socket descriptor)• fd < 0} indicates that an error occurred• Socket descriptor s are similar to file descriptors

• AF_INET: associates a socket with the internet protocol family • SOCK_STREAM: selects the TCP protocol • SOCK_DGRAM: selects the UDP protocol

Page 6: Socket programming

• A protocol is a set of rules of communication. Protocols are the building blocks of a network architecture.

• Each protocol object has two different interfaces: service interface: operations on this protocol peer-to-peer interface: messages exchanged with

peer

• Term “protocol” is overloaded specification of peer-to-peer interface module that implements this interface

Protocols

Page 7: Socket programming

• Server waits for client to request a connection.

• Client contacts server to establish a connection.

• Client sends request.

• Server sends reply.

• Client and/or server terminate connection.

Client - Server paradigm

Page 8: Socket programming

Socket API

Server and client exchange messages over the network through a common Socket API

SERVER

TCP/UDP TCP/UDP

IP IP

EthernetAdapter

Ethernet Adapter

CLIENTS

PORTS

SOCKET API

User space

Kernel space

Hardw-are

Page 9: Socket programming

Transmission Control Protocol (TCP)

TCP• Reliable - guarantee delivery

• Byte stream - in - order delivery

• Connection - oriented - single socket per connection

• Setup connection followed by data transfer

Telephone call• Guaranteed delivery

• In order delivery

• Connection oriented

• Setup connection followed by conversation

Example TCP applicationsWeb, E-mail, Telnet

Page 10: Socket programming

connect( )

close()

read()

write( )

accept( )

listen( )

bind( )

socket( )

write( )

read( )

close( )

socket( )

Review: TCP Client - server interactionTCP Server

TCP Client

read( )

Connection establishment

Data request

Data reply

end – of - file notification

Page 11: Socket programming

User Datagram Protocol (UDP)

UDP• Single socket to receive

messages• No guarantee of delivery• Not necessarily in – order

delivery• Datagram - independent

packets• Must address each packet

Post Mail• Single mail box to

receive letters• Unreliable• Not necessarily in -

order delivery• Letters send

independently• Must address each reply

Example UDP applicationsMultimedia, voice over IP

Page 12: Socket programming

Review: UDP Client – server interaction

recvfrom( )

close( )close( )

sendto()

recvfrom( )

bind( )

socket( )

sendto( )

socket( )

UDP Server

UDP client

blocks until datagram received from a client

data reply

data request

Page 13: Socket programming

TCP vs. UDP

• Transmission Control Protocol (TCP)

• One - to - one• Connection - oriented• Reliable• In order delivery• Transmission after connect

• User Datagram Protocol (UDP)

• One to one or many• Connectionless• Unreliable• Unordered delivery• Transmission with destination address

Page 14: Socket programming

Final thoughts

• Make sure to #include the header files that defines used functions

• Check man – pages and web – site for additional information

Page 15: Socket programming

Thank You