Results 1 to 2 of 2
  1. #1
    beginner123 is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default I want to delete a file

    I found a tutorial online about creating a chat application. They have two files to run the server - one that runs in command and the other a GUI.
    Both files work but I only want to use the command file but if I delete the GUI file it causes errors in the other file.
    Could someone please help me change the command file so it can run without the GUI file?

    Server.java file
    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    
    public class Server {
    // a unique ID for each connection
    	private static int uniqueId;
    	// an ArrayList to keep the list of the Client
    	private ArrayList<ClientThread> al;
    	// if I am in a GUI
    	private ServerGUI sg;
    	// to display time
    	private SimpleDateFormat sdf;
    	// the port number to listen for connection
    	private int port;
    	// the boolean that will be turned of to stop the server
    	private boolean keepGoing;
    	
    
    	/*
    	 *  server constructor that receive the port to listen to for connection as parameter
    	 *  in console
    	 */
    	public Server(int port) {
    		this(port, null);
    	}
    	
    	public Server(int port, ServerGUI sg) {
    		// GUI or not
    		this.sg = sg;
    		// the port
    		this.port = port;
    		// to display hh:mm:ss
    		sdf = new SimpleDateFormat("HH:mm:ss");
    		// ArrayList for the Client list
    		al = new ArrayList<ClientThread>();
    	}
    	
    	public void start() {
    		keepGoing = true;
    		/* create socket server and wait for connection requests */
    		try 
    		{
    			// the socket used by the server
    			ServerSocket serverSocket = new ServerSocket(port);
    
    			// infinite loop to wait for connections
    			while(keepGoing) 
    			{
    				// format message saying we are waiting
    				display("Server waiting for Clients on port " + port + ".");
    				
    				Socket socket = serverSocket.accept();  	// accept connection
    				// if I was asked to stop
    				if(!keepGoing)
    					break;
    				ClientThread t = new ClientThread(socket);  // make a thread of it
    				al.add(t);									// save it in the ArrayList
    				t.start();
    			}
    			// I was asked to stop
    			try {
    				serverSocket.close();
    				for(int i = 0; i < al.size(); ++i) {
    					ClientThread tc = al.get(i);
    					try {
    					tc.sInput.close();
    					tc.sOutput.close();
    					tc.socket.close();
    					}
    					catch(IOException ioE) {
    						// not much I can do
    					}
    				}
    			}
    			catch(Exception e) {
    				display("Exception closing the server and clients: " + e);
    			}
    		}
    		// something went bad
    		catch (IOException e) {
                String msg = sdf.format(new Date()) + " Exception on new ServerSocket: " + e + "\n";
    			display(msg);
    		}
    	}		
        /*
         * For the GUI to stop the server
         */
    	protected void stop() {
    		keepGoing = false;
    		// connect to myself as Client to exit statement 
    		// Socket socket = serverSocket.accept();
    		try {
    			new Socket("localhost", port);
    		}
    		catch(Exception e) {
    			// nothing I can really do
    		}
    	}
    	/*
    	 * Display an event (not a message) to the console or the GUI
    	 */
    	private void display(String msg) {
    		String time = sdf.format(new Date()) + " " + msg;
    		if(sg == null)
    			System.out.println(time);
    		else
    			sg.appendEvent(time + "\n");
    	}
    	/*
    	 *  to broadcast a message to all Clients
    	 */
    	private synchronized void broadcast(String message) {
    		// add HH:mm:ss and \n to the message
    		String time = sdf.format(new Date());
    		String messageLf = time + " " + message + "\n";
    		// display message on console or GUI
    		if(sg == null)
    			System.out.print(messageLf);
    		else
    			sg.appendRoom(messageLf);     // append in the room window
    		
    		// we loop in reverse order in case we would have to remove a Client
    		// because it has disconnected
    		for(int i = al.size(); --i >= 0;) {
    			ClientThread ct = al.get(i);
    			// try to write to the Client if it fails remove it from the list
    			if(!ct.writeMsg(messageLf)) {
    				al.remove(i);
    				display("Disconnected Client " + ct.username + " removed from list.");
    			}
    		}
    	}
    
    	// for a client who logoff using the LOGOUT message
    	synchronized void remove(int id) {
    		// scan the array list until we found the Id
    		for(int i = 0; i < al.size(); ++i) {
    			ClientThread ct = al.get(i);
    			// found it
    			if(ct.id == id) {
    				al.remove(i);
    				return;
    			}
    		}
    	}
    	
    	
    	public static void main(String[] args) {
    		// start server on port 1500 unless a PortNumber is specified 
    		int portNumber = 1500;
    		switch(args.length) {
    			case 1:
    				try {
    					portNumber = Integer.parseInt(args[0]);
    				}
    				catch(Exception e) {
    					System.out.println("Invalid port number.");
    					System.out.println("Usage is: > java Server [portNumber]");
    					return;
    				}
    			case 0:
    				break;
    			default:
    				System.out.println("Usage is: > java Server [portNumber]");
    				return;
    				
    		}
    		// create a server object and start it
    		Server server = new Server(portNumber);
    		server.start();
    	}
    
    	/** One instance of this thread will run for each client */
    	class ClientThread extends Thread {
    		// the socket where to listen/talk
    		Socket socket;
    		ObjectInputStream sInput;
    		ObjectOutputStream sOutput;
    		// my unique id (easier for deconnection)
    		int id;
    		// the Username of the Client
    		String username;
    		// the only type of message a will receive
    		ChatMessage cm;
    		// the date I connect
    		String date;
    
    		// Constructore
    		ClientThread(Socket socket) {
    			// a unique id
    			id = ++uniqueId;
    			this.socket = socket;
    			/* Creating both Data Stream */
    			System.out.println("Thread trying to create Object Input/Output Streams");
    			try
    			{
    				// create output first
    				sOutput = new ObjectOutputStream(socket.getOutputStream());
    				sInput  = new ObjectInputStream(socket.getInputStream());
    				// read the username
    				username = (String) sInput.readObject();
    				display(username + " just connected.");
    			}
    			catch (IOException e) {
    				display("Exception creating new Input/output Streams: " + e);
    				return;
    			}
    			// have to catch ClassNotFoundException
    			// but I read a String, I am sure it will work
    			catch (ClassNotFoundException e) {
    			}
                date = new Date().toString() + "\n";
    		}
    
    		// what will run forever
    		public void run() {
    			// to loop until LOGOUT
    			boolean keepGoing = true;
    			while(keepGoing) {
    				// read a String (which is an object)
    				try {
    					cm = (ChatMessage) sInput.readObject();
    				}
    				catch (IOException e) {
    					display(username + " Exception reading Streams: " + e);
    					break;				
    				}
    				catch(ClassNotFoundException e2) {
    					break;
    				}
    				// the messaage part of the ChatMessage
    				String message = cm.getMessage();
    
    				// Switch on the type of message receive
    				switch(cm.getType()) {
    
    				case ChatMessage.MESSAGE:
    					broadcast(username + ": " + message);
    					break;
    				case ChatMessage.LOGOUT:
    					display(username + " disconnected with a LOGOUT message.");
    					keepGoing = false;
    					break;
    				case ChatMessage.WHOISIN:
    					writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n");
    					// scan al the users connected
    					for(int i = 0; i < al.size(); ++i) {
    						ClientThread ct = al.get(i);
    						writeMsg((i+1) + ") " + ct.username + " since " + ct.date);
    					}
    					break;
    				}
    			}
    			// remove myself from the arrayList containing the list of the
    			// connected Clients
    			remove(id);
    			close();
    		}
    		
    		// try to close everything
    		private void close() {
    			// try to close the connection
    			try {
    				if(sOutput != null) sOutput.close();
    			}
    			catch(Exception e) {}
    			try {
    				if(sInput != null) sInput.close();
    			}
    			catch(Exception e) {};
    			try {
    				if(socket != null) socket.close();
    			}
    			catch (Exception e) {}
    		}
    
    		/*
    		 * Write a String to the Client output stream
    		 */
    		private boolean writeMsg(String msg) {
    			// if Client is still connected send the message to it
    			if(!socket.isConnected()) {
    				close();
    				return false;
    			}
    			// write the message to the stream
    			try {
    				sOutput.writeObject(msg);
    			}
    			// if an error occurs, do not abort just inform the user
    			catch(IOException e) {
    				display("Error sending message to " + username);
    				display(e.toString());
    			}
    			return true;
    		}
    	}
    }
    ServerGUI.java file
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /*
     * The server as a GUI
     */
    public class ServerGUI extends JFrame implements ActionListener, WindowListener {
    	
    	private static final long serialVersionUID = 1L;
    	// the stop and start buttons
    	private JButton stopStart;
    	// JTextArea for the chat room and the events
    	private JTextArea chat, event;
    	// The port number
    	private JTextField tPortNumber;
    	// my server
    	private Server server;
    	
    	
    	// server constructor that receive the port to listen to for connection as parameter
    	ServerGUI(int port) {
    		super("Chat Server");
    		server = null;
    		// in the NorthPanel the PortNumber the Start and Stop buttons
    		JPanel north = new JPanel();
    		north.add(new JLabel("Port number: "));
    		tPortNumber = new JTextField("  " + port);
    		north.add(tPortNumber);
    		// to stop or start the server, we start with "Start"
    		stopStart = new JButton("Start");
    		stopStart.addActionListener(this);
    		north.add(stopStart);
    		add(north, BorderLayout.NORTH);
    		
    		// the event and chat room
    		JPanel center = new JPanel(new GridLayout(2,1));
    		chat = new JTextArea(80,80);
    		chat.setEditable(false);
    		appendRoom("Chat room.\n");
    		center.add(new JScrollPane(chat));
    		event = new JTextArea(80,80);
    		event.setEditable(false);
    		appendEvent("Events log.\n");
    		center.add(new JScrollPane(event));	
    		add(center);
    		
    		// need to be informed when the user click the close button on the frame
    		addWindowListener(this);
    		setSize(400, 600);
    		setVisible(true);
    	}		
    
    	// append message to the two JTextArea
    	// position at the end
    	void appendRoom(String str) {
    		chat.append(str);
    		chat.setCaretPosition(chat.getText().length() - 1);
    	}
    	void appendEvent(String str) {
    		event.append(str);
    		event.setCaretPosition(chat.getText().length() - 1);
    		
    	}
    	
    	// start or stop where clicked
    	public void actionPerformed(ActionEvent e) {
    		// if running we have to stop
    		if(server != null) {
    			server.stop();
    			server = null;
    			tPortNumber.setEditable(true);
    			stopStart.setText("Start");
    			return;
    		}
          	// OK start the server	
    		int port;
    		try {
    			port = Integer.parseInt(tPortNumber.getText().trim());
    		}
    		catch(Exception er) {
    			appendEvent("Invalid port number");
    			return;
    		}
    		// ceate a new Server
    		server = new Server(port, this);
    		// and start it as a thread
    		new ServerRunning().start();
    		stopStart.setText("Stop");
    		tPortNumber.setEditable(false);
    	}
    	
    	// entry point to start the Server
    	public static void main(String[] arg) {
    		// start server default port 1500
    		new ServerGUI(1500);
    	}
    
    	/*
    	 * If the user click the X button to close the application
    	 * I need to close the connection with the server to free the port
    	 */
    	public void windowClosing(WindowEvent e) {
    		// if my Server exist
    		if(server != null) {
    			try {
    				server.stop();			// ask the server to close the conection
    			}
    			catch(Exception eClose) {
    			}
    			server = null;
    		}
    		// dispose the frame
    		dispose();
    		System.exit(0);
    	}
    	// I can ignore the other WindowListener method
    	public void windowClosed(WindowEvent e) {}
    	public void windowOpened(WindowEvent e) {}
    	public void windowIconified(WindowEvent e) {}
    	public void windowDeiconified(WindowEvent e) {}
    	public void windowActivated(WindowEvent e) {}
    	public void windowDeactivated(WindowEvent e) {}
    
    	/*
    	 * A thread to run the Server
    	 */
    	class ServerRunning extends Thread {
    		public void run() {
    			server.start();         // should execute until if fails
    			// the server failed
    			stopStart.setText("Start");
    			tPortNumber.setEditable(true);
    			appendEvent("Server crashed\n");
    			server = null;
    		}
    	}
    
    }
    In the tutorial he said the GUI was optional so I just want to delete any code that has to do with the GUI file.
    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: I want to delete a file

    Remove the ServerGUI attribute from the Server class.
    Compile.
    Resolve compilation errors (or simply look for uses of the 'sg' variable).

    If you have an IDE this should be a doddle.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. cannot delete file.
    By urbanleg in forum New To Java
    Replies: 9
    Last Post: 08-30-2011, 09:05 PM
  2. Replies: 2
    Last Post: 05-26-2011, 05:38 AM
  3. Delete A File
    By nitinverma in forum CLDC and MIDP
    Replies: 13
    Last Post: 06-15-2010, 08:12 AM
  4. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 10:17 AM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •