Advertisement
mrmgscott

Server Class

Aug 7th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.net.URL;
  8. import java.net.URLDecoder;
  9. import java.util.LinkedList;
  10.  
  11. import javax.swing.JOptionPane;
  12.  
  13.  
  14. public class Server {
  15.    
  16.     ServerSocket serverSocket = null;
  17.     Socket clientSocket = null;
  18.     static int defaultPort= 7341;
  19.     String fromClientStr; //The string from the client
  20.     ObjectOutputStream oos = null; //For SENDING data to client
  21.     ObjectInputStream ois = null; //Getting info FROM client
  22.     private static LinkedList<ServerMultiClient> allClients = new LinkedList<ServerMultiClient>();//Holds ALL client connections
  23.     public static int serverID = 1; //Unique IDs for client from server. Starts are 1. Note: If you a user gets a Server ID LESS than one, something is wrong
  24.     private static boolean graphsReady = false; //This boolean is checked by ServerMultiClient to see if the Graphs are ready, if they are, ServerMultiClient will send the graphs to their Clients
  25.  
  26.  
  27.     public static void main(String[] args) {
  28.        
  29.         //Run server code in a new thread so the GUI will respond
  30.         Runnable runServer = new Runnable(){
  31.             public void run(){
  32.                 startServer();
  33.             }
  34.         };
  35.         Thread startServer = new Thread(runServer);
  36.         startServer.start();
  37.  
  38.     }
  39.    
  40.     private static void startServer(){
  41.        
  42.         ServerSocket serverSocket = null;
  43.         boolean listening = true;
  44.  
  45.         //Set up the connection
  46.         try {
  47.             serverSocket = new ServerSocket(getPort());
  48.         } catch (IOException e) {
  49.             JOptionPane.showMessageDialog(null,
  50.                     "Could not listen on port: " + getPort(),
  51.                     "Error",
  52.                     JOptionPane.ERROR_MESSAGE);
  53.         }
  54.        
  55.         //Start listening for new Clients to connect
  56.         while (listening){
  57.             try {
  58.                 ServerMultiClient newClient = new ServerMultiClient(serverSocket.accept(), serverID++); //Accept a new client
  59.                 newClient.start(); //Start this new client
  60.                 allClients.add(newClient); //Add this new client to the list of all clients
  61.                
  62.             } catch (IOException e) {
  63.                 e.printStackTrace();        
  64.                 Runnable runError = new Runnable(){
  65.                     public void run(){
  66.                         JOptionPane.showMessageDialog(null,
  67.                             "Failed Accepting Client",
  68.                             "Error",
  69.                             JOptionPane.ERROR_MESSAGE);
  70.                     }
  71.                 };
  72.                 Thread thrError = new Thread(runError);
  73.                 thrError.start();
  74.                
  75.             }
  76.         }
  77.  
  78.         try {
  79.             serverSocket.close();
  80.         } catch (IOException e) {
  81.             e.printStackTrace();
  82.             JOptionPane.showMessageDialog(null,
  83.                     "Could not close server socket",
  84.                     "Error",
  85.                     JOptionPane.ERROR_MESSAGE);
  86.         }
  87.        
  88.     }
  89.    
  90.     //Returns the port from the box
  91.     private static int getPort(){
  92.         return defaultPort;
  93.     }
  94.    
  95.     //This method finds and returns the main program path of the main jar file.
  96.     public static String getProgramPath() throws IOException{
  97.         URL url = Server.class.getProtectionDomain().getCodeSource().getLocation(); //Gets the path
  98.         String jarPath = URLDecoder.decode(url.getFile(), "UTF-8"); //Should fix it to be read correctly by the system
  99.         String parentPath = new File(jarPath).getParentFile().getPath(); //Path of the jar
  100.         return parentPath + File.separator;
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement