lecture 8 - socket programming

Upload: asitha-sandun-gamage

Post on 05-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Lecture 8 - Socket Programming

    1/16

    ICT 2406

    Internet Programming

    M.K.C.Surangika

    B.Sc.(RJT), M.Sc.(MRT)

    Faculty of Applied Sciences

    Rajarata University of Sri Lanka

    Lecture 8

    Socket Programming

  • 7/31/2019 Lecture 8 - Socket Programming

    2/16

    Client/Server Communication

    Network based systems consist of a

    server

    client

    media for communication

    ICT2406 Internet Programming2

  • 7/31/2019 Lecture 8 - Socket Programming

    3/16

    Client/Server Communication

    A computer running a program that makes request

    for services is called client machine.

    A computer running a program that offers

    requested services from one or more clients iscalled server machine.

    The media for communication can be wired or

    wireless network.

    ICT2406 Internet Programming3

  • 7/31/2019 Lecture 8 - Socket Programming

    4/16

    Socket programming

    Socket API

    introduced in BSD4.1 UNIX,

    1981 explicitly created, used, released

    by apps

    client/server paradigm

    two types of transport service via

    socket API:

    UDP

    TCP

    ICT2406 Internet Programming4

    A application-created,

    OS-controlledinterface (a

    door) into which

    application process can

    both send and

    receive messages to/from

    another application process

    socket

    Goal: learn how to build client/server application that communicate usingsockets

  • 7/31/2019 Lecture 8 - Socket Programming

    5/16

    Socket programming basics

    Server must be running

    before client can send

    anything to it.

    Server must have asocket (door) through

    which it receives and

    sends segments

    Similarly client needs a

    socket

    Socket is locally

    identified with a port

    number

    Analogous to the door # ina building

    Client needs to know

    server IP address and

    socket port number.

    ICT2406 Internet Programming5

  • 7/31/2019 Lecture 8 - Socket Programming

    6/16

    Socket-programming using TCP

    TCP service: reliable transfer ofbytes from one process to

    another

    ICT2406 Internet Programming6

    process

    TCP withbuffers,

    variables

    socket

    controlled by

    application

    developer

    controlled byoperating

    system

    host or

    server

    process

    TCP with

    buffers,variables

    socket

    controlled by

    application

    developer

    controlled by

    operatingsystem

    host or

    server

    internet

  • 7/31/2019 Lecture 8 - Socket Programming

    7/16

    Socket programming with TCP

    Client must contact server server process must first be

    running

    server must have created socket

    (door) that welcomes clients

    contact

    Client contacts server by:

    creating client-local TCP socket

    specifying IP address, port

    number of server process When client creates socket:

    client TCP establishes

    connection to server TCP

    When contacted by client, serverTCP creates new socket for

    server process to communicate

    with client

    allows server to talk with

    multiple clients

    ICT2406 Internet Programming7

    TCP provides reliable, in-order

    transfer of bytes (pipe)

    between client and server

    application viewpoint

  • 7/31/2019 Lecture 8 - Socket Programming

    8/16

    Client/server socket interaction: TCP

    ICT2406 Internet Programming8

    wait for incoming

    connection requestconnectionSocket =

    welcomeSocket.accept()

    create socket,port=x, for

    incoming request:welcomeSocket =

    ServerSocket()

    create socket,connect to hostid, port=xclientSocket =

    Socket()

    close

    connectionSocket

    read reply from

    clientSocket

    close

    clientSocket

    Server (running onhostid

    ) Client

    send request using

    clientSocketread request from

    connectionSocket

    write reply to

    connectionSocket

    TCPconnection setup

  • 7/31/2019 Lecture 8 - Socket Programming

    9/16

    A stream is a sequence ofcharacters that flow into or out

    of a process.

    An input stream is attached to

    some input source for the

    process, e.g., keyboard orsocket.

    An output stream is attached to

    an output source, e.g., monitor

    or socket.

    ICT2406 Internet Programming9

    outToServer

    to network from network

    inFromServer

    inFromUser

    keyboard monitor

    Process

    clientSocket

    input

    stream

    input

    stream

    output

    stream

    TCP

    socket

    Client

    process

    client TCP

    socket

    Stream jargon

  • 7/31/2019 Lecture 8 - Socket Programming

    10/16

    Socket programming with TCP

    Example client-server app:

    1) client reads line from standard input, sends to server via

    socket

    2) The server reads line from its connection socket

    3) The server converts line to uppercase,

    4) The server sends modified line back to client

    5) The client reads modified line from its socket, prints

    modified line

    ICT2406 Internet Programming10

  • 7/31/2019 Lecture 8 - Socket Programming

    11/16

    Example: Java client (TCP)

    ICT2406 Internet Programming11

    import java.io.*;import java.net.*;

    class TCPClient {

    public static void main(String argv[]) throws Exception

    {

    String sentence;

    String modifiedSentence;

    BufferedReader inFromUser =

    new BufferedReader(new InputStreamReader(System.in));

    Socket clientSocket = new Socket("hostname", 6789);

    DataOutputStream outToServer =

    new DataOutputStream(clientSocket.getOutputStream());

    Create

    input stream

    Create

    client socket,

    connect to server

    Create

    output stream

    attached to socket

  • 7/31/2019 Lecture 8 - Socket Programming

    12/16

    Example: Java client (TCP), cont.

    ICT2406 Internet Programming12

    BufferedReader inFromServer =

    new BufferedReader(new

    InputStreamReader(clientSocket.getInputStream()));

    sentence = inFromUser.readLine();

    outToServer.writeBytes(sentence + '\n');

    modifiedSentence = inFromServer.readLine();

    System.out.println("FROM SERVER: " + modifiedSentence);

    clientSocket.close();

    }

    }

    Create

    input stream

    attached to socket

    Send line

    to server

    Read line

    from server

  • 7/31/2019 Lecture 8 - Socket Programming

    13/16

    Example: Java server (TCP)

    ICT2406 Internet Programming13

    import java.io.*;

    import java.net.*;

    class TCPServer {

    public static void main(String argv[]) throws Exception

    {

    String clientSentence;String capitalizedSentence;

    ServerSocket welcomeSocket = new ServerSocket(6789);

    while(true) {

    Socket connectionSocket = welcomeSocket.accept();

    BufferedReader inFromClient =

    new BufferedReader(new

    InputStreamReader(connectionSocket.getInputStream()));

    Create

    welcoming socket

    at port 6789

    Wait, on welcoming

    socket for contactby client

    Create input

    stream, attached

    to socket

  • 7/31/2019 Lecture 8 - Socket Programming

    14/16

    Example: Java server (TCP), cont

    ICT2406 Internet Programming14

    DataOutputStream outToClient =

    new DataOutputStream(connectionSocket.getOutputStream());

    clientSentence = inFromClient.readLine();

    capitalizedSentence = clientSentence.toUpperCase() + '\n';

    outToClient.writeBytes(capitalizedSentence);

    }

    }}

    Read in line

    from socket

    Create output

    stream, attached

    to socket

    Write out line

    to socket

    End of while loop,

    loop back and wait for

    another client connection

  • 7/31/2019 Lecture 8 - Socket Programming

    15/16

    TCP observations & questions

    Server has two types of sockets: ServerSocket and Socket

    When client knocks on serverSockets door, server creates

    connectionSocket and completes TCP conx.

    Dest IP and port are not explicitly attached to segment. Can multiple clients use the server?

    ICT2406 Internet Programming15

  • 7/31/2019 Lecture 8 - Socket Programming

    16/16

    Sources

    [1] Slides adapted from, Computer Networking: ATop Down Approach, 5th ed., Jim Kurose, Keith

    Ross, Addison-Wesley, April 2009, Lecture

    SlidesChapter 1

    [2] Kurose, James F. & Ross, Keith W. (2005)

    Computer NetworkingA Top-Down Approach

    Featuring the Internet. 3rd ed., Pearson EducationAsia

    ICT2406 Internet Programming

    16