KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > server > ChatServer


1 package sellwin.server;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 import sellwin.domain.*;
8
9 // SellWin http://sourceforge.net/projects/sellwincrm
10
//Contact support@open-app.com for commercial help with SellWin
11
//This software is provided "AS IS", without a warranty of any kind.
12

13 /**
14  * simple chat server, started from the command line
15  * waits for client request, processes them
16  */

17 public class ChatServer {
18
19     /** global list of online chat users */
20     private static TreeMap onlineUsers = new TreeMap();
21
22     public static final int DEFAULT_PORT = 7900;
23
24     public static void main(String JavaDoc[] args) {
25         int port = DEFAULT_PORT;
26         ServerSocket serverSocket = null;
27         Socket socket = null;
28
29         System.out.println("ChatServer v1.0 starting");
30
31         try {
32             if(args.length > 0)
33                 port = Integer.parseInt(args[0]);
34         } catch(NumberFormatException JavaDoc nfe) {
35             System.err.println("Usage: java ChatServer [port]");
36             System.err.println("Where options include:");
37             System.err.println("\tport the port on which to listen.");
38             System.exit(0);
39         }
40
41         try {
42             serverSocket = new ServerSocket(port);
43             System.out.println("waiting for chat clients");
44
45             ChatReaper reaper = new ChatReaper(onlineUsers);
46             reaper.start();
47
48             while(true) {
49                 socket = serverSocket.accept();
50                 ChatHandler handler = new ChatHandler(socket, onlineUsers);
51                 handler.start();
52             }
53
54         } catch(IOException ioe) {
55             ioe.printStackTrace();
56         } finally {
57             try {
58                 serverSocket.close();
59             } catch(IOException ioe) {
60                 ioe.printStackTrace();
61             }
62         }
63     }
64 }
65
Popular Tags