SlideShare a Scribd company logo
1 of 23
Socket
Programming


              1
What is a socket?
 An interface between application and
  network
    The application creates a socket
    The socket type dictates the style of
     communication
       • reliable vs. best effort
       • connection-oriented vs. connectionless
 Once configured the application can
   pass data to the socket for network
    transmission
   receive data from the socket (transmitted
    through the network by some other host)
                                                  2
Two essential types of sockets
 SOCK_STREAM                          SOCK_DGRAM
       TCP                                UDP
       reliable delivery                  unreliable delivery
       in-order guaranteed                no order guarantees
       connection-oriented                no notion of “connection” –
       bidirectional                       app indicates dest. for each
                                            packet
                                           can send or receive
  App

3 2                                          App                      D1
    1
         socket               Dest.
                                         3 2
                                             1
                                                   socket              D2

                                                                 D3
                                                                       3
SERVER AND CLIENTS
From: UNIX Network Programming Volume 1, figure 4.1              TCP Server
                                                                 socket()

                                                                  bind()

          TCP Client                                             listen()

           socket()                                              accept()
                                connection establishment
          connect()
                                        data request
            write()                                               read()

                                          data reply              write()
             read()

                                      end-of-file notification    read()
            close()
                                                                  close()     44
Socket Creation in C: socket
 int s = socket(domain, type, protocol);
    s: socket descriptor, an integer (like a file-handle)
    domain: integer, communication domain
        • e.g., PF_INET (IPv4 protocol) – typically used
      type: communication type
        • SOCK_STREAM: reliable, 2-way, connection-based
          service
        • SOCK_DGRAM: unreliable, connectionless,
        • other values: need root permission, rarely used, or
          obsolete
    protocol: specifies protocol (see file /etc/protocols
     for a list of options) - usually set to 0
 NOTE: socket call does not specify where data will be
  coming from, nor where it will be going to – it just
  creates the interface!                                   5
Ports
 Each host has 65,536
                            Port 0
  ports
                            Port 1
 Some ports are
 reserved for specific
 apps                       Port 65535

   20,21: FTP
                      A socket provides an interface
   23: Telnet          to send data to/from the
   80: HTTP            network through a port
   see RFC 1700 (about
    2000 ports are
    reserved)
                                                        6
The bind function
 associates and (can exclusively) reserves a
  port for use by the socket
 int status = bind(sockid, &addrport, size);
    status: error status, = -1 if bind failed
    sockid: integer, socket descriptor
    addrport: struct sockaddr, the (IP) address and
     port of the machine (address usually set to
     INADDR_ANY – chooses a local address)
    size: the size (in bytes) of the addrport
     structure

                                                       7
Skipping the bind
 SOCK_DGRAM:
   if only sending, no need to bind. The OS finds a
    port each time the socket sends a pkt
   if receiving, need to bind

 SOCK_STREAM:
   destination determined during conn. setup
   don‟t need to know port sending from (during
    connection setup, receiving end is informed of
    port)



                                                       8
Connection Setup                     (SOCK_STREAM)

 Recall: no connection setup for SOCK_DGRAM
 A connection occurs between two kinds of
  participants
      passive: waits for an active participant to request
       connection
      active: initiates connection request to passive side
 Once connection is established, passive and active
  participants are “similar”
      both can send & receive data
      either can terminate the connection




                                                              9
Connection setup cont‟d
 Passive participant         Active participant
    step 1: listen (for
     incoming requests)
                                   step 2: request &
    step 3: accept (a
                                    establish connection
     request)
    step 4: data transfer
                                   step 4: data transfer
 The accepted
  connection is on a new         Passive Participant
  socket                       a-sock-1    l-sock   a-sock-2
 The old socket
  continues to listen for
  other active
  participants                  socket               socket
 Why?
                              Active 1              Active 2
                                                               10
Connection setup: listen & accept
 Called by passive participant
 int status = listen(sock, queuelen);
    status: 0 if listening, -1 if error
    sock: integer, socket descriptor
    queuelen: integer, # of active participants that can
      “wait” for a connection
    listen is non-blocking: returns immediately

 int s = accept(sock, &name, &namelen);
    s: integer, the new socket (used for data-transfer)
    sock: integer, the orig. socket (being listened on)
    name: struct sockaddr, address of the active participant
    namelen: sizeof(name): value/result parameter
        • must be set appropriately before call
        • adjusted by OS upon return
      accept is blocking: waits for connection before returning
                                                                   11
connect call
 int status = connect(sock, &name, namelen);
    status: 0 if successful connect, -1 otherwise
    sock: integer, socket to be used in connection
    name: struct sockaddr: address of passive
     participant
    namelen: integer, sizeof(name)

 connect is blocking




                                                      12
Sending / Receiving Data
 With a connection (SOCK_STREAM):
   int count = send(sock, &buf, len, flags);
        •   count: # bytes transmitted (-1 if error)
        •   buf: char[], buffer to be transmitted
        •   len: integer, length of buffer (in bytes) to transmit
        •   flags: integer, special options, usually just 0
    int    count = recv(sock, &buf, len, flags);
        •   count: # bytes received (-1 if error)
        •   buf: void[], stores received bytes
        •   len: # bytes received
        •   flags: integer, special options, usually just 0
      Calls are blocking [returns only after data is sent
       (to socket buf) / received]
                                                                    13
Sending / Receiving Data                             (cont‟d)
 Without a connection (SOCK_DGRAM):
      int count = sendto(sock, &buf, len, flags, &addr, addrlen);
        • count, sock, buf, len, flags: same as send
        • addr: struct sockaddr, address of the destination
        • addrlen: sizeof(addr)
      int count = recvfrom(sock, &buf, len, flags, &addr,
                            &addrlen);
        • count, sock, buf, len, flags: same as recv
        • name: struct sockaddr, address of the source
        • namelen: sizeof(name): value/result parameter
 Calls are blocking [returns only after data is sent (to
  socket buf) / received]

                                                                  14
close
 When finished using a socket, the socket
  should be closed:
 status = close(s);
   status: 0 if successful, -1 if error
   s: the file descriptor (socket being closed)

 Closing a socket
   closes a connection (for SOCK_STREAM)
   frees up the port used by the socket




                                                   15
The struct sockaddr
 The generic:                  The Internet-specific:
   struct sockaddr {              struct sockaddr_in {
       u_short sa_family;             short sin_family;
       char sa_data[14];              u_short sin_port;
   };                                 struct in_addr sin_addr;
                                      char sin_zero[8];
                                  };
      sa_family
                                   sin_family = AF_INET
        • specifies which
                                   sin_port: port # (0-65535)
          address family is
          being used               sin_addr: IP-address

        • determines how the       sin_zero: unused
          remaining 14 bytes
          are used

                                                                 16
Address and port byte-ordering
  Address and port are stored as
    integers                                        struct in_addr {
        u_short sin_port; (16 bit)                   u_long s_addr;
        in_addr sin_addr; (32 bit)                 };
 Problem:
    different machines / OS‟s use different word orderings
         • little-endian: lower bytes first
         • big-endian: higher bytes first
      these machines may communicate with one another over the
       network
                   Big-Endian
                   machine                    Little-Endian
                                                                    12.40.119.128
 128.119.40.12                                machine

          128    119   40    12                    128   119   40     12
                                                                               17
Solution: Network Byte-Ordering
 Defs:
    Host Byte-Ordering: the byte ordering
     used by a host (big or little)
    Network Byte-Ordering: the byte
     ordering used by the network – always
     big-endian
 Any words sent through the network
  should be converted to Network Byte-
  Order prior to transmission (and back to
  Host Byte-Order once received)


                                             18
UNIX‟s byte-ordering funcs
 u_long htonl(u_long x);         u_long ntohl(u_long x);
 u_short htons(u_short x);       u_short ntohs(u_short x);


  On big-endian machines, these routines do nothing
  On little-endian machines, they reverse the byte
    order
                   Big-Endian
                   machine         Little-Endian12       40 119 128
                                                         128.119.40.12
128 119 40
 128.119.40.12    12
                                   machine




                                                                   ntohl
          128    119   40   12          128   119   40     12


  Same code would have worked regardless of endian-
    ness of the two machines                                       19
Other useful functions
 bzero(char* c, int n): 0‟s n bytes starting at c
 gethostname(char *name, int len): gets the name of
  the current host
 gethostbyaddr(char *addr, int len, int type): converts
  IP hostname to structure containing long integer
 inet_addr(const char *cp): converts dotted-decimal
  char-string to long integer
 inet_ntoa(const struct in_addr in): converts long to
  dotted-decimal notation




                                                           20
