SlideShare uma empresa Scribd logo
1 de 40
Sockets
Jerin Mathew George
S5 MCA
Roll No:201219
Socket
• Sockets allow communication between two
different processes on the same or
different machines.
• it's a way to talk to other computers using
standard Unix file descriptors.
• Sockets were first introduced in 2.1BSD
and subsequently refined into their current
form with 4.2BSD. The sockets feature is
now available with most current UNIX
system releases.
• Bidirectional.
• Socket is a end-point communication.
• Socket: An interface between an
application process and transport layer.
The application process can
send/receive messages to/from
another application process (local or
remote)via a socket.
• End point determined by two things:
– Host address: IP address in Network Layer
– Port number: is Transport Layer
• Two end-points determine a connection:
socket pair
– ex: 206.62.226.35,p21 + 198.69.10.2,p1500
– ex: 206.62.226.35,p21 + 198.69.10.2,p1499
• Create Socket
– socket(domain,type,protocol)
• Domain: method of identifing
sockets(address)
– Range of Communicatin
Process on a single host
Across a network
• Type: Stream/Datagram
• Protocol: Usually 0, which means default
Common Socket Domains
• Unix Domain (AF_UNIX)
– Communication on a single host
– Adress->file system path name
• IPv4 Domain (AF_INET)
– Communication on IPv4 network
– Address->IPv4 address (32bit)+Port No
• IPv6 Domain (AF_INET6)
– Communication on IPv6 network
– Address->IPv4 address (128bit)+Port No
Ports
• Numbers (typical, since vary by OS):
– 0-1023 “reserved”, must be root
– 1024 - 5000 “ephemeral”
– Above 5000 for general use
(50,000 is specified max)
• Well-known, reserved services (see
/etc/services in Unix):
– ftp 21/tcp
– telnet 23/tcp
– finger 79/tcp
– snmp 161/udp
Using Ports to Identify Services
10
Web server
(port 80)
Client host
Server host 128.2.194.242
Echo server
(port 7)
Service request for
128.2.194.242:80
(i.e., the Web server)
Web server
(port 80)
Echo server
(port 7)
Service request for
128.2.194.242:7
(i.e., the echo server)
OS
OS
Client
Client
Socket Types
• Two main Types are:-
– UDP: User Datagram Protocol
(SOCK_STREAM)
– TCP: Transmission Control Protocol
(SOCK_DGRAM)
• Other domain provided by UNIX system is
– Sequential Packet
(SOCK_SEQPACKET)
Stream Socket(TCP)
Byte streamr
reliable (in order, all arrive, no duplicates)
flow control
Two channels on the socket for send bytes
to each other
Connection-oriented
* Eg: like two party phone calls
Datagram Socket(UDP)
Message oriented
no acknowledgements
no retransmissions
Unreliable (out of order, duplicates)
Connection less
*Eg: Postal System
Sequential Packet Socket(SCTP)
It is not widely used protocol
Midway between stream and datagram
socket
Message oriented
Connection oriented
Reliable
Addresses and Sockets
• Structure to hold address information
• Functions pass address from user to OS
bind()
connect()
sendto()
• Functions pass address from OS to user
accept()
recvfrom()
Socket Address Structure
struct in_addr {
in_addr_t s_addr; /* 32-bit IPv4 addresses */
};
struct sockaddr_in {
unit8_t sin_len; /* length of structure */
sa_family_t sin_family; /* AF_INET *domain/
in_port_t sin_port; /* TCP/UDP Port num */
struct in_addr sin_addr; /* IPv4 address (above) */
char sin_zero[8]; /* unused */
}
• Are also “generic” and “IPv6” socket
structures
TCP Client-Server
socket()
bind()
listen()
accept()
Server
socket()
connect()
send()
recv()
Client
(Block until connection)
“Handshake”
recv()
send()
Data (request)
Data (reply)
close()
End-of-File
recv()
“well-known”
port ActiveSocket
Passive Socket
socket()
int socket(int family, int type, int protocol);
Create a socket, giving access to transport layer
service.
• family is one of
– AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix),
– AF_ROUTE (access to routing tables), AF_KEY (new, for
encryption)
• type is one of
– SOCK_STREAM (TCP), SOCK_DGRAM (UDP)
– SOCK_RAW (for special IP packets, PING, etc. Must be root)
setuid bit (-rws--x--x root 1997 /sbin/ping*)
• protocol is 0 (used for some raw socket
options)
• upon success returns socket descriptor
– Integer, like file descriptor
– Return -1 if failure
bind()
• sockfd is socket descriptor from socket()
• myaddr is a pointer to address struct with:
– port number and IP address
– if port is 0, then host will pick ephemeral port
not usually for server (exception RPC port-map)
– IP address != INADDR_ANY (unless multiple nics)
• addrlen is length of structure
• returns 0 if ok, -1 on error
– EADDRINUSE (“Address already in use”)
int bind(int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);
Assign a local protocol address (“name”) to a socket.
listen()
• sockfd is socket descriptor from socket()
• backlog is maximum number of incomplete
connections
– historically 5
– rarely above 15 on a even moderate Web server!
• Sockets default to active (for a client)
– change to passive so OS will accept connection
int listen(int sockfd, int backlog);
Change socket state for TCP server.
accept()
• sockfd is socket descriptor from socket()
• cliaddr and addrlen return protocol address from
client
• returns brand new descriptor, created by OS
• note, if create new process or thread, can create
concurrent server
int accept(int sockfd, struct sockaddr
cliaddr, socklen_t *addrlen);
Return next completed connection.
close()
• sockfd is socket descriptor from socket()
• closes socket for reading/writing
– returns (doesn’t block)
– attempts to send any unsent data
– socket option SO_LINGER
block until data sent
or discard any remaining data
– returns -1 if error
int close(int sockfd);
Close socket for use.
TCP Client-Server
socket()
bind()
listen()
accept()
Server
socket()
connect()
send()
recv()
Client
(Block until connection)
“Handshake”
recv()
send()
Data (request)
Data (reply)
close()
End-of-File
recv()
“well-known”
port
connect()
• sockfd is socket descriptor from socket()
• servaddr is a pointer to a structure with:
– port number and IP address
– must be specified (unlike bind())
• addrlen is length of structure
• client doesn’t need bind()
– OS will pick ephemeral port
• returns socket descriptor if ok, -1 on error
int connect(int sockfd, const struct sockaddr
*servaddr, socklen_t addrlen);
Connect to server.
Sending and Receiving
int recv(int sockfd, void *buff, size_t
mbytes, int flags);
int send(int sockfd, void *buff, size_t
mbytes, int flags);
• Same as read() and write() but for flags
– MSG_DONTWAIT (this send non-blocking)
– MSG_OOB (out of band data, 1 byte sent ahead)
– MSG_PEEK (look, but don’t remove)
– MSG_WAITALL (don’t give me less than max)
– MSG_DONTROUTE (bypass routing table)
UDP Client-Server
socket()
bind()
recvfrom()
Server
socket()
sendto()
recvfrom()
Client
(Block until receive datagram)
sendto()
Data (request)
Data (reply)
close()
“well-known”
port
- No “handshake”
- No simultaneous close
- No fork()/spawn() for concurrent servers!
Sending and Receiving
int recvfrom(int sockfd, void *buff, size_t mbytes,
int flags, struct sockaddr *from, socklen_t
*addrlen);
int sendto(int sockfd, void *buff, size_t mbytes, int
flags, const struct sockaddr *to, socklen_t
addrlen);
• Same as recv() and send() but for addr
– recvfrom fills in address of where packet
came from
– sendto requires address of where sending
packet to
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix
Sockets in unix

Mais conteúdo relacionado

Mais procurados (20)

Application Layer
Application Layer Application Layer
Application Layer
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Transport layer
Transport layer Transport layer
Transport layer
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer services
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptx
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Ch 19 Network-layer protocols Section 1
Ch 19  Network-layer protocols Section 1Ch 19  Network-layer protocols Section 1
Ch 19 Network-layer protocols Section 1
 
UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram Protocol
 
Ch 20 UNICAST ROUTING SECTION 2
Ch 20   UNICAST ROUTING  SECTION  2Ch 20   UNICAST ROUTING  SECTION  2
Ch 20 UNICAST ROUTING SECTION 2
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Chapter 4 data link layer
Chapter 4 data link layerChapter 4 data link layer
Chapter 4 data link layer
 
TCP/ IP
TCP/ IP TCP/ IP
TCP/ IP
 
Chapter 3 : User Datagram Protocol (UDP)
Chapter 3 : User Datagram Protocol (UDP)Chapter 3 : User Datagram Protocol (UDP)
Chapter 3 : User Datagram Protocol (UDP)
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Pthread
PthreadPthread
Pthread
 
Network Layer
Network LayerNetwork Layer
Network Layer
 
IP Datagram Structure
IP Datagram StructureIP Datagram Structure
IP Datagram Structure
 
TCP & UDP ( Transmission Control Protocol and User Datagram Protocol)
TCP & UDP ( Transmission Control Protocol and User Datagram Protocol)TCP & UDP ( Transmission Control Protocol and User Datagram Protocol)
TCP & UDP ( Transmission Control Protocol and User Datagram Protocol)
 

Destaque

Inter process communication using Linux System Calls
Inter process communication using Linux System CallsInter process communication using Linux System Calls
Inter process communication using Linux System Callsjyoti9vssut
 
Mobile Ad hoc Networks
Mobile Ad hoc NetworksMobile Ad hoc Networks
Mobile Ad hoc NetworksJagdeep Singh
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationMohd Tousif
 

Destaque (6)

Inter process communication using Linux System Calls
Inter process communication using Linux System CallsInter process communication using Linux System Calls
Inter process communication using Linux System Calls
 
Ipc ppt
Ipc pptIpc ppt
Ipc ppt
 
Mobile Ad hoc Networks
Mobile Ad hoc NetworksMobile Ad hoc Networks
Mobile Ad hoc Networks
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

Semelhante a Sockets in unix

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.pptMajedAboubennah
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptsenthilnathans25
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxRockyBhai46825
 
Computer network (12)
Computer network (12)Computer network (12)
Computer network (12)NYversity
 
Socket programming
Socket programmingSocket programming
Socket programmingDivya Sharma
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfPraveenKumar187040
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using pythonAli Nezhad
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.pptEloOgardo
 

Semelhante a Sockets in unix (20)

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
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
Np unit2
Np unit2Np unit2
Np unit2
 
Sockets
Sockets Sockets
Sockets
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
 
Computer network (12)
Computer network (12)Computer network (12)
Computer network (12)
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Sockets
Sockets Sockets
Sockets
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using python
 
Networking.ppt
Networking.pptNetworking.ppt
Networking.ppt
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 

Último

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Último (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Sockets in unix

  • 1. Sockets Jerin Mathew George S5 MCA Roll No:201219
  • 2. Socket • Sockets allow communication between two different processes on the same or different machines. • it's a way to talk to other computers using standard Unix file descriptors. • Sockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases.
  • 3. • Bidirectional. • Socket is a end-point communication. • Socket: An interface between an application process and transport layer. The application process can send/receive messages to/from another application process (local or remote)via a socket.
  • 4. • End point determined by two things: – Host address: IP address in Network Layer – Port number: is Transport Layer • Two end-points determine a connection: socket pair – ex: 206.62.226.35,p21 + 198.69.10.2,p1500 – ex: 206.62.226.35,p21 + 198.69.10.2,p1499 • Create Socket – socket(domain,type,protocol)
  • 5. • Domain: method of identifing sockets(address) – Range of Communicatin Process on a single host Across a network • Type: Stream/Datagram • Protocol: Usually 0, which means default
  • 6. Common Socket Domains • Unix Domain (AF_UNIX) – Communication on a single host – Adress->file system path name • IPv4 Domain (AF_INET) – Communication on IPv4 network – Address->IPv4 address (32bit)+Port No • IPv6 Domain (AF_INET6) – Communication on IPv6 network – Address->IPv4 address (128bit)+Port No
  • 7.
  • 8.
  • 9. Ports • Numbers (typical, since vary by OS): – 0-1023 “reserved”, must be root – 1024 - 5000 “ephemeral” – Above 5000 for general use (50,000 is specified max) • Well-known, reserved services (see /etc/services in Unix): – ftp 21/tcp – telnet 23/tcp – finger 79/tcp – snmp 161/udp
  • 10. Using Ports to Identify Services 10 Web server (port 80) Client host Server host 128.2.194.242 Echo server (port 7) Service request for 128.2.194.242:80 (i.e., the Web server) Web server (port 80) Echo server (port 7) Service request for 128.2.194.242:7 (i.e., the echo server) OS OS Client Client
  • 11. Socket Types • Two main Types are:- – UDP: User Datagram Protocol (SOCK_STREAM) – TCP: Transmission Control Protocol (SOCK_DGRAM) • Other domain provided by UNIX system is – Sequential Packet (SOCK_SEQPACKET)
  • 12. Stream Socket(TCP) Byte streamr reliable (in order, all arrive, no duplicates) flow control Two channels on the socket for send bytes to each other Connection-oriented * Eg: like two party phone calls
  • 13.
  • 14. Datagram Socket(UDP) Message oriented no acknowledgements no retransmissions Unreliable (out of order, duplicates) Connection less *Eg: Postal System
  • 15. Sequential Packet Socket(SCTP) It is not widely used protocol Midway between stream and datagram socket Message oriented Connection oriented Reliable
  • 16. Addresses and Sockets • Structure to hold address information • Functions pass address from user to OS bind() connect() sendto() • Functions pass address from OS to user accept() recvfrom()
  • 17. Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { unit8_t sin_len; /* length of structure */ sa_family_t sin_family; /* AF_INET *domain/ in_port_t sin_port; /* TCP/UDP Port num */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8]; /* unused */ } • Are also “generic” and “IPv6” socket structures
  • 18. TCP Client-Server socket() bind() listen() accept() Server socket() connect() send() recv() Client (Block until connection) “Handshake” recv() send() Data (request) Data (reply) close() End-of-File recv() “well-known” port ActiveSocket Passive Socket
  • 19. socket() int socket(int family, int type, int protocol); Create a socket, giving access to transport layer service. • family is one of – AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), – AF_ROUTE (access to routing tables), AF_KEY (new, for encryption) • type is one of – SOCK_STREAM (TCP), SOCK_DGRAM (UDP) – SOCK_RAW (for special IP packets, PING, etc. Must be root) setuid bit (-rws--x--x root 1997 /sbin/ping*) • protocol is 0 (used for some raw socket options) • upon success returns socket descriptor – Integer, like file descriptor – Return -1 if failure
  • 20. bind() • sockfd is socket descriptor from socket() • myaddr is a pointer to address struct with: – port number and IP address – if port is 0, then host will pick ephemeral port not usually for server (exception RPC port-map) – IP address != INADDR_ANY (unless multiple nics) • addrlen is length of structure • returns 0 if ok, -1 on error – EADDRINUSE (“Address already in use”) int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Assign a local protocol address (“name”) to a socket.
  • 21. listen() • sockfd is socket descriptor from socket() • backlog is maximum number of incomplete connections – historically 5 – rarely above 15 on a even moderate Web server! • Sockets default to active (for a client) – change to passive so OS will accept connection int listen(int sockfd, int backlog); Change socket state for TCP server.
  • 22. accept() • sockfd is socket descriptor from socket() • cliaddr and addrlen return protocol address from client • returns brand new descriptor, created by OS • note, if create new process or thread, can create concurrent server int accept(int sockfd, struct sockaddr cliaddr, socklen_t *addrlen); Return next completed connection.
  • 23. close() • sockfd is socket descriptor from socket() • closes socket for reading/writing – returns (doesn’t block) – attempts to send any unsent data – socket option SO_LINGER block until data sent or discard any remaining data – returns -1 if error int close(int sockfd); Close socket for use.
  • 24. TCP Client-Server socket() bind() listen() accept() Server socket() connect() send() recv() Client (Block until connection) “Handshake” recv() send() Data (request) Data (reply) close() End-of-File recv() “well-known” port
  • 25. connect() • sockfd is socket descriptor from socket() • servaddr is a pointer to a structure with: – port number and IP address – must be specified (unlike bind()) • addrlen is length of structure • client doesn’t need bind() – OS will pick ephemeral port • returns socket descriptor if ok, -1 on error int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Connect to server.
  • 26. Sending and Receiving int recv(int sockfd, void *buff, size_t mbytes, int flags); int send(int sockfd, void *buff, size_t mbytes, int flags); • Same as read() and write() but for flags – MSG_DONTWAIT (this send non-blocking) – MSG_OOB (out of band data, 1 byte sent ahead) – MSG_PEEK (look, but don’t remove) – MSG_WAITALL (don’t give me less than max) – MSG_DONTROUTE (bypass routing table)
  • 27. UDP Client-Server socket() bind() recvfrom() Server socket() sendto() recvfrom() Client (Block until receive datagram) sendto() Data (request) Data (reply) close() “well-known” port - No “handshake” - No simultaneous close - No fork()/spawn() for concurrent servers!
  • 28. Sending and Receiving int recvfrom(int sockfd, void *buff, size_t mbytes, int flags, struct sockaddr *from, socklen_t *addrlen); int sendto(int sockfd, void *buff, size_t mbytes, int flags, const struct sockaddr *to, socklen_t addrlen); • Same as recv() and send() but for addr – recvfrom fills in address of where packet came from – sendto requires address of where sending packet to

Notas do Editor

  1. Length field makes it easier for OS to handle