SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
Socket Programming

          Srinidhi Varadarajan
Client-server paradigm
Client:
                                   application
                                   transport
   initiates contact with server    network
                                    data link
   (“speaks first”)                 physical

   typically requests service                    request
   from server,
   for Web, client is
   implemented in browser; for
   e-mail, in mail reader
                                                           reply
Server:
   provides requested service                              application
                                                           transport
   to client                                                network
                                                            data link
                                                            physical
   e.g., Web server sends
   requested Web page, mail
   server delivers e-mail
Application Layer Programming

API: application programming interface
 defines interface between application and
 transport layer

 sockets: Internet API
  – two processes communicate by sending data
    into socket, reading data out of socket
Socket Interface. What is it?
 Gives a file system like abstraction to the
 capabilities of the network.

 Each transport protocol offers a set of
 services. The socket API provides the
 abstraction to access these services

 The API defines function calls to create,
 close, read and write to/from a socket.
Socket Abstraction
 The socket is the basic abstraction for network
 communication in the socket API
  – Defines an endpoint of communication for a process
  – Operating system maintains information about the
    socket and its connection
  – Application references the socket for sends, receives,
    etc.


                       Network
         Process                      Process
            A                            B

                   Ports (Sockets)
What do you need for socket communication ?

    Basically 4 parameters
    –   Source Identifier (IP address)
    –   Source Port
    –   Destination Identifier
    –   Destination Port


    In the socket API, this information is
    communicated by binding the socket.
Creating a socket
 int socket(int domain, int type, int protocol)


Protocol Family:                     Usually
  PF_INET or                         UNSPEC
   PF_UNIX

                     Communication
                       semantics:
                   SOCK_STREAM or
                    SOCK_DGRAM


The call returns a integer identifier called a
  handle
Binding a socket
int bind (int socket, struct sockaddr *address, int addr_len)



  This call is executed by:
   – Server in TCP and UDP


  It binds the socket to the specified address. The
  address parameter specifies the local component
  of the address, e.g. IP address and UDP/TCP port
Socket Descriptors
 Operating system maintains a set of
 socket descriptors for each process
 – Note that socket descriptors are shared
   by threads
 Three data structures
 – Socket descriptor table
 – Socket data structure
 – Address data structure
Socket Descriptors
  Socket        Socket Data
 Descriptor      Structure
   Table      proto family:
0:            PF_INET
                                 Address Data
1:            service:            Structure
2:            SOCK_STREAM
                                address family:
      .
      .       local address:
      .                         AF_INET
              remote address:   host IP:
                                128.173.88.85
                      .
                      .
                      .         port:
                                80
TCP Server Side: Listen
int listen (int socket, int backlog)


 This server side call specifies the number
 of pending connections on the given
 socket.

 When the server is processing a
 connection, “backlog” number of
 connections may be pending in a queue.
TCP Server Side: Passive Open
int accept (int socket, struct sockaddr *address, int *addr_len)


   This call is executed by the server.

   The call does not return until a remote
   client has established a connection.

   When it completes, it returns a new socket
   handle corresponding to the just-
   established connection
TCP Client Side: Active Open
int connect (int socket, struct sockaddr *address, int *addr_len)

   This call is executed by the client. *address
   contains the remote address.

   The call attempts to connect the socket to a
   server. It does not return until a connection has
   been established.

   When the call completes, the socket “socket” is
   connected and ready for communication.
Sockets: Summary
   Client:
int socket(int domain, int type, int protocol)
int connect (int socket, struct sockaddr *address, int addr_len)


   Server:
int socket(int domain, int type, int protocol)
int bind (int socket, struct sockaddr *address, int addr_len)
int listen (int socket, int backlog)
int accept (int socket, struct sockaddr *address, int *addr_len)
Message Passing
 int send (int socket, char *message, int msg_len, int
           flags) (TCP)
 int sendto (int socket, void *msg, int len, int
             flags, struct sockaddr * to,
             int tolen ); (UDP)
 int write(int socket, void *msg, int len); /* TCP */


 int recv (int socket, char *buffer,   int buf_len, int
           flags) (TCP)
 int recvfrom(int socket, void *msg,   int len, int
              flags, struct sockaddr   *from, int
              *fromlen); (UDP)
 int read(int socket, void *msg, int   len); (TCP)
Summary of Basic Socket Calls
    CLIENT        Connect      SERVER
             (3-way handshake)
   connect()                   accept()




                                          new connection
    write()       Data         read()


     read()       Data         write()


    close()                    close()
Network Byte Order
 Network byte order is most-significant
 byte first
 Byte ordering at a host may differ
 Utility functions
 – htons(): Host-to-network byte order for a short
   word (2 bytes)
 – htonl(): Host-to-network byte order for a long
   word (4 bytes)
 – ntohs(): Network-to-host byte order for a short
   word
 – ntohl(): Network-to-host byte order for a long
   word
Some Other “Utility” Functions
 gethostname() -- get name of local host
 getpeername() -- get address of remote
 host
 getsockname() -- get local address of
 socket
 getXbyY() -- get protocol, host, or service
 number using known number, address, or
 port, respectively
 getsockopt() -- get current socket options
 setsockopt() -- set socket options
 ioctl() -- retrieve or set socket information
Some Other “Utility” Functions
 inet_addr() -- convert “dotted”
 character string form of IP address to
 internal binary form
 inet_ntoa() -- convert internal binary
 form of IP address to “dotted”
 character string form
Address Data Structures
struct sockaddr {
  u_short    sa_family;       // type of address
  char       sa_data[14];     // value of address
}

  sockaddr is a generic address structure

struct sockaddr_in {
  u_short    sa_family;        // type of address (AF_INET)
  u_short    sa_port;          // protocol port number
  struct     in_addr sin_addr;          // IP address
  char       sin_zero[8];      // unused (set to zero)
}
   sockaddr_in is specific instance for the Internet address
   family

Contenu connexe

Tendances

Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)UC San Diego
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unixswtjerin4u
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer ProtocolUjjayanta Bhaumik
 
TCP/IP Protocol Architeture
TCP/IP Protocol ArchitetureTCP/IP Protocol Architeture
TCP/IP Protocol ArchitetureManoj Kumar
 
UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram ProtocolPeter R. Egli
 
04 coms 525 tcpip - arp and rarp
04   coms 525 tcpip - arp and rarp04   coms 525 tcpip - arp and rarp
04 coms 525 tcpip - arp and rarpPalanivel Kuppusamy
 
Address resolution protocol (ARP)
Address resolution protocol (ARP)Address resolution protocol (ARP)
Address resolution protocol (ARP)NetProtocol Xpert
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaEdureka!
 
Transport Layer In Computer Network
Transport Layer In Computer NetworkTransport Layer In Computer Network
Transport Layer In Computer NetworkDestro Destro
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using javaUC San Diego
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationRJ Mehul Gadhiya
 

Tendances (20)

Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)
 
Chap 12 tcp
Chap 12 tcpChap 12 tcp
Chap 12 tcp
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Sockets
SocketsSockets
Sockets
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer Protocol
 
TCP/IP Protocol Architeture
TCP/IP Protocol ArchitetureTCP/IP Protocol Architeture
TCP/IP Protocol Architeture
 
UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram Protocol
 
04 coms 525 tcpip - arp and rarp
04   coms 525 tcpip - arp and rarp04   coms 525 tcpip - arp and rarp
04 coms 525 tcpip - arp and rarp
 
Np unit2
Np unit2Np unit2
Np unit2
 
Address resolution protocol (ARP)
Address resolution protocol (ARP)Address resolution protocol (ARP)
Address resolution protocol (ARP)
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
 
Transport Layer In Computer Network
Transport Layer In Computer NetworkTransport Layer In Computer Network
Transport Layer In Computer Network
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Application Layer
Application LayerApplication Layer
Application Layer
 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
 

En vedette

Micro Programmed Control Unit
Micro Programmed Control UnitMicro Programmed Control Unit
Micro Programmed Control UnitKamal Acharya
 
CIO_Defining a Career for the Future
CIO_Defining a Career for the FutureCIO_Defining a Career for the Future
CIO_Defining a Career for the FutureMichael Jenkins
 
Hướng dẫn làm bt về chuỗi.doc
Hướng dẫn làm bt về chuỗi.docHướng dẫn làm bt về chuỗi.doc
Hướng dẫn làm bt về chuỗi.docHoang Dinh Vu
 
renewable energies
renewable energiesrenewable energies
renewable energiesserzan2000
 
Tertawa Tersenyum Marah Hati
Tertawa   Tersenyum   Marah  HatiTertawa   Tersenyum   Marah  Hati
Tertawa Tersenyum Marah HatiCynthia D
 
Palacio de Cristal-Madrid
Palacio de Cristal-MadridPalacio de Cristal-Madrid
Palacio de Cristal-MadridKlagares16
 
Round 3 - S5PDH1 World Cup Stock Market
Round 3 - S5PDH1 World Cup Stock MarketRound 3 - S5PDH1 World Cup Stock Market
Round 3 - S5PDH1 World Cup Stock MarketVas Ratusau
 
Maison de vacances Ploumoguer
Maison de vacances PloumoguerMaison de vacances Ploumoguer
Maison de vacances PloumoguerErwan Person
 
TDI | OOH Advertising +91 11 2619 2619
TDI | OOH Advertising +91 11 2619 2619TDI | OOH Advertising +91 11 2619 2619
TDI | OOH Advertising +91 11 2619 2619Hiyav Bajaj
 
Developing with Adobe AIR
Developing with Adobe AIRDeveloping with Adobe AIR
Developing with Adobe AIRPeter Elst
 
Quality management introduction
Quality management introductionQuality management introduction
Quality management introductionselinasimpson2801
 

En vedette (17)

socket programming
socket programming socket programming
socket programming
 
Micro Programmed Control Unit
Micro Programmed Control UnitMicro Programmed Control Unit
Micro Programmed Control Unit
 
Application Layer
Application Layer Application Layer
Application Layer
 
CIO_Defining a Career for the Future
CIO_Defining a Career for the FutureCIO_Defining a Career for the Future
CIO_Defining a Career for the Future
 
Spectacu
SpectacuSpectacu
Spectacu
 
Hướng dẫn làm bt về chuỗi.doc
Hướng dẫn làm bt về chuỗi.docHướng dẫn làm bt về chuỗi.doc
Hướng dẫn làm bt về chuỗi.doc
 
renewable energies
renewable energiesrenewable energies
renewable energies
 
Tertawa Tersenyum Marah Hati
Tertawa   Tersenyum   Marah  HatiTertawa   Tersenyum   Marah  Hati
Tertawa Tersenyum Marah Hati
 
Palacio de Cristal-Madrid
Palacio de Cristal-MadridPalacio de Cristal-Madrid
Palacio de Cristal-Madrid
 
Round 3 - S5PDH1 World Cup Stock Market
Round 3 - S5PDH1 World Cup Stock MarketRound 3 - S5PDH1 World Cup Stock Market
Round 3 - S5PDH1 World Cup Stock Market
 
Maison de vacances Ploumoguer
Maison de vacances PloumoguerMaison de vacances Ploumoguer
Maison de vacances Ploumoguer
 
TDI | OOH Advertising +91 11 2619 2619
TDI | OOH Advertising +91 11 2619 2619TDI | OOH Advertising +91 11 2619 2619
TDI | OOH Advertising +91 11 2619 2619
 
Developing with Adobe AIR
Developing with Adobe AIRDeveloping with Adobe AIR
Developing with Adobe AIR
 
RAV Gazette Notice
RAV Gazette NoticeRAV Gazette Notice
RAV Gazette Notice
 
Ieee conference
Ieee conferenceIeee conference
Ieee conference
 
Quality management introduction
Quality management introductionQuality management introduction
Quality management introduction
 
Mievom Pres
Mievom PresMievom Pres
Mievom Pres
 

Similaire à Socket Programming

Similaire à Socket Programming (20)

socket programming
 socket programming  socket programming
socket programming
 
Sockets
Sockets Sockets
Sockets
 
Os 2
Os 2Os 2
Os 2
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
 
Java 1
Java 1Java 1
Java 1
 
Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
 
java networking
 java networking java networking
java networking
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
 
Socket
SocketSocket
Socket
 
03 sockets
03 sockets03 sockets
03 sockets
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
 
Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 

Plus de elliando dias

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slideselliando dias
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScriptelliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structureselliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de containerelliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Webelliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduinoelliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorceryelliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Designelliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makeselliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebookelliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Studyelliando dias
 

