Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.util.LinkedList;
- import javax.swing.JOptionPane;
- public class Server {
- ServerSocket serverSocket = null;
- Socket clientSocket = null;
- static int defaultPort= 7341;
- String fromClientStr; //The string from the client
- ObjectOutputStream oos = null; //For SENDING data to client
- ObjectInputStream ois = null; //Getting info FROM client
- private static LinkedList<ServerMultiClient> allClients = new LinkedList<ServerMultiClient>();//Holds ALL client connections
- 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
- 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
- public static void main(String[] args) {
- //Run server code in a new thread so the GUI will respond
- Runnable runServer = new Runnable(){
- public void run(){
- startServer();
- }
- };
- Thread startServer = new Thread(runServer);
- startServer.start();
- }
- private static void startServer(){
- ServerSocket serverSocket = null;
- boolean listening = true;
- //Set up the connection
- try {
- serverSocket = new ServerSocket(getPort());
- } catch (IOException e) {
- JOptionPane.showMessageDialog(null,
- "Could not listen on port: " + getPort(),
- "Error",
- JOptionPane.ERROR_MESSAGE);
- }
- //Start listening for new Clients to connect
- while (listening){
- try {
- ServerMultiClient newClient = new ServerMultiClient(serverSocket.accept(), serverID++); //Accept a new client
- newClient.start(); //Start this new client
- allClients.add(newClient); //Add this new client to the list of all clients
- } catch (IOException e) {
- e.printStackTrace();
- Runnable runError = new Runnable(){
- public void run(){
- JOptionPane.showMessageDialog(null,
- "Failed Accepting Client",
- "Error",
- JOptionPane.ERROR_MESSAGE);
- }
- };
- Thread thrError = new Thread(runError);
- thrError.start();
- }
- }
- try {
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null,
- "Could not close server socket",
- "Error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- //Returns the port from the box
- private static int getPort(){
- return defaultPort;
- }
- //This method finds and returns the main program path of the main jar file.
- public static String getProgramPath() throws IOException{
- URL url = Server.class.getProtectionDomain().getCodeSource().getLocation(); //Gets the path
- String jarPath = URLDecoder.decode(url.getFile(), "UTF-8"); //Should fix it to be read correctly by the system
- String parentPath = new File(jarPath).getParentFile().getPath(); //Path of the jar
- return parentPath + File.separator;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement