part 4: network applications

34
Part 4: Network Applications Client-server interaction, example applications

Upload: devin-huff

Post on 31-Dec-2015

30 views

Category:

Documents


0 download

DESCRIPTION

Part 4: Network Applications. Client-server interaction, example applications. Client-Server Interaction. The client-server paradigm, characteristics of clients and servers, dynamic server creation, complex client-server interactions. Application software issues. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Part 4: Network Applications

Part 4: Network Applications

Client-server interaction, example applications

Page 2: Part 4: Network Applications

Client-Server Interaction

The client-server paradigm, characteristics of clients and servers,

dynamic server creation, complex client-server interactions

Page 3: Part 4: Network Applications

Application software issues

• Transport layer software supports the reliable transfer of data between applications

• Application layer software supports:– initiating connections– an application programming interface (API)– encoding data– user friendly naming– definition of specific applications

Page 4: Part 4: Network Applications

The client server paradigm• A widely used form of communication• Server application waits passively for contact

from clients• A server provides a specific service• Client application actively initiates contact

with the server• Information can flow in both directions• Typical situation is many clients interacting

with each server

Page 5: Part 4: Network Applications

Characteristics of clients• General application that becomes a client

when remote access is needed, but that performs other computation locally

• Invoked by the user • Executes for one session• Runs on users local computer• Actively initiates contact with the server• Can access multiple services as needed, but

only one at a time

Page 6: Part 4: Network Applications

Characteristics of servers• Special purpose program to provide one

service• can handle multiple clients at one time• invoked when the system boots• runs through many sessions• runs on a relatively powerful shared computer• waits passively for contact from various kinds

of client, but only provides one service

Page 7: Part 4: Network Applications

Transport protocols and client-server interaction

• Clients and servers communicate using a transport protocol - unaware of underlying layers

Page 8: Part 4: Network Applications

Server class computers

• Often a single server class computer runs several servers

Page 9: Part 4: Network Applications

Identifying a particular service

• Transport protocols assign a unique identifier to each service– server registers its ID with protocol software when

it boots– client specifies the desired ID when it makes a

connection

• TCP uses protocol port numbers

Page 10: Part 4: Network Applications

Concurrency and servers• Concurrent server offers supports multiple clients

at the same time• Uses multiple threads of control• Core part of server accepts new requests and

dynamically creates new servers as separate threads to handle them

• Each thread deals with its client’s requests• N concurrent clients => N + 1 threads• TCP uses a combination of destination port and

source port to identify the right thread

Page 11: Part 4: Network Applications

Complex client-server interactions

• A client is not restricted to accessing a single service

• A client does not have to use a single server for a given service

• A server can itself become a client of another server

• Watch out for circular dependencies

Page 12: Part 4: Network Applications

Distributed programming• Extend client-server paradigm to more general

distributed programming• Provide greater transparency for programmers

– remote procedure calls (RPC)– distributed objects and components

• Provide standard services for locating and manufacturing other services– traders– factories

Page 13: Part 4: Network Applications

Traders

trader

serviceclient

1. export service offer

2. Request service

3. use service

Page 14: Part 4: Network Applications

Factories

trader

serviceclient

1. Request service

4. use service

factory

2. request create service

3. create service

Page 15: Part 4: Network Applications

Other forms of communication

peer

peer

peer

peer

Peer to peercommunication

Page 16: Part 4: Network Applications

The Socket Interface

Socket API overview

Socket system calls

Example client and server programs

Page 17: Part 4: Network Applications

Application Programming Interface

• The API is the interface that the application uses to communicate with transport protocol software - usually a set of functions or classes

• Socket API is the de facto standard for TCP/IP

• Originated on UNIX but now available for other operating systems (e.g., Windows)

Page 18: Part 4: Network Applications

Sockets and descriptors

• Application requests the operating system to create a socket– systems returns descriptor - small integer– application then specifies details such as transport

protocol, protocol addresses, specify if client or server using further functions

– application then uses the descriptor as an argument to functions that “read” and “write” data

– application then closes the socket

Page 19: Part 4: Network Applications

Server Client

socket

bind

listen

accept

recv/send

close

socket

connect

send/recv

close

Page 20: Part 4: Network Applications

Creating a socket

• First create a socket with descriptor = socket(protofamily,type,protocol) – protofamily specifies the protocol family to be

used, e.g., PF_INET or PF_APPLETALK– type specifies the type of communication, e.g.,