Release of ports
 Sometimes, a “rough” exit from a program (e.g.,
  ctrl-c) does not properly free up a port
 Eventually (after a few minutes), the port will be
  freed
 To reduce the likelihood of this problem, include
  the following code:
       #include <signal.h>
       void cleanExit(){exit(0);}
      in socket code:
       signal(SIGTERM, cleanExit);
       signal(SIGINT, cleanExit);

                                                       21
Ex1 Echo Server and Client

 Aim:
 Aim of this exercise is to implement Echo server and client
    using TCP socket
   Algorithm- Server- echoes message sent by client
   1. Create a TCP server socket.
   2. Allocate memory using „memset‟ for data exchange.
   3. Set initial parameters like „sin_family‟ and „sin_port‟ of
    server socket.
   4. Bind the server socket.
   5. Keep listening for client connection.
   6. Once an invitation for connection is sensed, accept the
    connection.
   7. Receive bytes from the accepted connection and echo it.
   8. If „q‟ or ‟Q‟ is received from client, close the connection.
   9. Terminate the program.

                                                                      22
Algorithm- Client- sends message
to server

 1. Create a TCP client socket.
 2. Allocate memory using „memset‟ for data
    exchange.
   3. Set initial parameters like „sin_family‟ and
    „sin_port‟ of server socket.
   4. Read data to be echoed in the server.
   5. Connect the client socket to server.
   6. Send data through the established connection.
   7. Send „q‟ or „Q‟ to client when all data have been
    sent.
   8. Close the connection.
                                                           23

More Related Content

What's hot

Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming TutorialJignesh Patel
 
Socket programming or network programming
Socket programming or network programmingSocket programming or network programming
Socket programming or network programmingMmanan91
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using CAjit Nayak
 
Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
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!
 
TCP/IP 3-way Handshake
TCP/IP 3-way Handshake TCP/IP 3-way Handshake
TCP/IP 3-way Handshake Alok Tripathi
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unixswtjerin4u
 
Chapter 4 data link layer
Chapter 4 data link layerChapter 4 data link layer
Chapter 4 data link layerNaiyan Noor
 
Internet control message protocol
Internet control message protocolInternet control message protocol
Internet control message protocolasimnawaz54
 
Address resolution protocol (ARP)
Address resolution protocol (ARP)Address resolution protocol (ARP)
Address resolution protocol (ARP)NetProtocol Xpert
 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddressSayak Sarkar
 
SMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOLSMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOLVidhu Arora
 
IPC (Inter-Process Communication) with Shared Memory
IPC (Inter-Process Communication) with Shared MemoryIPC (Inter-Process Communication) with Shared Memory
IPC (Inter-Process Communication) with Shared MemoryHEM DUTT
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Pythondidip
 

What's hot (20)

Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Socket programming or network programming
Socket programming or network programmingSocket programming or network programming
Socket programming or network programming
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Socket programming
Socket programmingSocket programming
Socket programming
 
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
 
TCP/IP 3-way Handshake
TCP/IP 3-way Handshake TCP/IP 3-way Handshake
TCP/IP 3-way Handshake
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Chapter 4 data link layer
Chapter 4 data link layerChapter 4 data link layer
Chapter 4 data link layer
 
Dhcp
DhcpDhcp
Dhcp
 
Internet control message protocol
Internet control message protocolInternet control message protocol
Internet control message protocol
 
Address resolution protocol (ARP)
Address resolution protocol (ARP)Address resolution protocol (ARP)
Address resolution protocol (ARP)
 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
SMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOLSMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOL
 
Java - Sockets
Java - SocketsJava - Sockets
Java - Sockets
 
IPC (Inter-Process Communication) with Shared Memory
IPC (Inter-Process Communication) with Shared MemoryIPC (Inter-Process Communication) with Shared Memory
IPC (Inter-Process Communication) with Shared Memory
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Python
 

Viewers also liked

Network Implementation and Support Lesson 09 Group Policy - Eric Vanderburg
Network Implementation and Support Lesson 09   Group Policy - Eric VanderburgNetwork Implementation and Support Lesson 09   Group Policy - Eric Vanderburg
Network Implementation and Support Lesson 09 Group Policy - Eric VanderburgEric Vanderburg
 
Listen and accept function
Listen and accept functionListen and accept function
Listen and accept functionJithin Parakka
 
Node worshop Realtime - Socket.io
Node worshop Realtime - Socket.ioNode worshop Realtime - Socket.io
Node worshop Realtime - Socket.ioCaesar Chi
 
Node js oc meetup 2 socket io intro
Node js oc meetup 2 socket io introNode js oc meetup 2 socket io intro
Node js oc meetup 2 socket io introeddify
 
Socket io - JSZurich
Socket io - JSZurichSocket io - JSZurich
Socket io - JSZurichstreunerlein
 
TCP echo 서버 및 클라이언트 예제 스터디
TCP echo 서버 및 클라이언트 예제 스터디TCP echo 서버 및 클라이언트 예제 스터디
TCP echo 서버 및 클라이언트 예제 스터디quxn6
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tddMarcos Iglesias
 
Realtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIORealtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIOHüseyin BABAL
 
Data visualization
Data visualizationData visualization
Data visualizationsagalabo
 
Building your First MEAN App
Building your First MEAN AppBuilding your First MEAN App
Building your First MEAN AppMongoDB
 
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014Stéphane ESCANDELL
 
Socket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time ApplicationSocket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time ApplicationVorakamol Choonhasakulchok
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONTomomi Imura
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsasync_io
 
Building notification system in NodeJS + Redis
Building notification system in NodeJS + RedisBuilding notification system in NodeJS + Redis
Building notification system in NodeJS + RedisLe Duc
 

Viewers also liked (20)

Network Implementation and Support Lesson 09 Group Policy - Eric Vanderburg
Network Implementation and Support Lesson 09   Group Policy - Eric VanderburgNetwork Implementation and Support Lesson 09   Group Policy - Eric Vanderburg
Network Implementation and Support Lesson 09 Group Policy - Eric Vanderburg
 
Listen and accept function
Listen and accept functionListen and accept function
Listen and accept function
 
Node worshop Realtime - Socket.io
Node worshop Realtime - Socket.ioNode worshop Realtime - Socket.io
Node worshop Realtime - Socket.io
 
Node js oc meetup 2 socket io intro
Node js oc meetup 2 socket io introNode js oc meetup 2 socket io intro
Node js oc meetup 2 socket io intro
 
Socket io - JSZurich
Socket io - JSZurichSocket io - JSZurich
Socket io - JSZurich
 
TCP echo 서버 및 클라이언트 예제 스터디
TCP echo 서버 및 클라이언트 예제 스터디TCP echo 서버 및 클라이언트 예제 스터디
TCP echo 서버 및 클라이언트 예제 스터디
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tdd
 
Realtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIORealtime web applications with ExpressJS and SocketIO
Realtime web applications with ExpressJS and SocketIO
 
tea
teatea
tea
 
Data visualization
Data visualizationData visualization
Data visualization
 
Transforming WebSockets
Transforming WebSocketsTransforming WebSockets
Transforming WebSockets
 
Building your First MEAN App
Building your First MEAN AppBuilding your First MEAN App
Building your First MEAN App
 
Socket.io (part 1)
Socket.io (part 1)Socket.io (part 1)
Socket.io (part 1)
 
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
NodeJS & Socket IO on Microsoft Azure Cloud Web Sites - DWX 2014
 
Socket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time ApplicationSocket.IO - Alternative Ways for Real-time Application
Socket.IO - Alternative Ways for Real-time Application
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
Building notification system in NodeJS + Redis
Building notification system in NodeJS + RedisBuilding notification system in NodeJS + Redis
Building notification system in NodeJS + Redis
 
Unit 3
Unit 3Unit 3
Unit 3
 

Similar to Socket Programming

Similar to Socket Programming (20)

Np unit2
Np unit2Np unit2
Np unit2
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.ppt
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Sockets
Sockets Sockets
Sockets
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
Sockets
Sockets Sockets
Sockets
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
sockets
socketssockets
sockets
 
Os 2
Os 2Os 2
Os 2
 
Sockets
SocketsSockets
Sockets
 
Sockets
SocketsSockets
Sockets
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using python
 
lab04.pdf
lab04.pdflab04.pdf
lab04.pdf
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
Npc08
Npc08Npc08
Npc08
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 
socket programming
 socket programming  socket programming
socket programming
 
socket programming
socket programming socket programming
socket programming
 

More from VisualBee.com

Homenagem para luiz e marcos (shared using VisualBee)
Homenagem para luiz e marcos 
 (shared using VisualBee)Homenagem para luiz e marcos 
 (shared using VisualBee)
Homenagem para luiz e marcos (shared using VisualBee)VisualBee.com
 
PowerPoint Presentation (shared using VisualBee)
PowerPoint Presentation (shared using VisualBee)PowerPoint Presentation (shared using VisualBee)
PowerPoint Presentation (shared using VisualBee)VisualBee.com
 
PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...
PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...
PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...VisualBee.com
 
The bible and I (shared using VisualBee)
The bible and I (shared using VisualBee)The bible and I (shared using VisualBee)
The bible and I (shared using VisualBee)VisualBee.com
 
bb (shared using VisualBee)
bb  
(shared using VisualBee)bb  
(shared using VisualBee)
bb (shared using VisualBee)VisualBee.com
 
Chua nhat III mua Thuong Nien - Nam C
Chua nhat III mua Thuong Nien - Nam CChua nhat III mua Thuong Nien - Nam C
Chua nhat III mua Thuong Nien - Nam CVisualBee.com
 
LA FE QUE AGRADA A DIOS
LA FE QUE AGRADA A DIOSLA FE QUE AGRADA A DIOS
LA FE QUE AGRADA A DIOSVisualBee.com
 
Martin Luther king JR
Martin Luther king JRMartin Luther king JR
Martin Luther king JRVisualBee.com
 
Diapositive 1 (shared using http://VisualBee.com).
Diapositive 1 (shared using http://VisualBee.com).Diapositive 1 (shared using http://VisualBee.com).
Diapositive 1 (shared using http://VisualBee.com).VisualBee.com
 

More from VisualBee.com (20)

Homenagem para luiz e marcos (shared using VisualBee)
Homenagem para luiz e marcos 
 (shared using VisualBee)Homenagem para luiz e marcos 
 (shared using VisualBee)
Homenagem para luiz e marcos (shared using VisualBee)
 
PowerPoint Presentation (shared using VisualBee)
PowerPoint Presentation (shared using VisualBee)PowerPoint Presentation (shared using VisualBee)
PowerPoint Presentation (shared using VisualBee)
 
PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...
PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...
PowerPoint Presentation (shared using http://VisualBee.com). (shared using Vi...
 
The bible and I (shared using VisualBee)
The bible and I (shared using VisualBee)The bible and I (shared using VisualBee)
The bible and I (shared using VisualBee)
 
bb bb b
bb bb bbb bb b
bb bb b
 
bb (shared using VisualBee)
bb  
(shared using VisualBee)bb  
(shared using VisualBee)
bb (shared using VisualBee)
 
bb
bbbb
bb
 
loki
lokiloki
loki
 
ASH WEDNESDAY
ASH WEDNESDAYASH WEDNESDAY
ASH WEDNESDAY
 
hijospreferidos
hijospreferidoshijospreferidos
hijospreferidos
 
yo
yoyo
yo
 
hijo preferido
hijo preferidohijo preferido
hijo preferido
 
Alcoholismo
AlcoholismoAlcoholismo
Alcoholismo
 
west love
west lovewest love
west love
 
jaa
jaajaa
jaa
 
Chua nhat III mua Thuong Nien - Nam C
Chua nhat III mua Thuong Nien - Nam CChua nhat III mua Thuong Nien - Nam C
Chua nhat III mua Thuong Nien - Nam C
 
LA FE QUE AGRADA A DIOS
LA FE QUE AGRADA A DIOSLA FE QUE AGRADA A DIOS
LA FE QUE AGRADA A DIOS
 
Martin Luther king JR
Martin Luther king JRMartin Luther king JR
Martin Luther king JR
 
Diapositive 1 (shared using http://VisualBee.com).
Diapositive 1 (shared using http://VisualBee.com).Diapositive 1 (shared using http://VisualBee.com).
Diapositive 1 (shared using http://VisualBee.com).
 
my cara de empanaaa
my cara de empanaaamy cara de empanaaa
my cara de empanaaa
 

Socket Programming

  • 2. What is a socket?  An interface between application and network  The application creates a socket  The socket type dictates the style of communication • reliable vs. best effort • connection-oriented vs. connectionless  Once configured the application can  pass data to the socket for network transmission  receive data from the socket (transmitted through the network by some other host) 2
  • 3. Two essential types of sockets  SOCK_STREAM  SOCK_DGRAM  TCP  UDP  reliable delivery  unreliable delivery  in-order guaranteed  no order guarantees  connection-oriented  no notion of “connection” –  bidirectional app indicates dest. for each packet  can send or receive App 3 2 App D1 1 socket Dest. 3 2 1 socket D2 D3 3
  • 4. SERVER AND CLIENTS From: UNIX Network Programming Volume 1, figure 4.1 TCP Server socket() bind() TCP Client listen() socket() accept() connection establishment connect() data request write() read() data reply write() read() end-of-file notification read() close() close() 44
  • 5. Socket Creation in C: socket  int s = socket(domain, type, protocol);  s: socket descriptor, an integer (like a file-handle)  domain: integer, communication domain • e.g., PF_INET (IPv4 protocol) – typically used  type: communication type • SOCK_STREAM: reliable, 2-way, connection-based service • SOCK_DGRAM: unreliable, connectionless, • other values: need root permission, rarely used, or obsolete  protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0  NOTE: socket call does not specify where data will be coming from, nor where it will be going to – it just creates the interface! 5
  • 6. Ports  Each host has 65,536 Port 0 ports Port 1  Some ports are reserved for specific apps Port 65535  20,21: FTP  A socket provides an interface  23: Telnet to send data to/from the  80: HTTP network through a port  see RFC 1700 (about 2000 ports are reserved) 6
  • 7. The bind function  associates and (can exclusively) reserves a port for use by the socket  int status = bind(sockid, &addrport, size);  status: error status, = -1 if bind failed  sockid: integer, socket descriptor  addrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY – chooses a local address)  size: the size (in bytes) of the addrport structure 7
  • 8. Skipping the bind  SOCK_DGRAM:  if only sending, no need to bind. The OS finds a port each time the socket sends a pkt  if receiving, need to bind  SOCK_STREAM:  destination determined during conn. setup  don‟t need to know port sending from (during connection setup, receiving end is informed of port) 8
  • 9. Connection Setup (SOCK_STREAM)  Recall: no connection setup for SOCK_DGRAM  A connection occurs between two kinds of participants  passive: waits for an active participant to request connection  active: initiates connection request to passive side  Once connection is established, passive and active participants are “similar”  both can send & receive data  either can terminate the connection 9
  • 10. Connection setup cont‟d  Passive participant  Active participant  step 1: listen (for incoming requests)  step 2: request &  step 3: accept (a establish connection request)  step 4: data transfer  step 4: data transfer  The accepted connection is on a new Passive Participant socket a-sock-1 l-sock a-sock-2  The old socket continues to listen for other active participants socket socket  Why? Active 1 Active 2 10
  • 11. Connection setup: listen & accept  Called by passive participant  int status = listen(sock, queuelen);  status: 0 if listening, -1 if error  sock: integer, socket descriptor  queuelen: integer, # of active participants that can “wait” for a connection  listen is non-blocking: returns immediately  int s = accept(sock, &name, &namelen);  s: integer, the new socket (used for data-transfer)  sock: integer, the orig. socket (being listened on)  name: struct sockaddr, address of the active participant  namelen: sizeof(name): value/result parameter • must be set appropriately before call • adjusted by OS upon return  accept is blocking: waits for connection before returning 11
  • 12. connect call  int status = connect(sock, &name, namelen);  status: 0 if successful connect, -1 otherwise  sock: integer, socket to be used in connection  name: struct sockaddr: address of passive participant  namelen: integer, sizeof(name)  connect is blocking 12
  • 13. Sending / Receiving Data  With a connection (SOCK_STREAM):  int count = send(sock, &buf, len, flags); • count: # bytes transmitted (-1 if error) • buf: char[], buffer to be transmitted • len: integer, length of buffer (in bytes) to transmit • flags: integer, special options, usually just 0  int count = recv(sock, &buf, len, flags); • count: # bytes received (-1 if error) • buf: void[], stores received bytes • len: # bytes received • flags: integer, special options, usually just 0  Calls are blocking [returns only after data is sent (to socket buf) / received] 13
  • 14. Sending / Receiving Data (cont‟d)  Without a connection (SOCK_DGRAM):  int count = sendto(sock, &buf, len, flags, &addr, addrlen); • count, sock, buf, len, flags: same as send • addr: struct sockaddr, address of the destination • addrlen: sizeof(addr)  int count = recvfrom(sock, &buf, len, flags, &addr, &addrlen); • count, sock, buf, len, flags: same as recv • name: struct sockaddr, address of the source • namelen: sizeof(name): value/result parameter  Calls are blocking [returns only after data is sent (to socket buf) / received] 14
  • 15. close  When finished using a socket, the socket should be closed:  status = close(s);  status: 0 if successful, -1 if error  s: the file descriptor (socket being closed)  Closing a socket  closes a connection (for SOCK_STREAM)  frees up the port used by the socket 15
  • 16. The struct sockaddr  The generic:  The Internet-specific: struct sockaddr { struct sockaddr_in { u_short sa_family; short sin_family; char sa_data[14]; u_short sin_port; }; struct in_addr sin_addr; char sin_zero[8]; };  sa_family  sin_family = AF_INET • specifies which  sin_port: port # (0-65535) address family is being used  sin_addr: IP-address • determines how the  sin_zero: unused remaining 14 bytes are used 16
  • 17. Address and port byte-ordering  Address and port are stored as integers struct in_addr {  u_short sin_port; (16 bit) u_long s_addr;  in_addr sin_addr; (32 bit) };  Problem:  different machines / OS‟s use different word orderings • little-endian: lower bytes first • big-endian: higher bytes first  these machines may communicate with one another over the network Big-Endian machine Little-Endian 12.40.119.128 128.119.40.12 machine 128 119 40 12 128 119 40 12 17
  • 18. Solution: Network Byte-Ordering  Defs:  Host Byte-Ordering: the byte ordering used by a host (big or little)  Network Byte-Ordering: the byte ordering used by the network – always big-endian  Any words sent through the network should be converted to Network Byte- Order prior to transmission (and back to Host Byte-Order once received) 18
  • 19. UNIX‟s byte-ordering funcs  u_long htonl(u_long x);  u_long ntohl(u_long x);  u_short htons(u_short x);  u_short ntohs(u_short x);  On big-endian machines, these routines do nothing  On little-endian machines, they reverse the byte order Big-Endian machine Little-Endian12 40 119 128 128.119.40.12 128 119 40 128.119.40.12 12 machine ntohl 128 119 40 12 128 119 40 12  Same code would have worked regardless of endian- ness of the two machines 19
  • 20. Other useful functions  bzero(char* c, int n): 0‟s n bytes starting at c  gethostname(char *name, int len): gets the name of the current host  gethostbyaddr(char *addr, int len, int type): converts IP hostname to structure containing long integer  inet_addr(const char *cp): converts dotted-decimal char-string to long integer  inet_ntoa(const struct in_addr in): converts long to dotted-decimal notation 20
  • 21. Release of ports  Sometimes, a “rough” exit from a program (e.g., ctrl-c) does not properly free up a port  Eventually (after a few minutes), the port will be freed  To reduce the likelihood of this problem, include the following code: #include <signal.h> void cleanExit(){exit(0);}  in socket code: signal(SIGTERM, cleanExit); signal(SIGINT, cleanExit); 21
  • 22. Ex1 Echo Server and Client  Aim:  Aim of this exercise is to implement Echo server and client using TCP socket  Algorithm- Server- echoes message sent by client  1. Create a TCP server socket.  2. Allocate memory using „memset‟ for data exchange.  3. Set initial parameters like „sin_family‟ and „sin_port‟ of server socket.  4. Bind the server socket.  5. Keep listening for client connection.  6. Once an invitation for connection is sensed, accept the connection.  7. Receive bytes from the accepted connection and echo it.  8. If „q‟ or ‟Q‟ is received from client, close the connection.  9. Terminate the program. 22
  • 23. Algorithm- Client- sends message to server  1. Create a TCP client socket.  2. Allocate memory using „memset‟ for data exchange.  3. Set initial parameters like „sin_family‟ and „sin_port‟ of server socket.  4. Read data to be echoed in the server.  5. Connect the client socket to server.  6. Send data through the established connection.  7. Send „q‟ or „Q‟ to client when all data have been sent.  8. Close the connection. 23