KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > systemsunion > LoggingServer > SocketServer2


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package com.systemsunion.LoggingServer;
19
20 import java.net.Socket JavaDoc;
21 import java.net.ServerSocket JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import org.apache.log4j.Category;
25 import org.apache.log4j.PropertyConfigurator;
26 import org.apache.log4j.NDC;
27
28 /**
29    A simple {@link SocketNode} based server.
30
31    <pre>
32      <b>Usage:</b> java org.apache.log4j.net.SocketServer port configFile
33
34      where <em>port</em> is a part number where the server listens and
35      <em>configFile</em> is a configuration file fed to the {@link
36      PropertyConfigurator}.
37    </pre>
38
39
40
41
42
43     @author Ceki G&uuml;lc&uuml;
44
45     @since 0.8.4 */

46
47 public class SocketServer2 {
48
49   static Category cat = Category.getInstance(SocketServer2.class.getName());
50
51   static int port;
52
53   public
54   static
55   void main(String JavaDoc argv[]) {
56     if(argv.length == 2)
57       init(argv[0], argv[1]);
58     else
59       usage("Wrong number of arguments.");
60
61     try {
62       cat.info("Listening on port " + port);
63       ServerSocket JavaDoc serverSocket = new ServerSocket JavaDoc(port);
64       while(true) {
65     cat.info("Waiting to accept a new client.");
66     Socket JavaDoc socket = serverSocket.accept();
67     cat.info("Connected to client at " + socket.getInetAddress());
68     cat.info("Starting new socket node.");
69     new Thread JavaDoc(new SocketNode2(socket)).start();
70       }
71     }
72     catch(Exception JavaDoc e) {
73       e.printStackTrace();
74     }
75   }
76
77
78   static
79   void usage(String JavaDoc msg) {
80     System.err.println(msg);
81     System.err.println(
82       "Usage: java " + SocketServer2.class.getName() + " port configFile");
83     System.exit(1);
84   }
85
86   static
87   void init(String JavaDoc portStr, String JavaDoc configFile) {
88     try {
89       port = Integer.parseInt(portStr);
90     }
91     catch(java.lang.NumberFormatException JavaDoc e) {
92       e.printStackTrace();
93       usage("Could not interpret port number ["+ portStr +"].");
94     }
95     PropertyConfigurator.configure(configFile);
96     NDC.push("Server");
97   }
98 }
99
Popular Tags