Ch03 Socket

Embed Size (px)

Citation preview

  • 7/31/2019 Ch03 Socket

    1/29

    Client/Server Middleware

    RPC, Messaging, Peer to Peer

  • 7/31/2019 Ch03 Socket

    2/29

    RPC, Messaging, Peer to Peer

    How do clients and servers talk to each

    other

    How are the request and response

    synchronized

    How are dissimilar data representation on

    different computer handled

    How if one of the parties are not available

  • 7/31/2019 Ch03 Socket

    3/29

    Peer to Peer communication

    Early client server application were implementedusing low level, conversational, peer to peerprotocols

    - sockets

    - TLI

    - CPIC/APPC

    - Net Bios

    - Named Pipes

    P2P protocols are hard to code and maintain

    P2P means that 2 sides of communication link usethe same protocol interface to conductconversation

  • 7/31/2019 Ch03 Socket

    4/29

    Peer to peer communication

    Not popular - solution

    Provide higher level of abstraction

    RPC, MOMs, ORBs

    Use by very demanding user app/system-levelsystem

  • 7/31/2019 Ch03 Socket

    5/29

    What is a socket?

    5

    Original idea came from UNIX

    The network is just like a file

    system

    Read and write a stream of data tothe network through socket

    A socket is bound to aport numberso

    that the TCP layer can identify the

    correct application for the data

    [email protected]

  • 7/31/2019 Ch03 Socket

    6/29

    What is a socket?

    A little background

    Socket provides the TCP/IP communication protocol

    Introduced in 1981- UNIX BSD 4.2

    Later Sun build RPC and NFS over the socket

    Is supported by all OS

    WinSocks for Windows 3.1

    Built into Windows 95, and NT For other OS it is not new

    6

    [email protected]

  • 7/31/2019 Ch03 Socket

    7/29

    Communication between processes :

    using port and socket

    7

    message

    agreed portany port

    socketsocket

    Internet address = 138.37.88.249Internet address = 138.37.94.248

    other portsclient server

    Port: The destination of a message

    Socket: The final point for processes communication

    [email protected]

  • 7/31/2019 Ch03 Socket

    8/29

    ports in TCP are also represented by a number in the

    range 1 65 535

    Ports below 1024 are restricted to use by well-known

    services. For example, Telnet (port 23)

    SMTP (port 25)

    HTTP (port 80)

    POP3 (port 110)

    8

    [email protected]

  • 7/31/2019 Ch03 Socket

    9/29

    9

    Sockets

    Figure A

    Figure B

  • 7/31/2019 Ch03 Socket

    10/29

    Socket Operations

    TCP sockets can perform a variety of

    operations:

    Establish a connection to a remote host

    Send data to a remote host

    Receive data from a remote host

    Close a connection

    10

    [email protected]

  • 7/31/2019 Ch03 Socket

    11/29

    Socket Operations

    There is a special type of socket that provides

    a service that will bind to a specific port

    number. Normally used only in servers, this

    socket can perform the following operations:

    Bind to a local port

    Accept incoming connections from remote hosts

    Unbind from a local port

    11

    [email protected]

  • 7/31/2019 Ch03 Socket

    12/29

    TCP and the Client/Server Paradigm

    In network programming, applications that

    use sockets are divided into clients and

    servers.

    A client is software that initiates a connection

    and sends requests.

    A server is software that listens for

    connections and processes requests.

    12

    [email protected]

  • 7/31/2019 Ch03 Socket

    13/29

    Network Clients

    Network clients initiate connections and controlnetwork transactions.

    The server fulfills the requests of the client but

    not the other way round. The network client speaks to the server using a

    network protocol. E.g an HTTP clientcommunicates with an HTTP server using HTTP.

    Port numbers are used to enable clients to locateserver applications. E.g. a web server uses port80.

    13

    [email protected]

  • 7/31/2019 Ch03 Socket

    14/29

    Network Servers

    The role of the network server is to bind to a specific

    port and to listen for new connections.

    Unlike the client, the server must run continually in

    the hope that some client will want its services. The server runs indefinitely. Normally, it is

    automatically started when the host computer of the

    server is started.

    14

    [email protected]

  • 7/31/2019 Ch03 Socket

    15/29

    Network Servers

    Some servers can handle only one connection at a

    time, while others can handle many connections

    concurrently, through the use ofthreads.

    Some protocols (e.g. HTTP/1.0) normally allow onlyone request per connection. Others, like POP3,

    support a sequence of requests.

    Servers answer the client request by sending either a

    response or an error message.

    15

    [email protected]

  • 7/31/2019 Ch03 Socket

    16/29

    Socket Types

    In Java, there are 4 main socket types:

    ServerSocket

    Socket

    DatagramSocket

    MulticastSocket

    Accessible by importing java.net.*;

    16

    [email protected]

  • 7/31/2019 Ch03 Socket

    17/29

    TCP Sockets and Java

    Java provide the following classes for TCPsockets:

    java.net.Socket

    java.net.ServerSocket

    The Socket class should be used whenwriting client software.

    The ServerSocket class should be usedwhen writing server software.

    17

    [email protected]

  • 7/31/2019 Ch03 Socket

    18/29

    Socket (client) Textbook page 273

    [email protected] 18

  • 7/31/2019 Ch03 Socket

    19/29

    Socket Class

    Socket objects represent client sockets, and

    is a communication channel between two TCP

    communications ports belonging to one or

    two machines.

    19

    [email protected]

  • 7/31/2019 Ch03 Socket

    20/29

    Socket Class

    There are several constructors for the Socket

    class.

    The easiest way to create a socket is shown

    below:Socket mySocket;

    try {

    mySocket = new Socket("www.aol.com", 80);

    } catch (Exception e) {

    }

    20

    [email protected]

    http://www.aol.com/http://www.aol.com/
  • 7/31/2019 Ch03 Socket

    21/29

    Reading from and Writing to TCP Sockets

    In Java, once a socket is created, it is

    connected and ready to read/write by using

    the socket's input and output streams. Use the methods getInputStream() and

    getOutputStream() to access those streams.

    21

    [email protected]

  • 7/31/2019 Ch03 Socket

    22/29

    22

    Socket socket;InputStreamReader isr;

    BufferedReader br;

    PrintStream ps;

    try {

    socket = new Socket("www.aol.com",80);

    isr = new InputStreamReader(socket.getInputStream())

    br = new BufferedReader(isr);

    ps = new PrintStream(socket.getOutputStream());

    } catch (Exception e) {

    }

    [email protected]

  • 7/31/2019 Ch03 Socket

    23/29

    ServerSockets(server) Textbook page 311

    [email protected] 23

  • 7/31/2019 Ch03 Socket

    24/29

    Server Socket

    Server Socket runs on the server and listens forincoming TCP connections.

    Server socket is bound to a certain port on the servermachine.

    When it is successfully bound to a port, it immediatelylistens to any attempt of incoming connection

    When a server detects an attempt of incomingconnection, it negotiates the connection between client

    and server. This creates/opens a regular Socket which handles

    communication between the client and server

    24

    [email protected]

  • 7/31/2019 Ch03 Socket

    25/29

    ServerSocket Class

    java.net.ServerSocket represents the serversocket

    ServerSocket object is created on a local port

    and calls method accept() to listen toincoming connection

    accept() will block until a connection is

    detected. Then it returns a Socket object thathandles communication with a client

    25

    [email protected]

  • 7/31/2019 Ch03 Socket

    26/29

    The easiest way to create a socket to listen at

    a certain port is shown below:ServerSocket mySocket;

    try {

    mySocket = new ServerSocket(80);

    } catch (Exception e) {

    }

    26

    [email protected]

  • 7/31/2019 Ch03 Socket

    27/29

    Accepting and Processing Requests from

    TCP Clients

    The most important function of a server socket is to

    accept client sockets. Once a client socket is obtained,

    the server can perform all the "real work" of server

    programming, which involves reading from andwriting to the socket to implement a network

    protocol.

    Example: a mail server that provides access to stored

    messages would listen to commands and send backmessage contents.

    27

    [email protected]

  • 7/31/2019 Ch03 Socket

    28/29

    28

    ServerSocket server;

    BufferedReader reader;

    PrintWriter writer;

    server = new ServerSocket(13);

    while (true) {

    Socket client = server.accept();

    reader = new BufferedReader(new InputStreamReader(

    client.getInputStream()));

    writer = new PrintWriter(

    new OutputStreamWriter(

    client.getOutputStream()));

    }

    [email protected]

  • 7/31/2019 Ch03 Socket

    29/29

    Take a break!

    [email protected] 29

    What are threads?