Home | History | Annotate | Download | only in chatserver
      1 A Simple Chat Server Example
      2 
      3 INTRODUCTION
      4 ============
      5 This directory contains a very simple chat server, the server takes input from a
      6 socket ("user") and sends it to all other connected sockets ("users") along with
      7 the provided name the user was asked for when first connecting.
      8 
      9 The server was written to demonstrate the asynchronous I/O API in JDK 7. 
     10 The sample assumes the reader has some familiarity with the subject matter.
     11 
     12 SETUP
     13 =====
     14 
     15 The server must be built with version 7 (or later) of the JDK.
     16 The server is built with:
     17 
     18     % mkdir build
     19     % javac -source 7 -target 7 -d build *.java
     20 
     21 EXECUTION
     22 =========
     23 
     24     % java -classpath build ChatServer [-port <port number>]
     25 
     26     Usage:  ChatServer [options]
     27         options:
     28             -port port      port number
     29                 default: 5000
     30 
     31 CLIENT EXECUTION
     32 ================
     33 
     34 No client binary is included in the sample.
     35 Connections can be made using for example the telnet command or any program
     36 that supports a raw TCP connection to a port.
     37 
     38 SOURCE CODE OVERVIEW
     39 ====================
     40 ChatServer is the main class, it handles the startup and handles incoming
     41 connections on the listening sockets. It keeps a list of connected client
     42 and provides methods for sending a message to them.
     43 
     44 Client represents a connected user, it provides methods for reading/writing
     45 from/to the underlying socket. It also contains a buffer of input read from
     46 the user.
     47 
     48 DataReader provides the interface of the two states a user can
     49 be in. Waiting for a name (and not receiving any messages while doing so, implemented
     50 by NameReader) and waiting for messages from the user (implemented by MessageReader).
     51 
     52 ClientReader contains the "main loop" for a connected client. 
     53 
     54 NameReader is the initial state for a new client, it sends the user a string and
     55 waits for a response before changing the state to MessageReader.
     56 
     57 MessageReader is the main state for a client, it checks for new messages to send to
     58 other clients and reads messages from the client.
     59 
     60 FINALLY
     61 =======
     62 This is a sample: it is not production quality and isn't optimized for performance.
     63