SlideShare une entreprise Scribd logo
1  sur  27
Introduction To Socket
Programming
Dr.Parminder Singh
Associate Professor
Department of IT
CEC Landran
Definition of Socket
•when we desire a communication between two
applications possibly running on different machines,
we need sockets.
•Socket needs to return the file descriptor of following
values like domain name, type of socket and name of
the protocol.
•Socket commonly executes the user processes at
Transport Layer.
2
Socket and Process Communication
The interface that the OS provides to its networking subsystem
application layer
transport layer (TCP/UDP)
network layer (IP)
link layer (e.g. ethernet)
application layer
transport layer (TCP/UDP)
network layer (IP)
link layer (e.g. ethernet)
OS network
stack
User Process User Process
Socket
OS network
stack
Socket
Internet
Internet
Internet
3
Socket (Cont..)
4
•when we desire a communication between two applications possibly running on different machines, we need
sockets.
•Socket needs to return the file descriptor of following values like domain name, type of socket and name of
the protocol.
•Socket commonly executes the user processes at Transport Layer.
Connections
• Connection-oriented Protocol
• Connectionless protocol
5
Network Address
For Two processes to communicate, they need to know each others Network
address.
Network address consist of two parts:
<Internet (IP) address, Port number>
Port no. 1024-49151 are registered port
Port 4915-65536 can not be used. They are called ephemeral ports, which
are assigned automatically by TCP or UDP for Client
6
Socket, Port and Interface
For Two processes to communicate, they need to know each others Network
address.
Network address consist of two parts:
<Internet (IP) address, Port number>
Well-known vs. ephemeral ports
Between 0 and 1023 (requires root to use)
Port no. 1024-49151 are registered port
Port 4915-65536 can not be used. They are called ephemeral ports, which
are assigned automatically by TCP or UDP for Client
7
Using Ports to Identify Services
8
Web server
(port 80)
Client host
Server host 1.1.1.2
Echo server
(port 7)
Service request for
1.1.1.1:80
(i.e., the Web server)
Web server
(port 80)
Echo server
(port 7)
Service request for
1.1.1.2:7
(i.e., the echo server)
OS
OS
Client
Client
Two Types of Application Processes
Communication
• Datagram Socket (UDP)
– Collection of messages
– Best effort
– Connectionless
• Stream Socket (TCP)
– Stream of bytes
– Reliable
– Connection-oriented
9
User Datagram Protocol (UDP):
Datagram Socket
Postal Mail
• Single mailbox to receive
messages
• Unreliable 
• Not necessarily in-order
delivery
• Each letter is independent
• Must address each reply
Example UDP applications
Multimedia, voice over IP (Skype)
UDP
• Single socket to receive messages
• No guarantee of delivery
• Not necessarily in-order delivery
• Datagram – independent packets
• Must address each packet
Postal Mail
• Single mailbox to receive letters
• Unreliable
• Not necessarily in-order delivery
• Letters sent independently
• Must address each mail
10
Transmission Control Protocol (TCP):
Stream Socket
Postal Mail
• Single mailbox to receive
messages
• Unreliable 
• Not necessarily in-order
delivery
• Each letter is independent
• Must address each reply
Example TCP applications
Web, Email, Telnet
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
11
Socket Identification
• Communication Protocol
– TCP (Stream Socket): streaming, reliable
– UDP (Datagram Socket): packets, best effort
• Receiving host
– Destination address that uniquely identifies the host
– An IP address is a 32-bit quantity
• Receiving socket
– Host may be running many different processes
– Destination port that uniquely identifies the socket
– A port number is a 16-bit quantity
12
Socket Identification (Cont.)
TCP/UDP
IP
Ethernet Adapter
Process
A
Process
B
port X port Y
Host Address
Protocol
Port Number
13
Types of Socket
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.
14
Types of Socket (1)
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.
15
Client-Server Communication
Stream Sockets (TCP): Connection-oriented
Create a socket
Bind the socket
(what port am I on?)
Listen for client
(Wait for incoming connections)
Accept connection
Receive Request
Send response
Server
Client
Create a socket
Connect to server
Send the request
establish connection
data (request)
Receive response
data (reply)
16
Connection-oriented Example
(Stream Sockets -TCP)
socket()
bind()
listen()
accept()
recv()
send()
Server
Client
socket()
connect()
send()
establish connection
data (request)
recv()
data (reply)
17
Server: Server Preparing its Socket
• Server creates a socket and binds address/port
– Server creates a socket, just like the client does
– Server associates the socket with the port number
• Create a socket
– int socket(int domain,
int type, int protocol )
• Bind socket to the local address and port number
– int bind(int sockfd,
struct sockaddr *my_addr,
socklen_t addrlen )
18
Description of TCP Socket
• #include <sys/socket.h>
int socket( int domain, int type, int protocol);
The domain is an integer specifying the address family and protocol. These
families are defined in <sys/socket.h>. Some of the common domain
values are
Name Purpose
• PF_UNIX: Local communication
• PF_INETIPv4: Internet protocols
• PF_INET6IPv6: Internet protocols
19
Description of TCP Socket (cont..)
listen(), which is defined by
#include <sys/socket.h>
int listen(int sockfd, int queue_size);
sockfd is the usual socket file descriptor from the socket() system call and
the second is the maximum size of the queue of pending (incomplete)
connections.
The accept() call is defined as follows:
#include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
•addr will usually be a pointer to a local struct sockaddr_in. This is where the
information about the incoming connection will go (and with it you can
determine which host is calling you from which port).
20
Common Functions of TCP Socket
(cont..)
• addrlen is a local integer variable that should be set to sizeof(struct
sockaddr_in) before its address is passed to accept(). Accept will not put
more than that many bytes into addr. If it puts fewer in, it’ll change the
value of addrlen to reflect that.
• The shutdown(sockfd, SHUT_WR) system call turns off writing to the
socket.
#include <sys/socket.h>
int shutdown(int s, int how);
• convert() reads the connected socket until it receives the end-of-file
• The client uses ordinary read() and write() calls for its I/O operations.
• The return value of socket() is a file descriptor that can be used to read or
write the socket. As an example,
sockfd = socket(PF_INET, SOCK_STREAM, 0);
21
Stream Socket
• These two functions are for communicating over stream sockets or
connected datagram sockets. If you want to use regular unconnected
datagram sockets, you’ll need to see the section on sendto() and
recvfrom(), below.
• The send() call:
int send(int sockfd, const void *msg, int len, int flags);
• sockfd is the socket descriptor you want to send data to (whether it’s the
one returned by socket() or the one you got with accept().) msg is a
pointer to the data you want to send, and len is the length of that data in
bytes.
22
Stream Socket (Cont..)
• The recv() call is similar in many respects:
int recv(int sockfd, void *buf, int len, unsigned int flags);
• sockfd is the socket descriptor to read from, buf is the buffer to read the
information into, len is the maximum length of the buffer, and flags can
again be set to 0.
• recv() returns the number of bytes actually read into the buffer, or -1 on
error (with errno set, accordingly.)
• Wait! recv() can return 0. This can mean only one thing: the remote side
has closed the connection.
23
Generic Socket
The client does not need to remember the IP address of the server because
generic socket are used to convert hostname to address translation.
A generic socket address is defined by the sockaddr struct defined in
<sys/socket.h>:
struct sockaddr {
sa_family_t sa_family; /* address family */
char sa_data[]; /* socket address */
};
The address structure for PF_INET, defined in <netinet/in.h>, would be
struct sockaddr_in {
sa_family_t sin_family; /* internet address family */
in_port_t sin_port; /* port number */
struct in_addr sin_addr; /* IP address */
unsigned char sin_zero[8]; /* padding */ 24
Related Question
Q1: what are two types of Internet Sockets?
Q2: What is socket?
Q3: What uses stream sockets?
Q4: How do stream sockets achieve this high level of data transmission
quality?
Q5: What is Layered Model?
Q6: What are well-known port number?
25
Related Question
Q5: What is Layered Model?
A layered model more consistent with Unix might be:
• Application Layer (telnet, ftp, etc.)
• Host-to-Host Transport Layer (TCP, UDP)
• Internet Layer (IP and routing)
• Network Access Layer (Ethernet, wi-fi, or whatever)
26
REFERENCES
[1] W. R. Stevens, B. Fenner & A. M. Rudoff, Unix Network Programming,
Vol. I, 3rd Ed., Pearson Education.
[2] W. R. Stevens , Unix Network Programming, Vol. II, 2nd Ed., Pearson
Education.
[3] Comer and Stevens, Internetworking with TCP/IP, Vol. I, II and III, PHI.
[4] Christian Benvenuti, Understanding Linux Network Internals, O‘Reilly.
[5] W. R. Stevens , Advanced Programming in Unix Environment, Pearson
Education.
[6]Brian “Beej” Hall,"Beej’s Guide to Network Programming Using Internet
Sockets",2005.
[7] Xi Liu,"Socket Programming",Computer Networks, Spring 2008.
[8] Dave Hollinger,Socket Programming",Computer Network Systems.
27

Contenu connexe

Tendances

Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)k33a
 
Computer Network - Network Layer
Computer Network - Network LayerComputer Network - Network Layer
Computer Network - Network LayerManoj Kumar
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unixswtjerin4u
 
UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram ProtocolPeter R. Egli
 
Chapter 4 data link layer
Chapter 4 data link layerChapter 4 data link layer
Chapter 4 data link layerNaiyan Noor
 
IP addressing seminar ppt
IP addressing seminar pptIP addressing seminar ppt
IP addressing seminar pptSmriti Rastogi
 
Socket programming or network programming
Socket programming or network programmingSocket programming or network programming
Socket programming or network programmingMmanan91
 
Difference between OSI Layer & TCP/IP Layer
Difference between OSI Layer & TCP/IP LayerDifference between OSI Layer & TCP/IP Layer
Difference between OSI Layer & TCP/IP LayerNetwax Lab
 
Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
Connection( less & oriented)
Connection( less & oriented)Connection( less & oriented)
Connection( less & oriented)ymghorpade
 

Tendances (20)

Socket programming
Socket programmingSocket programming
Socket programming
 
Subnetting
SubnettingSubnetting
Subnetting
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
TCP and UDP
TCP and UDP TCP and UDP
TCP and UDP
 
Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)
 
Computer Network - Network Layer
Computer Network - Network LayerComputer Network - Network Layer
Computer Network - Network Layer
 
Sockets
SocketsSockets
Sockets
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Transport layer
Transport layer Transport layer
Transport layer
 
Application layer protocols
Application layer protocolsApplication layer protocols
Application layer protocols
 
Application Layer
Application Layer Application Layer
Application Layer
 
UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram Protocol
 
Chapter 4 data link layer
Chapter 4 data link layerChapter 4 data link layer
Chapter 4 data link layer
 
OSI Model
OSI ModelOSI Model
OSI Model
 
IP addressing seminar ppt
IP addressing seminar pptIP addressing seminar ppt
IP addressing seminar ppt
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
 
Socket programming or network programming
Socket programming or network programmingSocket programming or network programming
Socket programming or network programming
 
Difference between OSI Layer & TCP/IP Layer
Difference between OSI Layer & TCP/IP LayerDifference between OSI Layer & TCP/IP Layer
Difference between OSI Layer & TCP/IP Layer
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Connection( less & oriented)
Connection( less & oriented)Connection( less & oriented)
Connection( less & oriented)
 

Similaire à Introduction to Socket Programming

Similaire à Introduction to Socket Programming (20)

Np unit2
Np unit2Np unit2
Np unit2
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.ppt
 
Sockets
Sockets Sockets
Sockets
 
socket programming
 socket programming  socket programming
socket programming
 
socket programming
socket programming socket programming
socket programming
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
Lecture set 7
Lecture set 7Lecture set 7
Lecture set 7
 
Os 2
Os 2Os 2
Os 2
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 
Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
 

Dernier

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 

Dernier (20)

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

Introduction to Socket Programming

  • 1. Introduction To Socket Programming Dr.Parminder Singh Associate Professor Department of IT CEC Landran
  • 2. Definition of Socket •when we desire a communication between two applications possibly running on different machines, we need sockets. •Socket needs to return the file descriptor of following values like domain name, type of socket and name of the protocol. •Socket commonly executes the user processes at Transport Layer. 2
  • 3. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) application layer transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) OS network stack User Process User Process Socket OS network stack Socket Internet Internet Internet 3
  • 4. Socket (Cont..) 4 •when we desire a communication between two applications possibly running on different machines, we need sockets. •Socket needs to return the file descriptor of following values like domain name, type of socket and name of the protocol. •Socket commonly executes the user processes at Transport Layer.
  • 6. Network Address For Two processes to communicate, they need to know each others Network address. Network address consist of two parts: <Internet (IP) address, Port number> Port no. 1024-49151 are registered port Port 4915-65536 can not be used. They are called ephemeral ports, which are assigned automatically by TCP or UDP for Client 6
  • 7. Socket, Port and Interface For Two processes to communicate, they need to know each others Network address. Network address consist of two parts: <Internet (IP) address, Port number> Well-known vs. ephemeral ports Between 0 and 1023 (requires root to use) Port no. 1024-49151 are registered port Port 4915-65536 can not be used. They are called ephemeral ports, which are assigned automatically by TCP or UDP for Client 7
  • 8. Using Ports to Identify Services 8 Web server (port 80) Client host Server host 1.1.1.2 Echo server (port 7) Service request for 1.1.1.1:80 (i.e., the Web server) Web server (port 80) Echo server (port 7) Service request for 1.1.1.2:7 (i.e., the echo server) OS OS Client Client
  • 9. Two Types of Application Processes Communication • Datagram Socket (UDP) – Collection of messages – Best effort – Connectionless • Stream Socket (TCP) – Stream of bytes – Reliable – Connection-oriented 9
  • 10. User Datagram Protocol (UDP): Datagram Socket Postal Mail • Single mailbox to receive messages • Unreliable  • Not necessarily in-order delivery • Each letter is independent • Must address each reply Example UDP applications Multimedia, voice over IP (Skype) UDP • Single socket to receive messages • No guarantee of delivery • Not necessarily in-order delivery • Datagram – independent packets • Must address each packet Postal Mail • Single mailbox to receive letters • Unreliable • Not necessarily in-order delivery • Letters sent independently • Must address each mail 10
  • 11. Transmission Control Protocol (TCP): Stream Socket Postal Mail • Single mailbox to receive messages • Unreliable  • Not necessarily in-order delivery • Each letter is independent • Must address each reply Example TCP applications Web, Email, Telnet 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 11
  • 12. Socket Identification • Communication Protocol – TCP (Stream Socket): streaming, reliable – UDP (Datagram Socket): packets, best effort • Receiving host – Destination address that uniquely identifies the host – An IP address is a 32-bit quantity • Receiving socket – Host may be running many different processes – Destination port that uniquely identifies the socket – A port number is a 16-bit quantity 12
  • 13. Socket Identification (Cont.) TCP/UDP IP Ethernet Adapter Process A Process B port X port Y Host Address Protocol Port Number 13
  • 14. Types of Socket 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. 14
  • 15. Types of Socket (1) 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. 15
  • 16. Client-Server Communication Stream Sockets (TCP): Connection-oriented Create a socket Bind the socket (what port am I on?) Listen for client (Wait for incoming connections) Accept connection Receive Request Send response Server Client Create a socket Connect to server Send the request establish connection data (request) Receive response data (reply) 16
  • 17. Connection-oriented Example (Stream Sockets -TCP) socket() bind() listen() accept() recv() send() Server Client socket() connect() send() establish connection data (request) recv() data (reply) 17
  • 18. Server: Server Preparing its Socket • Server creates a socket and binds address/port – Server creates a socket, just like the client does – Server associates the socket with the port number • Create a socket – int socket(int domain, int type, int protocol ) • Bind socket to the local address and port number – int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen ) 18
  • 19. Description of TCP Socket • #include <sys/socket.h> int socket( int domain, int type, int protocol); The domain is an integer specifying the address family and protocol. These families are defined in <sys/socket.h>. Some of the common domain values are Name Purpose • PF_UNIX: Local communication • PF_INETIPv4: Internet protocols • PF_INET6IPv6: Internet protocols 19
  • 20. Description of TCP Socket (cont..) listen(), which is defined by #include <sys/socket.h> int listen(int sockfd, int queue_size); sockfd is the usual socket file descriptor from the socket() system call and the second is the maximum size of the queue of pending (incomplete) connections. The accept() call is defined as follows: #include <sys/types.h> #include <sys/socket.h> int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); •addr will usually be a pointer to a local struct sockaddr_in. This is where the information about the incoming connection will go (and with it you can determine which host is calling you from which port). 20
  • 21. Common Functions of TCP Socket (cont..) • addrlen is a local integer variable that should be set to sizeof(struct sockaddr_in) before its address is passed to accept(). Accept will not put more than that many bytes into addr. If it puts fewer in, it’ll change the value of addrlen to reflect that. • The shutdown(sockfd, SHUT_WR) system call turns off writing to the socket. #include <sys/socket.h> int shutdown(int s, int how); • convert() reads the connected socket until it receives the end-of-file • The client uses ordinary read() and write() calls for its I/O operations. • The return value of socket() is a file descriptor that can be used to read or write the socket. As an example, sockfd = socket(PF_INET, SOCK_STREAM, 0); 21
  • 22. Stream Socket • These two functions are for communicating over stream sockets or connected datagram sockets. If you want to use regular unconnected datagram sockets, you’ll need to see the section on sendto() and recvfrom(), below. • The send() call: int send(int sockfd, const void *msg, int len, int flags); • sockfd is the socket descriptor you want to send data to (whether it’s the one returned by socket() or the one you got with accept().) msg is a pointer to the data you want to send, and len is the length of that data in bytes. 22
  • 23. Stream Socket (Cont..) • The recv() call is similar in many respects: int recv(int sockfd, void *buf, int len, unsigned int flags); • sockfd is the socket descriptor to read from, buf is the buffer to read the information into, len is the maximum length of the buffer, and flags can again be set to 0. • recv() returns the number of bytes actually read into the buffer, or -1 on error (with errno set, accordingly.) • Wait! recv() can return 0. This can mean only one thing: the remote side has closed the connection. 23
  • 24. Generic Socket The client does not need to remember the IP address of the server because generic socket are used to convert hostname to address translation. A generic socket address is defined by the sockaddr struct defined in <sys/socket.h>: struct sockaddr { sa_family_t sa_family; /* address family */ char sa_data[]; /* socket address */ }; The address structure for PF_INET, defined in <netinet/in.h>, would be struct sockaddr_in { sa_family_t sin_family; /* internet address family */ in_port_t sin_port; /* port number */ struct in_addr sin_addr; /* IP address */ unsigned char sin_zero[8]; /* padding */ 24
  • 25. Related Question Q1: what are two types of Internet Sockets? Q2: What is socket? Q3: What uses stream sockets? Q4: How do stream sockets achieve this high level of data transmission quality? Q5: What is Layered Model? Q6: What are well-known port number? 25
  • 26. Related Question Q5: What is Layered Model? A layered model more consistent with Unix might be: • Application Layer (telnet, ftp, etc.) • Host-to-Host Transport Layer (TCP, UDP) • Internet Layer (IP and routing) • Network Access Layer (Ethernet, wi-fi, or whatever) 26
  • 27. REFERENCES [1] W. R. Stevens, B. Fenner & A. M. Rudoff, Unix Network Programming, Vol. I, 3rd Ed., Pearson Education. [2] W. R. Stevens , Unix Network Programming, Vol. II, 2nd Ed., Pearson Education. [3] Comer and Stevens, Internetworking with TCP/IP, Vol. I, II and III, PHI. [4] Christian Benvenuti, Understanding Linux Network Internals, O‘Reilly. [5] W. R. Stevens , Advanced Programming in Unix Environment, Pearson Education. [6]Brian “Beej” Hall,"Beej’s Guide to Network Programming Using Internet Sockets",2005. [7] Xi Liu,"Socket Programming",Computer Networks, Spring 2008. [8] Dave Hollinger,Socket Programming",Computer Network Systems. 27