problem in java web server
I make a simple java web server.It works well,but now i want to stop it by using stop button.But it don't works correctly.Here is my code...
import java.awt.event.ActionListener; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import javax.swing.*; import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddress; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.File; class HttpServer { /* WEB_ROOT is the directory where our HTML and other files reside. */ public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "content"; // shutdown command public static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // the shutdown command received public boolean shutdown = false; ServerSocket serverSocket; Socket socket; InputStream input; OutputStream output; public void await() { serverSocket = null; int port = 8080; try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException e) { e.printStackTrace(); System.exit(1); } // Loop waiting for a request while (!shutdown) { try { socket = serverSocket.accept(); input = socket.getInputStream(); output = socket.getOutputStream(); // create Request object and parse Request request = new Request(input); request.parse(); // create Response object Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); // Close the socket socket.close(); } catch (Exception e) { e.printStackTrace(); continue; } } } public void closeConnection() { String errorMessage = "Connection terminating"; try { output.write(errorMessage.getBytes()); } catch (IOException e) { e.printStackTrace(); } output=null; input=null; socket=null; } } class button extends JFrame { private JButton start; private JButton stop; public button() { super("Testing buttons"); setLayout(new FlowLayout()); start=new JButton("Start"); add(start); stop=new JButton("Stop"); add(stop); start.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { HttpServer server = new HttpServer(); server.await(); start.setEnabled(false); stop.setEnabled(true); } }); stop.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae1) { HttpServer server = new HttpServer(); server.closeConnection(); stop.setEnabled(false); start.setEnabled(true); } }); } } public class Server { public static void main(String args[]) { button b=new button(); b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b.setSize(300,200); b.setVisible(true); } } Anyone can help me..? Thanks for any answer. |
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
|
Thread closed. Ongoing discussion here: http://www.linuxquestions.org/questi...server-865028/
|
All times are GMT -5. The time now is 07:38 AM. |