Socket Intro aca

download Socket Intro aca

of 57

Transcript of Socket Intro aca

  • 8/13/2019 Socket Intro aca

    1/57

    NJIT

    Introduction to Sockets

    and TCP/IP Protocols

  • 8/13/2019 Socket Intro aca

    2/57

    Introduction to Sockets

    A socket is one of the most fundamentaltechnologies of computer networking.

    The socket is the BSD method for accomplishing

    interprocess communication (IPC). What this means is a socket is used to allow one

    process to speak to another, very much like thetelephone is used to allow one person to speak

    to another. Many of today's most popular software

    packages -- including Web Browsers, InstantMessaging and File Sharing -- rely on sockets.

  • 8/13/2019 Socket Intro aca

    3/57

    The Socket Interface

    Funded by ARPA (Advanced Research

    Projects Agency) in 1980.

    Developed at UC Berkeley Objective: to transport TCP/IP software to

    UNIX

    The socket interface has become a defacto standard.

  • 8/13/2019 Socket Intro aca

    4/57

    History of Sockets

    Sockets were introduced in 1981 as the

    Unix BSD 4.2 generic interface for Unix

    to Unix communications over networks. In 1985, SunOS introduced NFS and

    RPC over sockets.

    In 1986, AT&T introduced the TransportLayer Interface (TLI) with socket-likefunctionality but more networkindependent.

  • 8/13/2019 Socket Intro aca

    5/57

    TCP/IP Network Standard

    The Windows socket API, Winsock, is a multi-vendor specification to standardize the use ofTCP/IP under Windows. It is based on the

    Berkeley sockets interface.

    In BSD Unix, Sockets are part of the kerneland provide standalone and networked IPCservices.

    MS-DOS, Windows, Mac OS, and OS/2provide sockets in the form of libraries.

  • 8/13/2019 Socket Intro aca

    6/57

    3 Types of Socket

    Stream sockets interface to the TCP

    (transmission control protocol). Datagram sockets interface to the UDP

    (user datagram protocol).

    Raw sockets interface to the IP (Internetprotocol).

  • 8/13/2019 Socket Intro aca

    7/57

    TCP vs. UDP

    TCP used for services with a large data capacity, and apersistent connection, while UDP is more commonly used forquick lookups, and single use query-reply actions.

    Some common examples of TCP and UDP with their default

    ports:DNS lookup UDP 53

    FTP TCP 21

    HTTP TCP 80

    POP3 TCP 110

    Windows shared printer UDP 137

    name lookup

    Telnet TCP 23

  • 8/13/2019 Socket Intro aca

    8/57

    IPv4 and IPv6

    In 1978-1982, when the TCP/IP protocols weredeveloped, provisions were made for 232(about 4billion) hosts. The address protocol, IPv4, has

    proven inadequate due to the unexpected rapidgrowth of the internet and inefficient use ofaddress space.

    IPv6 uses 16 byte (128 bit) addresses allowing

    2128addressable entities. This is roughly 1,000 IPaddresses for each square meter of the surfaceof the earth, including the oceans.

  • 8/13/2019 Socket Intro aca

    9/57

    Addresses and Headers

    Try to avoid confusion between an IP addressand an IP header. An IP header usuallyincludes the address and port number of boththe source and destination nodes, along withother information, and has attached data. Theaddress is just an identifier for a networklocation.

  • 8/13/2019 Socket Intro aca

    10/57

    IPv4 addresses

    The 32 bits of an IPv4 address are broken into 4octets, or 8 bit fields. In decimal notation, an 8bit number can be represented by the values 0-255.

    For networks of different size, the first one (forlarge networks) to three (for small networks)

    octets can be used to identify the network, whilethe rest of the octets can be used to identify thenode on the network.

  • 8/13/2019 Socket Intro aca

    11/57

    Class A, B, C, D, E Addresses

    Using reserved values for the first octet, networkaddresses are broken into classes:

    Class Avery large networks (up to 224hosts)

    Class Blarge networks (up to 216hosts)

    Class Csmall networks (up to 255 hosts)

    Class Dmulti-cast messages to multiple hosts

    Class Eaddresses not allocated and reserved. This addressing scheme is shown graphically on

    the following slides.

  • 8/13/2019 Socket Intro aca

    12/57

    Figure 3.15 IP addresses (bits)

    7 24

    Class A : 0 Netw ork ID Host ID

    14 16

    Class B: 1 0 Netw ork ID Host ID

    21 8

    Class C: 1 1 0 Netw ork ID Host ID

    28

    Class D (multicast): 1 1 1 0 Multicast address

    27

    Class E (reserved): 1 1 1 1 unused0

    Coulouris et al

  • 8/13/2019 Socket Intro aca

    13/57

    Figure 3.16 IP addresses(decimal)

    octet 1 octet 2 octet 3

    Class A: 1 to 127

    0 to 255 0 to 255 1 to 254

    Class B: 128 to 191

    Class C: 192 to 223

    224 to 239Class D (multicast):

    Network ID

    Network ID

    Network ID

    Host ID

    Host ID

    Host ID

    Multicast address

    0 to 255 0 to 255 1 to 254

    0 to 255 0 to 255 0 to 255

    0 to 255 0 to 255 0 to 255

    Multicast address

    0 to 255 0 to 255 1 to 254240 to 255Class E (reserved):

    1.0.0.0 to127.255.255.255

    128.0.0.0 to191.255.255.255

    192.0.0.0 to223.255.255.255

    224.0.0.0 to

    239.255.255.255

    240.0.0.0 to255.255.255.255

    Range of addresses

    Coulouris et al

  • 8/13/2019 Socket Intro aca

    14/57

    Socket Address (IPv4)

    A socket address on the TCP/IP internet consists oftwo parts:

    An internet(IP) address, a 32 bit number usually

    represented by 4 decimal number separated by dots.It is a unique identifier for a network interface cardwithin an administered AF_INET domain. A TCP/IPHost may have as many addresses as it has network

    interfaces. (Newer IP addresses have 6 decimal numbers) A 16 bit port number, which is an entry point to anapplication that resides on a host. Port define entrypoints for services provided by server applications.Important commercial applications such as Oraclehave their own well known ports.

  • 8/13/2019 Socket Intro aca

    15/57

    IP Protocol Approach

    Define functions that support network

    communications in general, and use

    parameters to make TCP/IPcommunication a special case.

    Socket calls refer to all TCP/IP protocols

    as a single protocol family.

  • 8/13/2019 Socket Intro aca

    16/57

    IP Protocol

    The IP protocol transmits datagrams from onehost to another with unreliable or best-effortsemantics. Delivery is not guaranteed.

    The IP layer puts datagrams into packetssuitable for transmission in the underlyingnetwork, such as Ethernet.

    It must also inform the underlying network of theaddress of the message destination usingaddress resolution.

  • 8/13/2019 Socket Intro aca

    17/57

    Address Resolution

    The address resolution module must convert aninternet address so that it can be understood bythe underlying network. For example, the 32 bit

    IPv4 address has to be converted to a 48 bitEthernet address on an Ethernet network.

    This process is specific for each network, andnetwork addressing schemes do not correlate

    directly to one another. Typically, known addressresolutions will be cached, while new addressesare found by querying each node on the network.

  • 8/13/2019 Socket Intro aca

    18/57

    Classless InterdomainRouting (CIDR)

    In 1996, due largely to the allocation of class Bnetwork addresses to small networks, the Internetbegan to run out of addresses. Network

    administrators who could not be certain that theirnetwork would not grow past 255 nodes used classB addresses instead of Class C.

    The CIDR scheme was developed to allow a series

    of contiguous class C addresses to be used for asubnet requiring more than 255 addresses. This alsoallowed existing Class B addresses to be subdivided.

  • 8/13/2019 Socket Intro aca

    19/57

    CIDR Routing Tables

    CIDR required redesign of the routing tables toavoid inefficiency, since a former class Bnetwork address might now represent manywidely separated CIDR networks.

    The solution was to add a mask field to a routingtable. The mask is used to select the portion of

    the IP address that is to be used to select thenetwork identifier as opposed to the nodeidentifier.

  • 8/13/2019 Socket Intro aca

    20/57

    Unregistered Addresses

    All of the computers and devices that access theInternet do not need globally unique IP addresses.Computers that are attached to a local network and

    access the Internet through a router can use therouter to redirect packets to the correct computer. For example, the instructors home network is

    connected through a router to a cable modem to an

    Internet provider. The single globally unique IPaddress provided by the Internet service is theaddress of the cable modem, and is shared by thefour computers on the home network.

  • 8/13/2019 Socket Intro aca

    21/57

    Network Address Translation

    Unregistered internal Internet enabled devices areassigned addresses, usually by the Dynamic HostConfiguration Protocol (DHCP). Normally, small

    networks are assigned addresses on the192.168.1.x class C subnet, while larger networksuse either the 10.z.y.x. class A subnet or the172.16.y.x Class B subnet.

    NAT enabled routers maintain an addresstranslation table and use available source anddestination port numbers to assign packets to localnodes.

  • 8/13/2019 Socket Intro aca

    22/57

    Figure 3.18 A Home Network83.215.152.95

    Ethernet switch

    Modem / f irewa ll / router (NAT enabled)

    printer

    DSL or Cableconnection to ISP

    192.168.1.xx subnet

    PC 1

    WiFi base station/access point 192.168.1.10

    192.168.1.5

    192.168.1.2

    192.168.1.1

    192.168.1.104 PC 2192.168.1.101

    Laptop

    192.168.1.105

    Game box

    192.168.1.106

    Media hub

    TV m onitor

    Bluetoothadapter

    Bluetoothprinter

    CameraCoulouris

    et al

  • 8/13/2019 Socket Intro aca

    23/57

    IPv6

    In 1994, IPv6 was adopted as a more permanentsolution to the shortage of IP addresses andmigration to it over a period of time was

    recommended. IPv6 contains not only a much larger address space,

    but also provisions desired by large Internet serviceproviders. Some of these are controversial, such as

    the ability to assign classes to packets, so a providercan give a higher quality of service to its ownsubscribers than to transient traffic on its network.

  • 8/13/2019 Socket Intro aca

    24/57

    New IPv6 Provisions

    Larger address space

    Partitioned address space

    Reduced header complexity for faster routing Traffic class and flow label headers to identify

    traffic for special handling, such as a multimediastream

    The IPv6 header format is shown on the nextslide.

  • 8/13/2019 Socket Intro aca

    25/57

    Figure 3.19 IPv6 Header

    Coulouris et al

    Source address(128 bits)

    Destination address(128 bits)

    Version (4 bits) Traffic class (8 bits) Flow label (20 bits)

    Payload length (16 bits) Hop limit (8 bits)Next header (8 bits)

    C ti O i t d

  • 8/13/2019 Socket Intro aca

    26/57

    Connection OrientedProtocols

    Also known as session-based protocols, virtualcircuits, or sequenced packet exchanges.

    Provide reliable two-way connection service over asession.

    Packets are given unique sequence numbers.

    Delivered packets are individually acknowledged. Duplicated packets are detected and discarded.

  • 8/13/2019 Socket Intro aca

    27/57

    Connection OrientedProtocols

    Connection-oriented protocols operate in threephases. The first phase is the connection setupphase, during

    which the corresponding entities establish theconnection and negotiate the parameters defining theconnection.

    The second phase is the data transferphase, duringwhich the corresponding entities exchange messagesunder the auspices of the connection.

    Finally, the connection releasephase is when thecorrespondents "tear down" the connection because it

    is no longer needed.

  • 8/13/2019 Socket Intro aca

    28/57

    TCP/IP

    TCP/IP is a family of protocols.

    TCP/IP is built on "connectionless" technology.Information is transferred as a sequence of

    "datagrams". Generally, TCP/IP applications use 4 layers:

    An application protocol such as mail .

    A protocol such as TCP that provides services need by

    many applications. IP, which provides the basic service of getting datagrams

    to their destination .

    The protocols needed to manage a specific physicalmedium, such as Ethernet or a point to point line.

  • 8/13/2019 Socket Intro aca

    29/57

    Cost of Session Oriented

    Reliable service has an overhead cost.

    You must create and manage the session.

    A lost session must be reestablished byone of the parties, a problem for faulttolerant servers that switch automatically

    to backup. Sessions are a two party affair, and not

    well suited to broadcasting.

  • 8/13/2019 Socket Intro aca

    30/57

    Basic I/O Functions in UNIX

    Sockets extend these basis I/O functions:

    open

    close

    read (see alsorecv andrecvfrom)

    write (see alsosend andsendto)

    lseek

    ioctl

  • 8/13/2019 Socket Intro aca

    31/57

    Using I/O in UNIX

    int desc;

    ...

    desc = open(file, O_RDWR, 0);read(desc, buffer, 128);

    close(desc);

  • 8/13/2019 Socket Intro aca

    32/57

    Using UNIX I/O with TCP/IP

    They extended the conventional UNIX I/O

    facilities

    It became possible to use file descriptorsfor network communication

    Extended the readand writesystem calls

    so they work with the new networkdescriptors.

  • 8/13/2019 Socket Intro aca

    33/57

    Descriptor Table

    ...

    0

    1

    2

    Internal data structure

    for file 0

  • 8/13/2019 Socket Intro aca

    34/57

    ...

    0

    1

    2

    Internal data structure

    for file 0

    Family: PF_INET

    ...

    Service: SOCK_STREAM

    Local IP:Remote IP:

    Local Port:

    Remote Port:

  • 8/13/2019 Socket Intro aca

    35/57

    Passive/Active Socket

    Apassivesocket is used by a server to

    wait for an incoming connection.

    An activesocket is used by a client toinitiate a connection.

  • 8/13/2019 Socket Intro aca

    36/57

    Sockets

    When a socket is created it does not

    contain information about how it will be

    used. TCP/IP protocols define a communication

    endpoint to consist of an IP address and a

    protocol port number.

  • 8/13/2019 Socket Intro aca

    37/57

    Sockets

    Figure A

    Figure B

  • 8/13/2019 Socket Intro aca

    38/57

    socket()

    bind()

    listen()

    accept()

    read()

    write()

    procees request

    get a blocked client

    Server Process

    TCP UDP

    socket()

    connect()

    write()

    read()

    Client Process

    socket()

    bind()

    sendto()

    recvfrom()

    Client Process

    1

    2

    3

    socket()

    bind()

    recvfrom()

    sendto()

    Server Process

    get a blocked client

    process request

    UNIX

    version

  • 8/13/2019 Socket Intro aca

    39/57

    socket()

    bind()

    listen()

    accept()

    recv()

    send()

    process request

    get a blocked client

    Server Process

    TCP UDP

    socket()

    connect()

    send()

    recv()

    Client Process

    socket()

    bind()

    sendto()

    recvfrom()

    Client Process

    1

    2

    3

    socket()

    bind()

    recvfrom()

    sendto()

    Server Process

    get a blocked client

    process request

    Winsock

    or Unix

    version

  • 8/13/2019 Socket Intro aca

    40/57

    TCP vs. UDP

    TCP (Transmission Control Protocol)

    Connection-oriented

    Reliability in delivery of messages Splitting messages into datagrams

    keep track of order (or sequence)

    Use checksums for detecting errors

  • 8/13/2019 Socket Intro aca

    41/57

    TCP vs. UDP (Contd)

    UDP (User Datagram Protocols)

    Connectionless

    No attempt to fragment messages No reassembly and synchronization

    In case of error, message is retransmitted

    No acknowledgment

  • 8/13/2019 Socket Intro aca

    42/57

    Datagrams

    Also known as connectionlessor transmit andprayprotocols.

    Simple, but unreliable. They are not trackedby sequence number or acknowledged.

    LAN Server and some others haveacknowledged datagrams.

  • 8/13/2019 Socket Intro aca

    43/57

    Datagrams

    A datagram, often called apacket, is much moreatomic in nature.

    A datagramis an independent, self-contained

    message sent over the network whose arrival,arrival time, and content are not guaranteed.

    All data sent over the channel is received in thesame order in which it was sent. This is

    guaranteed by the channel.

    In modern data networking, it is important todistinguish between datagramsand streams.

  • 8/13/2019 Socket Intro aca

    44/57

    Selecting UDP

    Remote procedures are idempotent*

    Server and client messsages fit

    completely within a packet. The server handles multiple clients (UDP

    is stateless)

    *a mathematical operation that always produces the sameresult

  • 8/13/2019 Socket Intro aca

    45/57

    Selecting TCP

    Procedures are not idempotent

    Reliability is a must

    Messages exceed UDP packet size

  • 8/13/2019 Socket Intro aca

    46/57

    IP (Raw) Socket

    To use RAW sockets in Unix it is mandatorythat one have root authority. To create aRAW socket write:s=socket(AF_INET,SOCK_RAW,[protocol])

    Then you can sending or receive over it.Raw sockets are used to generate / receive

    packets of a type that the kernel doesn'texplicitly support.

  • 8/13/2019 Socket Intro aca

    47/57

    IP Socket example

    A familiar example is PING. Ping works bysending out an ICMP (internet controlmessage protocol - another IP protocol

    distinct from TCP or UDP) echo packet.The kernel has built-in code to respond toecho/ping packets. It doesn't have code togenerate these packets, because it isn't

    required. The "ping packet generator" is aprogram in user space. It formats an ICMPecho packet and sends it out over aSOCK_RAW, waiting for a response.

  • 8/13/2019 Socket Intro aca

    48/57

    OSI Layers vs. TCP/IP

    Network

    Hardware

    Interface

    IP

    TCP UDP

    User

    Application5-7. Session

    4. Transport

    3. Network

    1-2. Data Link/

    Physical

  • 8/13/2019 Socket Intro aca

    49/57

    Four Types of Servers

    Iterative

    Connectionless

    Iterative

    Connection-

    Oriented

    Concurrent

    Connectionless

    Concurrent

    Connection-

    Oriented

  • 8/13/2019 Socket Intro aca

    50/57

    NJIT

    Summary

    Algorithms for TCP and UDP

    Clients and Servers

  • 8/13/2019 Socket Intro aca

    51/57

    TCP Client AlgorithmComer and Stevens, Algorithm 6.1

    Find IP address and protocol port number onserver

    Allocate a socket Allow TCP to allocate an arbitrary local port Connect the socket to the server Send requests and receive replies Close the connection

  • 8/13/2019 Socket Intro aca

    52/57

    TCP Iterative Server AlgorithmComer and Stevens, Algorithm 8.1

    Create a socket and bind to the well knownaddress for the service offered

    Place socket in passive mode Accept next connection request and obtain a

    new socket Repeatedly receive requests and send replies When client is done, close the connection and

    return to waiting for connection requests

  • 8/13/2019 Socket Intro aca

    53/57

    TCP Concurrent Server AlgorithmComer and Stevens, Algorithm 8.4

    Master: Create a socket and bind to the well known address

    for the service offered. Leave socket unconnected

    Place socket in passive mode Repeatedly call acceptto get requests and create a

    new slave thread

    Slave:

    Receive connection request and socket Receive requests and send responses to client

    Close connection and exit

    UDP Cli Al i h

  • 8/13/2019 Socket Intro aca

    54/57

    UDP Client AlgorithmComer and Stevens, Algorithm 6.2

    Find IP address and protocol port number onserver

    Allocate a socket Allow UDP to allocate an arbitrary local port Specify the server Send requests and receive replies Close the socket

  • 8/13/2019 Socket Intro aca

    55/57

    UDP Iterative Server AlgorithmComer and Stevens, Algorithm 8.2

    Create a socket and bind to the well knownaddress for the service offered

    Repeatedly receive requests and send replies

  • 8/13/2019 Socket Intro aca

    56/57

    UDP Concurrent Server AlgorithmComer and Stevens, Algorithm 8.3

    Master: Create a socket and bind to the well known address

    for the service offered. Leave socket unconnected

    Repeatedly call recvfromto get requests and create anew slave thread

    Slave: Receive request and access to socket

    Form reply and send to client with sendto Exit

  • 8/13/2019 Socket Intro aca

    57/57

    References

    Robert Orfali, Dan Harkey, Jeri Edwards, ClientServer Survival Guide, Third Edition, Wiley, 1999.

    Douglas E. Comer and David L. Stevens,

    Internetworking With TCP/IP, Volume III, PrenticeHall, multiple editions and dates.

    George Coularis, Jean Dollimore and Tim Kindberg,Distributed Systems, Concepts and Design, Addison

    Wesley, Fourth Edition, 2005 Figures from the Coulouris text are from the

    instructors guide and are copyrighted by PearsonEducation 2005