Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MAIN SERVER APP - CAN RUN AS A THREAD (and stop/start etc.)
- import java.net.*;
- import java.io.*;
- public class ServerApp implements Runnable
- {
- volatile boolean stopped = true;
- private ClientTable clientTable;
- private ServerSocket serverSocket = null; //open a new socket.
- //private Socket socket = null;
- public void stopServer()
- {
- //check if server is already stopped
- if(stopped)
- {
- System.out.println("Error: Server is already stopped!");
- }
- else
- {
- try
- {
- //socket.close();
- stopped = true;
- serverSocket.close();
- System.out.println("stopping server");
- }
- catch (IOException e)
- {
- System.err.println("Error: Nonexistent socket. - the programs fucked.");
- }
- }
- }
- public void startServer()
- {
- //check if server is already running
- if (!stopped)
- {
- System.out.println("Error: Server is already running!");
- }
- else
- {
- //stopped is false i.e. server is now running
- stopped = false;
- System.out.println("starting server");
- //each time we start the server we need to create a NEW thread - you can't resume old ones lol
- Thread thread = new Thread(this);
- //then launch the new thread.
- thread.start();
- }
- }
- public ClientTable getClientTable()
- {
- return clientTable;
- }
- public void run()
- {
- stopped=false;
- while(!stopped)
- {
- try
- { //INIT SERVER CODE:
- ClientTable clientTable = new ClientTable(); //sharing by server threads
- //ServerSocket serverSocket = null; //Opening a new socket
- try
- {
- serverSocket = new ServerSocket(Port.number); //trying with a specific port - this is separate to the other socket mentioned below.
- }
- catch(IOException e)
- {
- System.err.println("Couldn't listen on port "+Port.number); //if port fails
- //TODO: exit here!
- }
- //MAIN SERVER CODE:
- try
- {
- //LOOP - Forever, common with servers.
- while(!stopped)
- {
- //still gotta check those interrupts to see if we killed the server.
- try
- {
- //System.out.println("Running.");
- Thread.sleep(1000);
- Socket socket = serverSocket.accept(); //listen to socket & accept new clients.
- BufferedReader fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); //so we can use readLine():
- String clientName = fromClient.readLine(); //get client's name
- System.out.println("New connection! Client Name: "+clientName); //print new client name
- clientTable.add(clientName); //add client to the table
- (new ServerReceiver(clientName, fromClient, clientTable)).start(); //create & start new thread to read from client.
- PrintStream toClient = new PrintStream(socket.getOutputStream()); //create & start new thread to write to client.
- (new ServerSender(clientTable.getQueue(clientName), toClient)).start();
- //work out how to return client table.
- //have a fiddle with the server socket timeouts etc.
- //every now and again we sleep this to check for an interrupt
- }
- catch (InterruptedException ex)
- {
- //kills the thread nicely :-)
- //System.out.println("caught");
- Thread.currentThread().interrupt();
- }
- }
- //System.out.println("Server Stopped.");
- }
- catch(IOException e)
- {
- System.err.println("IO Error " + e.getMessage());
- //TODO: Establish new connection?
- }
- //every now and again the server sleeps to allow for the code to check for interrupts.
- Thread.sleep(1000);
- }
- catch (InterruptedException ex)
- {
- //killing the thread nicely :-)
- Thread.currentThread().interrupt();
- }
- }
- System.out.println("Server Stopped.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement