SlideShare ist ein Scribd-Unternehmen logo
1 von 23
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

Weitere ähnliche Inhalte

Was ist angesagt?

Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
Tech_MX
 
Internetworking
InternetworkingInternetworking
Internetworking
Raghu nath
 

Was ist angesagt? (20)

Process Management-Process Migration
Process Management-Process MigrationProcess Management-Process Migration
Process Management-Process Migration
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programming
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
iSCSI (Internet Small Computer System Interface)
iSCSI (Internet Small Computer System Interface)iSCSI (Internet Small Computer System Interface)
iSCSI (Internet Small Computer System Interface)
 
Internetworking
InternetworkingInternetworking
Internetworking
 
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
 
Reflection in C Sharp
Reflection in C SharpReflection in C Sharp
Reflection in C Sharp
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Architecture of Linux
 Architecture of Linux Architecture of Linux
Architecture of Linux
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
FTP & TFTP
FTP & TFTPFTP & TFTP
FTP & TFTP
 
Disk scheduling algorithms
Disk scheduling algorithms Disk scheduling algorithms
Disk scheduling algorithms
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Practical Packet Analysis: Wireshark
Practical Packet Analysis: Wireshark Practical Packet Analysis: Wireshark
Practical Packet Analysis: Wireshark
 

Andere mochten auch

Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
elliando dias
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
Ralf Laemmel
 
Inter-Process communication using pipe in FPGA based adaptive communication
Inter-Process communication using pipe in FPGA based adaptive communicationInter-Process communication using pipe in FPGA based adaptive communication
Inter-Process communication using pipe in FPGA based adaptive communication
Mayur Shah
 
Introduction to C++ Remote Procedure Call (RPC)
Introduction to C++ Remote Procedure Call (RPC)Introduction to C++ Remote Procedure Call (RPC)
Introduction to C++ Remote Procedure Call (RPC)
Abdelrahman Al-Ogail
 
Chapter 4 a interprocess communication
Chapter 4 a interprocess communicationChapter 4 a interprocess communication
Chapter 4 a interprocess communication
AbDul ThaYyal
 
remote procedure calls
  remote procedure calls  remote procedure calls
remote procedure calls
Ashish Kumar
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
kamal kotecha
 
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Varun Patel
 

Andere mochten auch (19)

Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Inter-Process communication using pipe in FPGA based adaptive communication
Inter-Process communication using pipe in FPGA based adaptive communicationInter-Process communication using pipe in FPGA based adaptive communication
Inter-Process communication using pipe in FPGA based adaptive communication
 
Introduction to C++ Remote Procedure Call (RPC)
Introduction to C++ Remote Procedure Call (RPC)Introduction to C++ Remote Procedure Call (RPC)
Introduction to C++ Remote Procedure Call (RPC)
 
Java rmi
Java rmiJava rmi
Java rmi
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure call
 
RMI
RMIRMI
RMI
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
 
Java rmi
Java rmiJava rmi
Java rmi
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
 
Chapter 4 a interprocess communication
Chapter 4 a interprocess communicationChapter 4 a interprocess communication
Chapter 4 a interprocess communication
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
remote procedure calls
  remote procedure calls  remote procedure calls
remote procedure calls
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
Piping Training course-How to be an Expert in Pipe & Fittings for Oil & Gas c...
 
Java - Remote method invocation
Java - Remote method invocationJava - Remote method invocation
Java - Remote method invocation
 

Ähnlich wie IPC SOCKET

Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
Raghu nath
 
ComputerNetworksAssignment
ComputerNetworksAssignmentComputerNetworksAssignment
ComputerNetworksAssignment
Rebecca Patient
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
Raghu nath
 

Ähnlich wie IPC SOCKET (20)

Lesson 1 slideshow
Lesson 1 slideshowLesson 1 slideshow
Lesson 1 slideshow
 
TCP/IP 3RD SEM.2012 AUG.ASSIGNMENT
TCP/IP 3RD SEM.2012 AUG.ASSIGNMENTTCP/IP 3RD SEM.2012 AUG.ASSIGNMENT
TCP/IP 3RD SEM.2012 AUG.ASSIGNMENT
 
2010fall ch6 uugantsetseg
2010fall ch6 uugantsetseg2010fall ch6 uugantsetseg
2010fall ch6 uugantsetseg
 
computer network NCC l4dc assingment
computer network NCC l4dc assingment computer network NCC l4dc assingment
computer network NCC l4dc assingment
 
28 networking
28  networking28  networking
28 networking
 
pppppppppppppppppjjjjjjjjjjjpppppppp.pptx
pppppppppppppppppjjjjjjjjjjjpppppppp.pptxpppppppppppppppppjjjjjjjjjjjpppppppp.pptx
pppppppppppppppppjjjjjjjjjjjpppppppp.pptx
 
Computer Networks Notes Complete Syllabus
Computer Networks Notes Complete SyllabusComputer Networks Notes Complete Syllabus
Computer Networks Notes Complete Syllabus
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
ComputerNetworksAssignment
ComputerNetworksAssignmentComputerNetworksAssignment
ComputerNetworksAssignment
 
OSI model (7 layer )
OSI model (7 layer ) OSI model (7 layer )
OSI model (7 layer )
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Cisco doc
Cisco docCisco doc
Cisco doc
 
CCNA ppt Day 2
CCNA ppt Day 2CCNA ppt Day 2
CCNA ppt Day 2
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Internet of things protocols for resource constrained applications
Internet of things protocols for resource constrained applications Internet of things protocols for resource constrained applications
Internet of things protocols for resource constrained applications
 
C C N A Day1
C C N A  Day1C C N A  Day1
C C N A Day1
 
Ccna day1
Ccna day1Ccna day1
Ccna day1
 
Ccna day1
Ccna day1Ccna day1
Ccna day1
 
Unit 3 Assignment 1 Osi Model
Unit 3 Assignment 1 Osi ModelUnit 3 Assignment 1 Osi Model
Unit 3 Assignment 1 Osi Model
 
Lecture 3- tcp-ip
Lecture  3- tcp-ipLecture  3- tcp-ip
Lecture 3- tcp-ip
 

Mehr von Sanoj Kumar (14)

Internet of things
Internet of thingsInternet of things
Internet of things
 
Jsp applet
Jsp appletJsp applet
Jsp applet
 
Corba
CorbaCorba
Corba
 
Dns
DnsDns
Dns
 
Big data and Internet
Big data and InternetBig data and Internet
Big data and Internet
 
Ajax
AjaxAjax
Ajax
 
A New Security Model For Distributed System
A New Security Model For Distributed SystemA New Security Model For Distributed System
A New Security Model For Distributed System
 
A New Form of Dos attack in Cloud
A New Form of Dos attack in CloudA New Form of Dos attack in Cloud
A New Form of Dos attack in Cloud
 
Biometrics
BiometricsBiometrics
Biometrics
 
Inverted page tables basic
Inverted page tables basicInverted page tables basic
Inverted page tables basic
 
Hardware virtualization basic
Hardware virtualization basicHardware virtualization basic
Hardware virtualization basic
 
Dos attack basic
Dos attack basicDos attack basic
Dos attack basic
 
Steganography basic
Steganography basicSteganography basic
Steganography basic
 
Digital signatures
Digital signaturesDigital signatures
Digital signatures
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

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

Hinweis der Redaktion

  1. Abstraction of end point of communication
  2. Based on implementation
  3. Based on implementation
  4. Based on implementation
  5. Based on implementation
  6. Based on implementation
  7. Based on implementation
  8. If protocol is 0 then for SOCK_STREAM the TCP protocol is used
  9. If protocol is 0 then for SOCK_STREAM the TCP protocol is used
  10. If protocol is 0 then for SOCK_STREAM the TCP protocol is used
  11. Based on implementation
  12. Based on implementation
  13. 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.
  14. Based on implementation
  15. Based on implementation
  16. Based on implementation
  17. Based on implementation
  18. Based on implementation
  19. Based on implementation
  20. Based on implementation