CS 696 Emerging Technologies: Java Distributed Computing Spring Semester, 1999 Basic Client-Server |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 28-Jan-99 |
Basic Client-Server Definitions
Client - one that makes a request of another
Server - the receiver of the request
Client-Server in Objects
class foo { bar server = new bar(); public void makeRequest() { server.request(); } } class bar { public String request() { return "The answer is no"; } }
Definitions Continued
Client-Server Programs
Basic Mechanics of Client-Server
There is no Magic!
For a client to connect to a server program:
A Java Server import java.net.*; import java.io.*; import java.util.*; class JustADemoServer { public static void main(String[] args ) throws Exception { ServerSocket acceptor = new ServerSocket( 0 ); System.out.println("Using port " + acceptor.getLocalPort()); while (true) { Socket client = acceptor.accept(); InputStream rawInput = client.getInputStream(); BufferedReader input = new BufferedReader( new InputStreamReader( rawInput) ); OutputStream rawOutput = client.getOutputStream(); PrintWriter output = new PrintWriter( rawOutput ); String inputLine = input.readLine().toLowerCase(); if ( inputLine.startsWith("date") ) { Date now = new Date(); output.println(now.toString()); output.flush(); } input.close(); output.close(); } } }
A Java Client import java.net.*; import java.io.*; import sdsu.util.ProgramProperties; public class GetDateClient { public static void main(String[] args) throws Exception { ProgramProperties commandLine = new ProgramProperties( args); String host = commandLine.getString( "h" ); int port = commandLine.getInt( "p" ); Socket server = new Socket( host, port); InputStream rawInput = server.getInputStream(); BufferedReader input = new BufferedReader( new InputStreamReader( rawInput) ); OutputStream rawOutput = server.getOutputStream(); PrintWriter output = new PrintWriter( rawOutput ); output.println( "Date" ); output.flush(); System.out.println(input.readLine()); output.close(); input.close(); } }
For those without Client-Server Experience
1. Run JustADemoServer
2. From a different machine telnet to the machine running the server and to the port the server is using, then type Date followed by a return
From a UNIX machine this can be done with:
Issues
Concurrency
The JustADemoServer handles only one client at a time
What if:
Concurrent Server
Handles many requests at the same time
Use where the time taken to complete a request cannot be limited.
Advantages:
Protocol
Protocol
Error Checking
The following client will crash the server
import java.net.*; import java.io.*; import sdsu.util.ProgramProperties; public class GetDate { public static void main(String[] args) throws Exception { ProgramProperties commandLine = new ProgramProperties( args); String host = commandLine.getString( "h" ); int port = commandLine.getInt( "p" ); Socket server = new Socket( host, port); InputStream rawInput = server.getInputStream(); OutputStream rawOutput = server.getOutputStream(); rawInput .close(); rawOutput .close(); } }
Logging
How many people use your server?
How will you know if someone is trying to hack into your server?
How will you know what they did?
If something goes wrong in the server how will you know? How will you determine what went wrong?
How will you know if a particular client program has trouble connecting to your server?
Servers need to log client access
Security
Who should have access to your server?
What should individual be limited to doing on your server?
If the traffic between client and server secure?
Who validates a user?
Stateful vs Stateless Servers
Stateless Server: