LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2011, 12:52 PM   #1
ridoy
LQ Newbie
 
Registered: Feb 2011
Posts: 19

Rep: Reputation: 1
Unhappy problem in java web server


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...


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-27-2011, 04:09 AM   #2
thermite_1033
Member
 
Registered: May 2004
Location: Antwerp, Belgium
Distribution: slackware
Posts: 112

Rep: Reputation: 18
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
 
1 members found this post helpful.
Old 03-01-2011, 09:02 AM   #3
ridoy
LQ Newbie
 
Registered: Feb 2011
Posts: 19

Original Poster
Rep: Reputation: 1
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?
 
Old 03-02-2011, 06:46 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
ridoy,

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 #
 
  


Reply



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
problem in java web server ridoy Linux - Newbie 2 02-25-2011 04:30 PM
LXer: Sun To Open Source Java System Web Server and Web Proxy Technologies LXer Syndicated Linux News 0 08-03-2008 07:50 PM
vnc server with java for connecting with web browser fakie_flip Linux - Software 5 04-05-2006 09:18 PM
Sun Java Web Server on Linux libc6.3 ceenu99 Linux - Software 0 08-29-2005 12:33 AM
Chat Server and web/java client Mercury3 Linux - Software 5 05-19-2004 10:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:03 PM.

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
Open Source Consulting | Domain Registration