SDSU CS 580 Client-Server Programming
Fall Semester, 2000
Java Server Intro
Previous    Lecture Notes Index    Next    
© 2000, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 04-Sep-00

Contents of Doc 5, Java Server Intro


Dr. Vinge's CS580 class notes, Spring 2000,

Parts of this document are from Dr. Vinge's lecture notes, which in turn based on earlier CS 580 classes taught by Andrew Scherpbier & Roger Whitney & Socket. See or


Doc 5, Java Server Intro Slide # 2

What is a Server?


Any program that waits for incoming communication requests from a client
Extracts requested information from data and return to client

Basic algorithm:

   while (true) {
     Wait for an incoming request;
     Perform whatever actions are requested;

Doc 5, Java Server Intro Slide # 3
Some Basic Server Issues

Doc 5, Java Server Intro Slide # 4
Java TCP Sockets

ServerSocket basic methods

public ServerSocket(int port) //port = 0 gives random port

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

Doc 5, Java Server Intro Slide # 5

A Simple Server

import java.util.Date;
class SimpleDateServer {

   public static void main(String[] args) throws IOException {
      ServerSocket acceptor = new ServerSocket(0);
      System.out.println("On port " + acceptor.getLocalPort());
      while (true) {
         Socket client = acceptor.accept();

(Why "new ServerSocket(0)"?)

Doc 5, Java Server Intro Slide # 6
Processing Client Request

   static void processRequest(InputStream in,OutputStream out)
      throws IOException {
      BufferedReader parsedInput = 
            new BufferedReader(new InputStreamReader(in));
      // the "true" is to get autoflushing:
      PrintWriter parsedOutput = new PrintWriter(out,true);
      String inputLine = parsedInput.readLine();
      if (inputLine.startsWith("date")) {
         Date now = new Date();

Note: This server is just a first example. It needs a lot of work. We will be working on improving it in later lectures.

Doc 5, Java Server Intro Slide # 7
Running the Server

Sample run of SimpleDateServer.
(I typed everything appearing in bold font here.)

rohan 16-> java SimpleDateServer &
[1] 16269
On port 62047

rohan 17-> telnet rohan 62047
Connected to
Escape character is '^]'.
date today
Mon Sep 04 13:37:30 PDT 2000
Connection closed by foreign host.

rohan 18-> telnet rohan 62047
Connected to
Escape character is '^]'.
Connection closed by foreign host.

In this class, shut things down:

rohan 19-> fg
java SimpleDateServer

Doc 5, Java Server Intro Slide # 8
Simple Server Issues

Automate testing of method processRequest()
Test main by hand (for now)

Using our SimpleDateServer

      Client A builds connection to server, 
      Client A goes to lunch 
      Client B builds connection to server and ... :-( 

Multiple connections need to be accepted concurrently.

More on this in later lectures.

Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 04-Sep-00    Next