ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i make a simple java web server.it has 2 buttons.by Start button i want to start the server,it works correctly.but when i press Stop button,it cannot work,it cannot stop the server.Here is my code...
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();
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);
}
}
several things are wrong:
* do not use the UI thread to run the server in.
* stopping the server can only be done on a server object that has been started: you are making a server object when starting and an other when stopping the server
* after a socket.accept, you normally handle the client socket and communication in a new thread, currently only one client can connect at a time.
* closeConnection() is strange, do want to send "Connection terminating" to all open clientsockets
fixes:
* start a new thread when starting the server
* keep the thread object in a member
* stopping the server can be done by interrupting the thread
** add a catch for the InterruptedException in the server loop
Thanks,it now works correctly.Now i want to use this server for multiple clients.so a client,requesting to show the page index.html should know the machine name or IP address of the computer,which runs my server programme.How can i do this in java?
Suggestion: Put code in a Code block or a pastebin -- it will make your post easier to read, & that in turn will help you get more, better answers faster.
You could start by editing your OP above.
Post 4300, ppd=1.85
Last edited by archtoad6; 03-03-2011 at 05:48 AM.
Reason: add post #
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.