1 16 17 18 package com.systemsunion.LoggingServer; 19 20 import java.net.Socket ; 21 import java.net.ServerSocket ; 22 import java.io.IOException ; 23 24 import org.apache.log4j.Category; 25 import org.apache.log4j.PropertyConfigurator; 26 import org.apache.log4j.NDC; 27 28 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 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 serverSocket = new ServerSocket (port); 64 while(true) { 65 cat.info("Waiting to accept a new client."); 66 Socket socket = serverSocket.accept(); 67 cat.info("Connected to client at " + socket.getInetAddress()); 68 cat.info("Starting new socket node."); 69 new Thread (new SocketNode2(socket)).start(); 70 } 71 } 72 catch(Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 77 78 static 79 void usage(String 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 portStr, String configFile) { 88 try { 89 port = Integer.parseInt(portStr); 90 } 91 catch(java.lang.NumberFormatException 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 |