LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 02-25-2011, 03:03 AM   #1
ridoy
LQ Newbie
 
Registered: Feb 2011
Posts: 19

Rep: Reputation: 1
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.
 
Old 02-25-2011, 05:09 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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.
 
Old 02-25-2011, 05:30 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Thread closed. Ongoing discussion here: http://www.linuxquestions.org/questi...server-865028/
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Sun To Open Source Java System Web Server and Web Proxy Technologies LXer Syndicated Linux News 0 08-03-2008 08:50 PM
Need help to choose hardware for Web Server running website based on Linux and Java saharabear Linux - Hardware 1 07-02-2008 08:55 AM
vnc server with java for connecting with web browser fakie_flip Linux - Software 5 04-05-2006 10:18 PM
Sun Java Web Server on Linux libc6.3 ceenu99 Linux - Software 0 08-29-2005 01:33 AM
Chat Server and web/java client Mercury3 Linux - Software 5 05-19-2004 11:39 AM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration