Client-Server Programming
Spring Semester, 2005 Server Intro |
||
---|---|---|
© 2005, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 10-Feb-05 |
CS 580 Client-Server Programming Spring Semester, 2005 Doc 6 Server Intro
Copyright ©, All rights reserved. 2005 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
CS 580 Spring 05 | Doc 6, Server Intro Slide # 2 |
Java Network Programming, Harold
VisualWorks Internet Client Developer's Guide
java.net.ServerSocket & Socket. See http://java.sun.com/j2se/1.4.2/docs/api/
Java Network Programming, Harold
Basic Network Concepts, Chapter 2
Java I/O Chapter 4
Sockets for Servers Chapter 10
CS 580 Spring 05 | Doc 6, Server Intro Slide # 3 |
Server
Basic algorithm:
while (true) { Wait for an incoming request; Perform whatever actions are requested; }
CS 580 Spring 05 | Doc 6, Server Intro Slide # 4 |
| server | server := SocketAccessor newTCPserverAtPort: 9009. server listenFor: 5. [ | acceptedSocket | "wait for a new connection" acceptedSocket := server accept. "fork off processing of the new stream socket" [ | stream char | stream := acceptedSocket readAppendStream. stream lineEndTransparent. [ (char := stream next) isNil ] whileFalse: [ stream nextPut: char; commit ]. stream close. ] forkAt: Processor userSchedulingPriority -1. ] repeat.
CS 580 Spring 05 | Doc 6, Server Intro Slide # 5 |
CS 580 Spring 05 | Doc 6, Server Intro Slide # 6 |
Both Java & Smalltalk provide access to socket data via
Stream access is easier
Buffer access can be faster
We will cover Stream access first
CS 580 Spring 05 | Doc 6, Server Intro Slide # 7 |
ServerSocket
Used by servers to listen for clients
Socket
Used by clients to talk to servers Used by servers to talk to clients
ServerSocket basic methods
public ServerSocket(int port) //port = 0 gives random port public ServerSocket(int port, int backlog) public ServerSocket(int port, int backlog, InetAddress bindAddress) public Socket accept() throws IOException public void close() throws IOException public int getLocalPort()
Socket basic methods
public InputStream getInputStream() throws IOException public OutputStream getOutputStream() throws IOException
CS 580 Spring 05 | Doc 6, Server Intro Slide # 8 |
public class DateServer { private static Logger log = Logger.getLogger("dateLogger"); public static void main (String args[]) throws IOException { ProgramProperties flags = new ProgramProperties( args); int port = flags.getInt( "port" , 8765); new DateServer().run(port); } public void run(int port) throws IOException { ServerSocket input = new ServerSocket( port ); log.info("Server running on port " + input.getLocalPort()); while (true) { Socket client = input.accept(); log.info("Request from " + client.getInetAddress()); processRequest( client.getInputStream(), client.getOutputStream()); client.close(); } }
CS 580 Spring 05 | Doc 6, Server Intro Slide # 9 |
void processRequest(InputStream in,OutputStream out) throws IOException { BufferedReader parsedInput = new BufferedReader(new InputStreamReader(in)); boolean autoflushOn = true; PrintWriter parsedOutput = new PrintWriter(out,autoflushOn); String inputLine = parsedInput.readLine(); if (inputLine.startsWith("date")) { Date now = new Date(); parsedOutput.println(now.toString()); } } }
Note: This server is just a first example. It needs a lot of work. We will be working on improving it in later lectures.
CS 580 Spring 05 | Doc 6, Server Intro Slide # 10 |
Sample run of DateServer.
(I typed everything appearing in bold font here.)
rohan 16-> java -jar DateServer.jar Feb 19, 2004 10:56:59 AM DateServer run INFO: Server running on port 8765 |
|
||
|
Al 13-> telnet rohan.sdsu.edu 8765 Trying 130.191.3.100... Connected to rohan.sdsu.edu. Escape character is '^]'. |
||
Feb 19, 2004 10:57:34 AM DateServer run INFO: Request from /68.7.92.191 |
|
||
|
date Thu Feb 19 10:57:39 PST 2004 Connection closed by foreign host. |
CS 580 Spring 05 | Doc 6, Server Intro Slide # 11 |
Using telnet to interact with a server is
Don’t design server assuming it interacts directly with a human!
CS 580 Spring 05 | Doc 6, Server Intro Slide # 12 |
Using our SimpleDateServer
Client A builds connection to server, Client A goes to lunch Client B builds connection to server and ... :-(
Solution:
Multiple connections need to be accepted concurrently.
CS 580 Spring 05 | Doc 6, Server Intro Slide # 13 |
TCP accepts connections before the server is ready
TCP keeps a backlog queue of connections server has not accepted
Java ServerSocket constructor
There is no reasonable way to find out:
CS 580 Spring 05 | Doc 6, Server Intro Slide # 14 |
Start the SimpleDateServer
Connect to the server using telnet
While the server is waiting for you to type something
Connect to the server with a second telnet session
In the second session type “date” and return
What happens?
Now type “date” and return in the first session
What happens?
CS 580 Spring 05 | Doc 6, Server Intro Slide # 15 |
Some machines have two or more physical network interface
Each network interface has its own IP address
public ServerSocket(int port)
public ServerSocket(int port, int backlog, InetAddress bindAddress)
CS 580 Spring 05 | Doc 6, Server Intro Slide # 16 |
Closing TCP connections can remain for several minutes
TCP may block use of the port until the connection is gone
This can be annoying in development
ServerSocket method setReuseAddress(boolean on)
CS 580 Spring 05 | Doc 6, Server Intro Slide # 17 |
Platform |
End of Line Convention |
Unix |
Line Feed (LF) |
Macintosh OS 9 |
Carriage return (CR) |
Windows |
CR-LF |
LF is ASCII character 10
CR is ASCII character 13
A server should not make assumptions about a client’s platform
A client should not make assumptions about a server’s platform
Client-server protocol should specify which characters are used
CS 580 Spring 05 | Doc 6, Server Intro Slide # 18 |
Java and Smalltalk programs run on all major platforms
Smalltalk assumes files use platform’s end of line convention
Smalltalk input streams convert platforms end of line to CR
Smalltalk output streams convert CR to platform’s end of line
This makes writing cross platform programs easier
Don’t want this to happen socket streams
BufferedExternalStream>> lineEndTransparent
How does Java handle this?
CS 580 Spring 05 | Doc 6, Server Intro Slide # 19 |
On a stream connected to a socket
End of file indicates that the connection has been closed!
Don’t use end of file to determine when other end is done talking!
CS 580 Spring 05 | Doc 6, Server Intro Slide # 20 |
How do we know when we are at the end of a message?
BufferedReader parsedInput = new BufferedReader(new InputStreamReader(in));
char[] message = new char[500]; int sizeRead = parsedInput.read(message, 0, 500);
If
We still may not have the entire message!
Why?
CS 580 Spring 05 | Doc 6, Server Intro Slide # 21 |
A good client-server protocol specifies
Main methods used:
CS 580 Spring 05 | Doc 6, Server Intro Slide # 22 |
Java & Smalltalk streams are buffered
TCP buffers output before sending
A server cannot read bytes left in a client’s buffer
PrintWriter flush();
Copyright ©, All rights reserved.
2005 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.