SlideShare a Scribd company logo
Presented by
SANOJ<MU10CO13>
1. Introduction
2. Types
 Stream Sockets
 Sequential Sockets
 Datagram Sockets
 Raw Sockets
3. Creating a Socket
4. Binding a Socket
5. Using a Socket
 Connection
 Sending Data
 Receiving Data
 Other Operations
6. UNIX Socket Domain
9/13/2013 Socket Inter-Process Communication
What is a socket?
 End point of communication
 Used for IPC
• Within same machine
• Across networks
 Connection to network protocols (TCP/IP,
UDP...)
 Usage is similar to file (through file descriptors)
 This presentation will provide a very condensed
explanations of sockets, although this concept is
vast
9/13/2013 Socket Inter-Process Communication
Types of Sockets in UNIX
 Stream Sockets
 Sequential Sockets
 Datagram Sockets
 Raw Sockets
9/13/2013 Socket Inter-Process Communication
Stream Sockets
 Provides bidirectional, reliable, sequenced, and
unduplicated flow of data
 Designed to prevent the loss or duplication of data
 Default protocol in AF_INET domain is TCP
 Requires a connection before communication
 When using a stream socket for data transfer, an
application program needs to perform the following
sequence:
i. Create a connection to another socket using
the connect subroutine.
ii. Use the read and write subroutines
(or send and recv) to transfer data.
iii. Use the close subroutine to finish the session.
9/13/2013 Socket Inter-Process Communication
Sequential Sockets
 Provides sequenced, reliable, and unduplicated flow of
information
 Not very frequently used
 Difference between SOCK_STREAM and SOCK_SEQPACKET:
i. SOCK_STREAM is supported by windows and all major
versions of Linux but SOCK_SEQPACKET is compatible with
only certain builds.
ii. Both SOCK_SEQPACKET and SOCK_STREAM handle
backpressure by tossing out the offending packet but:
• SOCK_SEQPACKET returns an error to the sending
socket, whereas
• SOCK_STREAM asks for it to be retransmitted when the
buffer has space
9/13/2013 Socket Inter-Process Communication
Datagram Sockets
 Provides unreliable, non-sequenced and
datagrams, which are connectionless messages of
a fixed maximum length
 Designed for short messages
 Data packets may be duplicated
 Application program to sends datagrams to
correspondents named in send
 Application programs can receive datagrams
through sockets using the recv
9/13/2013 Socket Inter-Process Communication
Raw Sockets
 Provides access to internal network protocols and
interfaces
 allow an application to have direct access to lower-
level communication protocols
 intended for advanced users who want to take
advantage of some protocol feature that is not directly
accessible through a normal interface, or who want to
build new protocols on top of existing low-level
protocols
 Like datagram sockets, these are datagram-oriented.
 Superuser privileges required
9/13/2013 Socket Inter-Process Communication
 Before using a socket, it has to be made
 Its access is similar to a file descriptor
 For socket creation, the socket function is called:
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
 Returns a socket descriptor if creation is successful or -
1 on error
 Domain specifies the nature of the communication
where the values include:
• AF_INET and AF_INET6 for Internet Domain
(IPV4 and IPV6 respectively)
• AF_UNIX for the UNIX domain
• AF_UNSPEC for unspecified or “any” domain
9/13/2013 Socket Inter-Process Communication
 Type is for the type of socket used:
• SOCK_DGRAM – Datagram socket
• SOCK_STREAM – Stream socket
• SOCK_SEQPACKET – Sequential socket
• SOCK_RAW – Raw socket
 Protocol is usually 0 indicating that default protocol is
used.
 For example, to create a stream socket in the Internet
domain:
s = socket(AF_INET, SOCK_STREAM, 0);
 Similar to calling open and obtaining file descriptor
 Some functions which accept file descriptor can also be
used with a socket including close(), read(), write(),
fchmod(),….etc.
 close() is used for closing (or deallocating) a socket9/13/2013 Socket Inter-Process Communication
 Created sockets cannot be used without associating
with an address (or a name, basically)
 An address identifies a socket endpoint in a particular
communication domain
 Informs a process “WHERE” to look for incoming
messages
 For binding a socket with an address:
#include <sys/socket.h>
int bind(int sock_fd,const struct sockaddr *addr);
 Returns 0 if success or -1 if error in binding
9/13/2013 Socket Inter-Process Communication
 Format for the address is:
struct sockaddr {
sa_family_t sa_family;
char sa_data[];
..
…
};
 Internet address is specified in <netinet/in.h> by the
in_addr structure.
9/13/2013 Socket Inter-Process Communication
Connection
 For connection-oriented service like Sequential and
Stream Socket
 Prior connection between client and server is required
before data exchange
 For connection:
#include <sys/socket.h>
int connect(int sock_fd,const struct sockaddr *addr,
socklen_t len);
 Returns 0 if successful or -1 if error occurs
 If a socket is not bound before connect() is called then
the system will bind a default address to it
 Can also be used with connection-less sockets for
optimization
9/13/2013 Socket Inter-Process Communication
Sending Data
 For connection-oriented service write() can also be
used, provided a connection is established
 Three specific socket data transfer functions are
available if we want to specify options, or receive
packets from multiple clients,...etc
• ssize_t send(int sock_fd,const void *buff, size_t nbytes,
int flags);
• ssize_t sendto(int sock_fd,const void *buff, size_t nbytes,
int flags,const struct sockaddr *des_addr, socklen_t
deslen);
• ssize_t sendmsg(int sock_fd,const struct msghdr *msg,
int flags);
 Returns number of bytes sent if successful or -1 if error
occurs
 buff and nbytes have the same meaning as with write
9/13/2013 Socket Inter-Process Communication
Sending Data
 Sendmsg is for specifying multiple buffers from which
to transmit data.
 Structure of msghdr is:
struct msghdr {
void *msg_name;
socklen_t msg_namelen;
...
}
 Flags have special purposes which include sending
out-of-bound data, preventing packet to route out of
network, etc
 Flags are specifies by the constants:
MSG_DONTROUTE, MSG_DONTWAIT, MSG_OOB,
MSG_EOR
9/13/2013 Socket Inter-Process Communication
Receiving Data
 For connection-oriented service read() can also be used,
provided a connection is established
 Three specific socket data transfer functions are
available if we want to specify options, or receive
packets from multiple clients,...etc
• ssize_t recv(int sock_fd,const void *buff, size_t nbytes, int
flags);
• ssize_t recvfrom(int sock_fd,const void *buff, size_t
nbytes, int flags,const struct sockaddr *des_addr,
socklen_t deslen);
• ssize_t recvmsg(int sock_fd,const struct msghdr *msg,
int flags);
 Returns number of bytes sent if successful or -1 if error
occurs9/13/2013 Socket Inter-Process Communication
Other Operations
1) Shutdown:
 We can “disable” I/O on a socket with the shutdown()
function.
#include <sys/socket.h>
int shutdown (int sockfd, int how);
 Returns 0 if successful or -1 if an error occurs
 Values for how:
• SHUT_RD – Reading is disabled
• SHUT_WR –Writing is disabled
• SHUT_RDWR – Reading and writing is disabled
9/13/2013 Socket Inter-Process Communication
Other Operations
2) Close:
 Deallocates the socket (similar to closing file
descriptors).
int close (int sockfd);
 Returns 0 if successful or -1 if an error occurs
 Closing occurs only last active referenced is closed (in
case dup is used)
3) Socket Options:
 Control the behaviour of sockets or allow special
behaviour
 Set and get option a particular socket
#include <sys/socket.h>
int setsockopt(int sockfd, int level, int option, const
void *val, socklen_t len);
9/13/2013 Socket Inter-Process Communication
Other Operations
int getsockopt(int sockfd, int level, int option, void
*restrict val, socklen_t *restrict lenp);
 level specifies the protocol to which the option applies
 val points to a data structure or an integer (depending
on the option)
 len specifies the size of the object specified by val
 We can set and get three kinds of options:
• Generic options which work with all sockets
• Options managed at socket level but depends
on the protocol
• Protocol-specific options
 Single UNIX Specification specifies only socket-
layer options.
9/13/2013 Socket Inter-Process Communication
Other Operations
4) Other:
 Other operations include some function having a file
descriptor argument.
 Examples are:
• Duplication through the use of dup and dup2
• lseek
9/13/2013 Socket Inter-Process Communication
 UNIX domain sockets are used for IPC within a
machine.
 Internet Domain sockets can be used for above purpose
as well as for inter-machine communication
 UNIX domain sockets are more efficient for processes
running in the same machine.
 Used only for copying data but have no
acknowledgements to send, no nw headers to
add/remove, no checksums to calculate and so on.
 Provide stream as well as datagram interface.
 Datagram sockets here are however, much reliable and
ordered than in internet domain
 It is like cross b/w sockets and pipes.
9/13/2013 Socket Inter-Process Communication
 UNIX domain sockets are specified with the
sockaddr_un structure and it differs from one
implementation to another.
 Sockets can not be used with out binding.
 Socket without a name is same as a file descriptor with
out a file name or address in file system.
9/13/2013 Socket Inter-Process Communication
9/13/2013 Socket Inter-Process Communication

More Related Content

What's hot (20)

DOCX
Deadlock detection
Nadia Nahar
 
PDF
Fault tolerance
Gaurav Rawat
 
PPT
Thin client
Vinod Kumar V H
 
PDF
Chapter 1 Introduction of Cryptography and Network security
Dr. Kapil Gupta
 
PPTX
Techniques of achieving google quality of service
Satya P. Joshi
 
PPTX
Transmission impairments
Online
 
PPTX
2.6 backup and recovery
mrmwood
 
PPT
Synchronization in distributed systems
SHATHAN
 
PPTX
Protection in general purpose operating system
Prachi Gulihar
 
PPTX
Security & Protection in Operating System
Meghaj Mallick
 
PPT
Cryptography cse,ru
Hossain Md Shakhawat
 
PPTX
Message passing in Distributed Computing Systems
Alagappa Govt Arts College, Karaikudi
 
PPTX
Slice Based testing and Object Oriented Testing
varsha sharma
 
PPTX
Virus and its CounterMeasures -- Pruthvi Monarch
Pruthvi Monarch
 
PPT
Coda file system
Sneh Pahilwani
 
PPTX
Linker and Loader
sonalikharade3
 
PPTX
Block Cipher and Operation Modes
SHUBHA CHATURVEDI
 
PDF
Lecture 2 more about parallel computing
Vajira Thambawita
 
PPT
Chapter 4 a interprocess communication
AbDul ThaYyal
 
PPTX
Go back-n protocol
STEFFY D
 
Deadlock detection
Nadia Nahar
 
Fault tolerance
Gaurav Rawat
 
Thin client
Vinod Kumar V H
 
Chapter 1 Introduction of Cryptography and Network security
Dr. Kapil Gupta
 
Techniques of achieving google quality of service
Satya P. Joshi
 
Transmission impairments
Online
 
2.6 backup and recovery
mrmwood
 
Synchronization in distributed systems
SHATHAN
 
Protection in general purpose operating system
Prachi Gulihar
 
Security & Protection in Operating System
Meghaj Mallick
 
Cryptography cse,ru
Hossain Md Shakhawat
 
Message passing in Distributed Computing Systems
Alagappa Govt Arts College, Karaikudi
 
Slice Based testing and Object Oriented Testing
varsha sharma
 
Virus and its CounterMeasures -- Pruthvi Monarch
Pruthvi Monarch
 
Coda file system
Sneh Pahilwani
 
Linker and Loader
sonalikharade3
 
Block Cipher and Operation Modes
SHUBHA CHATURVEDI
 
Lecture 2 more about parallel computing
Vajira Thambawita
 
Chapter 4 a interprocess communication
AbDul ThaYyal
 
Go back-n protocol
STEFFY D
 

Viewers also liked (18)

PDF
Remote Method Invocation
elliando dias
 
PDF
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
PPTX
Remote method invocation
Dew Shishir
 
PPTX
Inter-Process communication using pipe in FPGA based adaptive communication
Mayur Shah
 
PPTX
Introduction to C++ Remote Procedure Call (RPC)
Abdelrahman Al-Ogail
 
PPSX
Java rmi
Tanmoy Barman
 
PPTX
RPC: Remote procedure call
Sunita Sahu
 
PPT
RMI
Aravind Nair
 
PDF
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
PPS
Java rmi
kamal kotecha
 
PPT
Socket System Calls
Avinash Varma Kalidindi
 
PDF
Remote Method Invocation (RMI)
Peter R. Egli
 
PPTX
Java RMI Presentation
Masud Rahman
 
PDF
Network Sockets
Peter R. Egli
 
PPT
remote procedure calls
Ashish Kumar
 
PPS
Java rmi example program with code
kamal kotecha
 
PDF
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Varun Patel
 
PPTX
Java - Remote method invocation
Riccardo Cardin
 
Remote Method Invocation
elliando dias
 
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
Remote method invocation
Dew Shishir
 
Inter-Process communication using pipe in FPGA based adaptive communication
Mayur Shah
 
Introduction to C++ Remote Procedure Call (RPC)
Abdelrahman Al-Ogail
 
Java rmi
Tanmoy Barman
 
RPC: Remote procedure call
Sunita Sahu
 
Introduction to Remote Method Invocation (RMI)
eLink Business Innovations
 
Java rmi
kamal kotecha
 
Socket System Calls
Avinash Varma Kalidindi
 
Remote Method Invocation (RMI)
Peter R. Egli
 
Java RMI Presentation
Masud Rahman
 
Network Sockets
Peter R. Egli
 
remote procedure calls
Ashish Kumar
 
Java rmi example program with code
kamal kotecha
 
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Varun Patel
 
Java - Remote method invocation
Riccardo Cardin
 
Ad

Similar to IPC SOCKET (20)

PPT
Sockets in unix
swtjerin4u
 
PPTX
Basics of sockets
AviNash ChaVhan
 
PPT
Socket Programming
CEC Landran
 
PPT
Socket programming in C
Deepak Swain
 
PPT
Sockets
Gopaiah Sanaka
 
PPTX
L5-Sockets.pptx
ycelgemici1
 
PDF
Network Programming Assignment Help
HelpWithAssignment.com
 
PPT
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
PPT
sockets_intro.ppt
AnilGupta681764
 
PPTX
Sockets
sikhinamnagamani
 
PPTX
Socket Programming
VisualBee.com
 
PPTX
Linux Inter Process Communication
Abhishek Sagar
 
PDF
Socket programming using C
Ajit Nayak
 
PDF
socketProgramming-TCP-and UDP-overview.pdf
Shilpachaudhari10
 
PDF
Socket programming
Rajivarnan (Rajiv)
 
PPT
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
PPTX
Byte Ordering - Unit 2.pptx
RockyBhai46825
 
PDF
lab04.pdf
SaidiCalala
 
PPTX
Socket programming
Anurag Tomar
 
PPT
Sockets intro
AviNash ChaVhan
 
Sockets in unix
swtjerin4u
 
Basics of sockets
AviNash ChaVhan
 
Socket Programming
CEC Landran
 
Socket programming in C
Deepak Swain
 
Sockets
Gopaiah Sanaka
 
L5-Sockets.pptx
ycelgemici1
 
Network Programming Assignment Help
HelpWithAssignment.com
 
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
sockets_intro.ppt
AnilGupta681764
 
Sockets
sikhinamnagamani
 
Socket Programming
VisualBee.com
 
Linux Inter Process Communication
Abhishek Sagar
 
Socket programming using C
Ajit Nayak
 
socketProgramming-TCP-and UDP-overview.pdf
Shilpachaudhari10
 
Socket programming
Rajivarnan (Rajiv)
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
Byte Ordering - Unit 2.pptx
RockyBhai46825
 
lab04.pdf
SaidiCalala
 
Socket programming
Anurag Tomar
 
Sockets intro
AviNash ChaVhan
 
Ad

More from Sanoj Kumar (14)

PPTX
Internet of things
Sanoj Kumar
 
PPT
Jsp applet
Sanoj Kumar
 
PPT
Corba
Sanoj Kumar
 
PPT
Dns
Sanoj Kumar
 
PPT
Big data and Internet
Sanoj Kumar
 
PPT
Ajax
Sanoj Kumar
 
PPTX
A New Security Model For Distributed System
Sanoj Kumar
 
PPTX
A New Form of Dos attack in Cloud
Sanoj Kumar
 
PPTX
Biometrics
Sanoj Kumar
 
PPTX
Inverted page tables basic
Sanoj Kumar
 
PPTX
Hardware virtualization basic
Sanoj Kumar
 
PPTX
Dos attack basic
Sanoj Kumar
 
PPTX
Steganography basic
Sanoj Kumar
 
PPTX
Digital signatures
Sanoj Kumar
 
Internet of things
Sanoj Kumar
 
Jsp applet
Sanoj Kumar
 
Corba
Sanoj Kumar
 
Big data and Internet
Sanoj Kumar
 
Ajax
Sanoj Kumar
 
A New Security Model For Distributed System
Sanoj Kumar
 
A New Form of Dos attack in Cloud
Sanoj Kumar
 
Biometrics
Sanoj Kumar
 
Inverted page tables basic
Sanoj Kumar
 
Hardware virtualization basic
Sanoj Kumar
 
Dos attack basic
Sanoj Kumar
 
Steganography basic
Sanoj Kumar
 
Digital signatures
Sanoj Kumar
 

Recently uploaded (20)

PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
July Patch Tuesday
Ivanti
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 

IPC SOCKET

  • 2. 1. Introduction 2. Types  Stream Sockets  Sequential Sockets  Datagram Sockets  Raw Sockets 3. Creating a Socket 4. Binding a Socket 5. Using a Socket  Connection  Sending Data  Receiving Data  Other Operations 6. UNIX Socket Domain 9/13/2013 Socket Inter-Process Communication
  • 3. What is a socket?  End point of communication  Used for IPC • Within same machine • Across networks  Connection to network protocols (TCP/IP, UDP...)  Usage is similar to file (through file descriptors)  This presentation will provide a very condensed explanations of sockets, although this concept is vast 9/13/2013 Socket Inter-Process Communication
  • 4. Types of Sockets in UNIX  Stream Sockets  Sequential Sockets  Datagram Sockets  Raw Sockets 9/13/2013 Socket Inter-Process Communication
  • 5. Stream Sockets  Provides bidirectional, reliable, sequenced, and unduplicated flow of data  Designed to prevent the loss or duplication of data  Default protocol in AF_INET domain is TCP  Requires a connection before communication  When using a stream socket for data transfer, an application program needs to perform the following sequence: i. Create a connection to another socket using the connect subroutine. ii. Use the read and write subroutines (or send and recv) to transfer data. iii. Use the close subroutine to finish the session. 9/13/2013 Socket Inter-Process Communication
  • 6. Sequential Sockets  Provides sequenced, reliable, and unduplicated flow of information  Not very frequently used  Difference between SOCK_STREAM and SOCK_SEQPACKET: i. SOCK_STREAM is supported by windows and all major versions of Linux but SOCK_SEQPACKET is compatible with only certain builds. ii. Both SOCK_SEQPACKET and SOCK_STREAM handle backpressure by tossing out the offending packet but: • SOCK_SEQPACKET returns an error to the sending socket, whereas • SOCK_STREAM asks for it to be retransmitted when the buffer has space 9/13/2013 Socket Inter-Process Communication
  • 7. Datagram Sockets  Provides unreliable, non-sequenced and datagrams, which are connectionless messages of a fixed maximum length  Designed for short messages  Data packets may be duplicated  Application program to sends datagrams to correspondents named in send  Application programs can receive datagrams through sockets using the recv 9/13/2013 Socket Inter-Process Communication
  • 8. Raw Sockets  Provides access to internal network protocols and interfaces  allow an application to have direct access to lower- level communication protocols  intended for advanced users who want to take advantage of some protocol feature that is not directly accessible through a normal interface, or who want to build new protocols on top of existing low-level protocols  Like datagram sockets, these are datagram-oriented.  Superuser privileges required 9/13/2013 Socket Inter-Process Communication
  • 9.  Before using a socket, it has to be made  Its access is similar to a file descriptor  For socket creation, the socket function is called: #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol);  Returns a socket descriptor if creation is successful or - 1 on error  Domain specifies the nature of the communication where the values include: • AF_INET and AF_INET6 for Internet Domain (IPV4 and IPV6 respectively) • AF_UNIX for the UNIX domain • AF_UNSPEC for unspecified or “any” domain 9/13/2013 Socket Inter-Process Communication
  • 10.  Type is for the type of socket used: • SOCK_DGRAM – Datagram socket • SOCK_STREAM – Stream socket • SOCK_SEQPACKET – Sequential socket • SOCK_RAW – Raw socket  Protocol is usually 0 indicating that default protocol is used.  For example, to create a stream socket in the Internet domain: s = socket(AF_INET, SOCK_STREAM, 0);  Similar to calling open and obtaining file descriptor  Some functions which accept file descriptor can also be used with a socket including close(), read(), write(), fchmod(),….etc.  close() is used for closing (or deallocating) a socket9/13/2013 Socket Inter-Process Communication
  • 11.  Created sockets cannot be used without associating with an address (or a name, basically)  An address identifies a socket endpoint in a particular communication domain  Informs a process “WHERE” to look for incoming messages  For binding a socket with an address: #include <sys/socket.h> int bind(int sock_fd,const struct sockaddr *addr);  Returns 0 if success or -1 if error in binding 9/13/2013 Socket Inter-Process Communication
  • 12.  Format for the address is: struct sockaddr { sa_family_t sa_family; char sa_data[]; .. … };  Internet address is specified in <netinet/in.h> by the in_addr structure. 9/13/2013 Socket Inter-Process Communication
  • 13. Connection  For connection-oriented service like Sequential and Stream Socket  Prior connection between client and server is required before data exchange  For connection: #include <sys/socket.h> int connect(int sock_fd,const struct sockaddr *addr, socklen_t len);  Returns 0 if successful or -1 if error occurs  If a socket is not bound before connect() is called then the system will bind a default address to it  Can also be used with connection-less sockets for optimization 9/13/2013 Socket Inter-Process Communication
  • 14. Sending Data  For connection-oriented service write() can also be used, provided a connection is established  Three specific socket data transfer functions are available if we want to specify options, or receive packets from multiple clients,...etc • ssize_t send(int sock_fd,const void *buff, size_t nbytes, int flags); • ssize_t sendto(int sock_fd,const void *buff, size_t nbytes, int flags,const struct sockaddr *des_addr, socklen_t deslen); • ssize_t sendmsg(int sock_fd,const struct msghdr *msg, int flags);  Returns number of bytes sent if successful or -1 if error occurs  buff and nbytes have the same meaning as with write 9/13/2013 Socket Inter-Process Communication
  • 15. Sending Data  Sendmsg is for specifying multiple buffers from which to transmit data.  Structure of msghdr is: struct msghdr { void *msg_name; socklen_t msg_namelen; ... }  Flags have special purposes which include sending out-of-bound data, preventing packet to route out of network, etc  Flags are specifies by the constants: MSG_DONTROUTE, MSG_DONTWAIT, MSG_OOB, MSG_EOR 9/13/2013 Socket Inter-Process Communication
  • 16. Receiving Data  For connection-oriented service read() can also be used, provided a connection is established  Three specific socket data transfer functions are available if we want to specify options, or receive packets from multiple clients,...etc • ssize_t recv(int sock_fd,const void *buff, size_t nbytes, int flags); • ssize_t recvfrom(int sock_fd,const void *buff, size_t nbytes, int flags,const struct sockaddr *des_addr, socklen_t deslen); • ssize_t recvmsg(int sock_fd,const struct msghdr *msg, int flags);  Returns number of bytes sent if successful or -1 if error occurs9/13/2013 Socket Inter-Process Communication
  • 17. Other Operations 1) Shutdown:  We can “disable” I/O on a socket with the shutdown() function. #include <sys/socket.h> int shutdown (int sockfd, int how);  Returns 0 if successful or -1 if an error occurs  Values for how: • SHUT_RD – Reading is disabled • SHUT_WR –Writing is disabled • SHUT_RDWR – Reading and writing is disabled 9/13/2013 Socket Inter-Process Communication
  • 18. Other Operations 2) Close:  Deallocates the socket (similar to closing file descriptors). int close (int sockfd);  Returns 0 if successful or -1 if an error occurs  Closing occurs only last active referenced is closed (in case dup is used) 3) Socket Options:  Control the behaviour of sockets or allow special behaviour  Set and get option a particular socket #include <sys/socket.h> int setsockopt(int sockfd, int level, int option, const void *val, socklen_t len); 9/13/2013 Socket Inter-Process Communication
  • 19. Other Operations int getsockopt(int sockfd, int level, int option, void *restrict val, socklen_t *restrict lenp);  level specifies the protocol to which the option applies  val points to a data structure or an integer (depending on the option)  len specifies the size of the object specified by val  We can set and get three kinds of options: • Generic options which work with all sockets • Options managed at socket level but depends on the protocol • Protocol-specific options  Single UNIX Specification specifies only socket- layer options. 9/13/2013 Socket Inter-Process Communication
  • 20. Other Operations 4) Other:  Other operations include some function having a file descriptor argument.  Examples are: • Duplication through the use of dup and dup2 • lseek 9/13/2013 Socket Inter-Process Communication
  • 21.  UNIX domain sockets are used for IPC within a machine.  Internet Domain sockets can be used for above purpose as well as for inter-machine communication  UNIX domain sockets are more efficient for processes running in the same machine.  Used only for copying data but have no acknowledgements to send, no nw headers to add/remove, no checksums to calculate and so on.  Provide stream as well as datagram interface.  Datagram sockets here are however, much reliable and ordered than in internet domain  It is like cross b/w sockets and pipes. 9/13/2013 Socket Inter-Process Communication
  • 22.  UNIX domain sockets are specified with the sockaddr_un structure and it differs from one implementation to another.  Sockets can not be used with out binding.  Socket without a name is same as a file descriptor with out a file name or address in file system. 9/13/2013 Socket Inter-Process Communication

Editor's Notes

  • #4: Abstraction of end point of communication
  • #5: Based on implementation
  • #6: Based on implementation
  • #7: Based on implementation
  • #8: Based on implementation
  • #9: Based on implementation
  • #10: Based on implementation
  • #11: If protocol is 0 then for SOCK_STREAM the TCP protocol is used
  • #12: If protocol is 0 then for SOCK_STREAM the TCP protocol is used
  • #13: If protocol is 0 then for SOCK_STREAM the TCP protocol is used
  • #14: Based on implementation
  • #15: Based on implementation
  • #16: Out-of-band data is an optional feature supported by some communication protocols, allowinghigher-priority delivery of data than normal. Out-of-band data is sent ahead of any data that isalready queued for transmission.
  • #17: Based on implementation
  • #18: Based on implementation
  • #19: Based on implementation
  • #20: Based on implementation
  • #21: Based on implementation
  • #22: Based on implementation
  • #23: Based on implementation