Guest User

Untitled

a guest
Feb 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package net.lorichon;
  2.  
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.util.concurrent.ConcurrentLinkedQueue;
  6. import java.util.logging.Handler;
  7. import java.util.logging.Level;
  8. import java.util.logging.LogRecord;
  9. import java.util.logging.Logger;
  10.  
  11. import net.lorichon.client.Client;
  12.  
  13. /**
  14.  * The main representation of the server. The server takes in packets and sends out packets
  15.  * to all connected clients as well as setting db information and logging stuff.
  16.  * @author Dan
  17.  *
  18.  */
  19. public final class Server {
  20.     public static final int m_maxReceivableBytes = 1024;
  21.     public ConcurrentLinkedQueue<Client> m_connections = new ConcurrentLinkedQueue<Client>();
  22.     private static final Logger log = Logger.getLogger("LorichonServer");
  23.     private static Server m_server;
  24.     private ServerSocket m_serverSocket;
  25.     private boolean m_keepAlive = true;
  26.     public static void handleServerException(String msg) {
  27.         ExceptionManager.handleException(Error.ERROR_SOCKET_CLOSED, msg);
  28.         try {
  29.             m_server.m_serverSocket.close();
  30.             m_server.m_keepAlive = false;
  31.         } catch (IOException e) {}
  32.     }
  33.    
  34.     public static void main(String[] args) throws IOException {
  35.         log.addHandler(new Handler() {
  36.  
  37.             @Override
  38.             public void close() throws SecurityException {
  39.             }
  40.  
  41.             @Override
  42.             public void flush() {
  43.             }
  44.  
  45.             @Override
  46.             public void publish(LogRecord arg0) {
  47.                 System.out.println(arg0.getMessage());
  48.             }});
  49.         log.setUseParentHandlers(false);
  50.         log.info("Starting up server...");
  51.        
  52.        
  53.         m_server = new Server();
  54.         m_server.m_serverSocket = new ServerSocket(7464);
  55.        
  56.         log.info("Accepting connections on 7464...");
  57.  
  58.        
  59.         // Main logic loop.
  60.         while(m_server.m_keepAlive) {
  61.             try {
  62.                 Client c = new Client(m_server.m_serverSocket.accept());
  63.                 ThreadManager.execute(new Client(m_server.m_serverSocket.accept()));
  64.                 m_server.m_connections.add(c);
  65.             } catch(Exception e) {} // ignore the exception for adding this.
  66.         }
  67.        
  68.         m_server.m_serverSocket.close();
  69.         System.exit(0);
  70.     }
  71.    
  72.     public static void log(Level l, String msg) {
  73.         log.log(l, msg);
  74.     }
  75. }
Add Comment
Please, Sign In to add comment