Transcript PPT

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
• 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
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
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
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
Transport protocols and client-server
interaction
• Clients and servers communicate using a
transport protocol - unaware of underlying
layers
Server class computers
• Often a single server class computer runs
several servers
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
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
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
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
Traders
trader
2. Request
service
1. export
service offer
client
service
3. use
service
Factories
trader
2. request
create
service
factory
1. Request
service
3. create
service
client
service
4. use
service
Other forms of communication
peer
peer
Peer to peer
communication
peer
peer
The Socket Interface

Socket API overview

Socket system calls

Example client and server programs
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)
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
socket
Server
bind
socket
listen
connect
accept
send/recv
recv/send
close
close
Client
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
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
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.
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
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
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
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
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
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
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
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
Client
import 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);
// 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);
}
}
}
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();
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);
} }}