Plus de elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Dernier

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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 AutomationSafe Software
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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 interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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...Miguel Araújo
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Socket Programming

  • 1. Socket Programming Srinidhi Varadarajan
  • 2. Client-server paradigm Client: application transport initiates contact with server network data link (“speaks first”) physical typically requests service request from server, for Web, client is implemented in browser; for e-mail, in mail reader reply Server: provides requested service application transport to client network data link physical e.g., Web server sends requested Web page, mail server delivers e-mail
  • 3. Application Layer Programming API: application programming interface defines interface between application and transport layer sockets: Internet API – two processes communicate by sending data into socket, reading data out of socket
  • 4. Socket Interface. What is it? Gives a file system like abstraction to the capabilities of the network. Each transport protocol offers a set of services. The socket API provides the abstraction to access these services The API defines function calls to create, close, read and write to/from a socket.
  • 5. Socket Abstraction The socket is the basic abstraction for network communication in the socket API – Defines an endpoint of communication for a process – Operating system maintains information about the socket and its connection – Application references the socket for sends, receives, etc. Network Process Process A B Ports (Sockets)
  • 6. What do you need for socket communication ? Basically 4 parameters – Source Identifier (IP address) – Source Port – Destination Identifier – Destination Port In the socket API, this information is communicated by binding the socket.
  • 7. Creating a socket int socket(int domain, int type, int protocol) Protocol Family: Usually PF_INET or UNSPEC PF_UNIX Communication semantics: SOCK_STREAM or SOCK_DGRAM The call returns a integer identifier called a handle
  • 8. Binding a socket int bind (int socket, struct sockaddr *address, int addr_len) This call is executed by: – Server in TCP and UDP It binds the socket to the specified address. The address parameter specifies the local component of the address, e.g. IP address and UDP/TCP port
  • 9. Socket Descriptors Operating system maintains a set of socket descriptors for each process – Note that socket descriptors are shared by threads Three data structures – Socket descriptor table – Socket data structure – Address data structure
  • 10. Socket Descriptors Socket Socket Data Descriptor Structure Table proto family: 0: PF_INET Address Data 1: service: Structure 2: SOCK_STREAM address family: . . local address: . AF_INET remote address: host IP: 128.173.88.85 . . . port: 80
  • 11. TCP Server Side: Listen int listen (int socket, int backlog) This server side call specifies the number of pending connections on the given socket. When the server is processing a connection, “backlog” number of connections may be pending in a queue.
  • 12. TCP Server Side: Passive Open int accept (int socket, struct sockaddr *address, int *addr_len) This call is executed by the server. The call does not return until a remote client has established a connection. When it completes, it returns a new socket handle corresponding to the just- established connection
  • 13. TCP Client Side: Active Open int connect (int socket, struct sockaddr *address, int *addr_len) This call is executed by the client. *address contains the remote address. The call attempts to connect the socket to a server. It does not return until a connection has been established. When the call completes, the socket “socket” is connected and ready for communication.
  • 14. Sockets: Summary Client: int socket(int domain, int type, int protocol) int connect (int socket, struct sockaddr *address, int addr_len) Server: int socket(int domain, int type, int protocol) int bind (int socket, struct sockaddr *address, int addr_len) int listen (int socket, int backlog) int accept (int socket, struct sockaddr *address, int *addr_len)
  • 15. Message Passing int send (int socket, char *message, int msg_len, int flags) (TCP) int sendto (int socket, void *msg, int len, int flags, struct sockaddr * to, int tolen ); (UDP) int write(int socket, void *msg, int len); /* TCP */ int recv (int socket, char *buffer, int buf_len, int flags) (TCP) int recvfrom(int socket, void *msg, int len, int flags, struct sockaddr *from, int *fromlen); (UDP) int read(int socket, void *msg, int len); (TCP)
  • 16. Summary of Basic Socket Calls CLIENT Connect SERVER (3-way handshake) connect() accept() new connection write() Data read() read() Data write() close() close()
  • 17. Network Byte Order Network byte order is most-significant byte first Byte ordering at a host may differ Utility functions – htons(): Host-to-network byte order for a short word (2 bytes) – htonl(): Host-to-network byte order for a long word (4 bytes) – ntohs(): Network-to-host byte order for a short word – ntohl(): Network-to-host byte order for a long word
  • 18. Some Other “Utility” Functions gethostname() -- get name of local host getpeername() -- get address of remote host getsockname() -- get local address of socket getXbyY() -- get protocol, host, or service number using known number, address, or port, respectively getsockopt() -- get current socket options setsockopt() -- set socket options ioctl() -- retrieve or set socket information
  • 19. Some Other “Utility” Functions inet_addr() -- convert “dotted” character string form of IP address to internal binary form inet_ntoa() -- convert internal binary form of IP address to “dotted” character string form
  • 20. Address Data Structures struct sockaddr { u_short sa_family; // type of address char sa_data[14]; // value of address } sockaddr is a generic address structure struct sockaddr_in { u_short sa_family; // type of address (AF_INET) u_short sa_port; // protocol port number struct in_addr sin_addr; // IP address char sin_zero[8]; // unused (set to zero) } sockaddr_in is specific instance for the Internet address family