SOCK-STREAM or SOCK_DGRAM– protocol specifies a specific protocol

• Function close ( socket ) terminates a socket

Page 21: Part 4: Network Applications

Binding a socket

• bind(socket,localaddr,addrlen)

associates a socket with a protocol port number– socket is a socket descriptor– localaddr is a structure that contains a local address– addrlen specifies the length of the address

Page 22: Part 4: Network Applications

Servers and connections

• Server uses listen(socket,queuesize) to put a socket in passive mode. Queuesize is the length of a request queue.

• Server uses newsock = accept(socket, caddress, caddresslen) to accept a new connection from a client. Creates a new socket for this client and fills in the client address.

Page 23: Part 4: Network Applications

Clients and connections

• A client uses connect (socket, saddress, saddresslen) to connect to server– socket is the local socket for the client– saddress contains the server’s address and

protocol port number– saddresslen is the length of the server’s

address

Page 24: Part 4: Network Applications

Sending data

• send (socket, data, length, flags)– socket is a connected socket– data is a pointer to the data– length is the length of the data– flags enable debugging options

• sendto and sendmsg are used with connectionless communication

Page 25: Part 4: Network Applications

Receiving data• recv(socket, buffer, length, flags) is used to receive data– socket is a connected socket– buffer is a pointer to allocated memory– length is the size of the buffer – flags controls additional details

• recvfrom and recvmsg support connectionless communication

• versions of the standard read and write functions are also available

Page 26: Part 4: Network Applications

Other useful functions

• After accepting, a server can use getpeername to get the complete address of the initiating client

• client or server can use gethostbyname to obtain information (e.g., IP address) about the host on which it is running

• gethostbyaddrr maps an IP address back to a host name

Page 27: Part 4: Network Applications

Sockets, threads and inheritance

• Each new thread initially inherits all existing sockets from its parent

• A thread typically closes sockets that it doesn’t need, removing them from its local list

• The system maintains a reference count for each socket- how many threads are using it - and terminates the socket when this reaches 0

Page 28: Part 4: Network Applications

Example

• Comer’s book (chapter 23 3rd ed.) includes simple examples of a client and a server– server sends a message to the client saying

how many times it has been contacted– server command line argument is a protocol

port number– client command line arguments are a protocol

port number and the name of the host on which the server is running

Page 29: Part 4: Network Applications

Networking in Java

• Package java.net provides the classes for implementing networking applications

• Using the socket classes you can implement client and server applications – ServerSocket – implements server sockets – Socket – implements client sockets – DatagramSocket – for sending and receiving

datagram packets (UDP)– MulticastSocket – for sending and receiving IP

multicast packets

Page 30: Part 4: Network Applications

Java Examples

• From the Java Developers Almanac 1.4 (http://javaalmanac.com/) – Creating a Client Socket– Creating a Server Socket – Reading Text from a Socket– Writing Text to a Socket

• For further info – Java Networking Tutorial http://java.sun.com/docs/books/tutorial/networking

Page 31: Part 4: Network Applications

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

public class Client { public static void main( String[] args) {

Socket sock; InetSocketAddress address; String host = "localhost"; int port = 9101; DataInputStream is; PrintStream ps;

try { // SOCKET sock = new Socket(); address = new InetSocketAddress(host, port);

// CONNECT sock.connect(address);

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

Page 32: Part 4: Network Applications

// SEND ps.println("something or another...");

BufferedReader br = new BufferedReader ( new InputStreamReader( sock.getInputStream() ) );

// RECV String str = br.readLine(); System.out.println(str); // CLOSE sock.close(); } catch ( Exception e ) { System.err.println(e); } }}

Page 33: Part 4: Network Applications

Server

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

public class Server { public static void main( String[] args) {

ServerSocket SSock; Socket conn; InetSocketAddress address; String host = "localhost"; int port = 9101; PrintStream ps;

try { // SOCKET SSock = new ServerSocket(); address = new InetSocketAddress(host, port);

// BIND & LISTEN SSock.bind(address);

Page 34: Part 4: Network Applications

while (true) { // ACCEPT conn = SSock.accept();

BufferedReader br = new BufferedReader ( new InputStreamReader ( conn.getInputStream() ) );

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

String str;

// RECV while ( (str = br.readLine()) != null ) { System.out.println("CLIENT WROTE: " + str); // SEND ps.println("YOU WROTE: " + str); }

// CLOSE conn.close(); } } catch ( Exception e ) { System.err.println(e); } }}