18
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/2009 1 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from the recommended textbook are by Lami Kaya, [email protected]. Lecture powerpoints are © 2009 Pearson Education Inc. Their content has sometimes been edited by Andy Brooks. socket/tengill

NET0183Lec31

Embed Size (px)

DESCRIPTION

NET0183Lec31

Citation preview

  • NET0183 Networks and Communications

    Lecture 31The Socket API

    8/25/2009 1NET0183 Networks and Communications

    by Dr Andy Brooks

    Lecture powerpoints from the recommended textbook are by Lami Kaya, [email protected] powerpoints are 2009 Pearson Education Inc.Their content has sometimes been edited by Andy Brooks.

    socket/tengill

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks2

    The recommended textbook is Computer Networks and Internets by Douglas E. Comerhttp://www.coursesmart.com/0136066992/?a=1773944www.pearson-books.com/student (for additional discounts and offers)

  • Socket @ Webopedia 31. mars 2010

    8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks3

    http://www.webopedia.com/TERM/s/socket.html

    (1) In UNIX and some other operating systems, a software object that connects an application to a network protocol. In UNIX, for example, a program can send and receive TCP/IP messages by opening a socket and reading and writing data to and from the socket. This simplifies program development because the programmer need only worry about manipulating the socket and can rely on the operating system to actually transport messages across the network correctly. Note that a socket in this sense is completely soft - it's a software object, not a physical component.

  • 4Socket: a door between an application process and a transport protocol (e.g. TCP)

    process

    TCP withbuffers,variables

    socket

    controlled byapplicationdeveloper

    controlled byoperating

    system

    host orserver

    process

    TCP withbuffers,variables

    socket

    controlled byapplicationdeveloper

    controlled byoperatingsystem

    host orserver

    internet

    from Kurose and Ross

  • 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. 5

    3.13 Network Programming and the Socket API

    The interface an application uses to specify communication is known as an Application Program Interface (API).

    One particular API has emerged as the de facto standard for software that communicates over the Internet. This API is

    known as the socket API, commonly abbreviated sockets.

    The socket API is available for many OS such as Microsoft's Windows systems as well as various UNIX systems,

    including Linux.

    sockets/tenglar

  • 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. 6

    3.15 Parameters and the Socket API

    Socket programming differs from conventional I/O.

    An application must specify details such as :

    the address of a remote computer

    the protocol port number

    whether the application will act as a client or as a server

    To avoid having a single socket function with many parameters, designers of the socket API chose to

    define many functions.

    An application creates a socket, and then invokes functions.

  • 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. 7

    Figure 3.7 A summary of the major functions in the socket API.

  • 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. 8

    3.16 Socket Calls in a Client and Server

    Figure 3.8 illustrates the sequence of socket calls made by a typical client and server that use a stream connection.

    The server waits for the client to send data.

    In practice, some applications arrange for the server to send first (i.e. send and recv are called in the reverse order).

  • 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. 9

    Figure 3.8 Illustration of the sequence of socket functions called by a client and

    server using the stream paradigm.

  • 10

    How does this all work in Java?A brief overview follows...

    8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks

  • Simple, example client-server in Java.

    11

    AdditionClient.java RunAdditionClient.javaAdditionServer.java RunAdditionServer.javaThe source code files are on NET0183 website.

    8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks

    The client sends two numbers to the server.The server computes the addition and sends the result back to the client.Window dialogues are used to report behaviour to let us see what is happening.

  • Socket and ServerSocket classes in Java

    Socket knows about: opening a connection

    sending data

    receiving data

    closing a connection

    ServerSocket knows about: binding to a port

    listening for incoming data

    accepting connections

    8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks12

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks13

    import java.net.*;

    import java.io.*;

    import javax.swing.*;

    public class AdditionServer extends JFrame

    {

    private JTextArea textWindow = new JTextArea();

    private int port;

    // the constructor

    public AdditionServer(int portIn)

    {

    port = portIn;

    setTitle("Addition Server");

    add("Center",textWindow);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(400, 300);

    setVisible(true);

    startServer();

    }

    AdditionServer.java

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks14

    AdditionServer.java

    private void startServer()

    {

    // declare a "general" socket and a server socket

    Socket connection;

    ServerSocket listenSocket;

    // declare low level and high level objects for input

    InputStream inStream;

    DataInputStream inDataStream;

    // declare low level and high level objects for output

    OutputStream outStream;

    DataOutputStream outDataStream;

    // declare other variables

    String client;

    int first, second, sum;

    boolean connected;

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks15

    AdditionServer.javawhile(true){

    try {

    // create a server socket

    listenSocket = new ServerSocket(port);

    textWindow.append("Listening on port " + port + "\n");

    // listen for a connection from the client

    connection = listenSocket.accept ();

    connected = true;

    // create an input stream from the client

    inStream = connection.getInputStream();

    inDataStream = new DataInputStream(inStream);

    // create an output stream to the client

    outStream = connection.getOutputStream ();

    outDataStream = new DataOutputStream (outStream );

    // wait for a string from the client

    client = inDataStream.readUTF();

    textWindow.append("Connection established with "

    + client + "\n" );

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks16

    AdditionServer.java

    while(connected)

    {

    //read an integer from the client

    first = inDataStream.readInt();

    textWindow.append( "First number received: "

    + first + "\n");

    //read an integer from the client

    second = inDataStream.readInt();

    textWindow.append( "Second number received: "

    + second + "\n");

    sum = first + second;

    textWindow.append( "Sum returned: "

    + sum + "\n");

    //send the sum to the client

    outDataStream.writeInt(sum);

    }

    }

    catch (IOException e)

    {

    connected = false;

    }}}}

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks17

    RunAdditionServer.java

    public class RunAdditionServer {

    /**

    * @param args

    */

    public static void main(String[] args) {

    new AdditionServer(8901);

    }

    }

  • 8/25/2009NET0183 Networks and Communications

    by Dr Andy Brooks18

    Simple, example client-server in Java.Extracts from the Java API.

    listenSocket = new ServerSocket(port); Creates a server socket, bound to the specified port. A

    port of 0 creates a socket on any free port. The maximum queue length for incoming connection indications (a request to connect) is set to 50. If a connection indication arrives when the queue is full, the connection is refused.

    connection = listenSocket.accept (); Listens for a connection to be made to this socket and

    accepts it. The method blocks until a connection is made. A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.