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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Well, actually the default Applet stop() method does nothing ...
But if you implement it in your code to stop the working thread, that would stop your applet to consuming CPU power, if this is what concerns you.
You cannot really do more while staying in the same web page, as far as I know, as your applet is responsible of its own area on the browser, and there must be code beyond it.
If you really want to quit the applet, have it redirect your browser to another page ...
well,I have tried,but it doesn't work,maybe because of thread,when I click the stop button,the applet seems doesn't know the port is null.you see, I am not familiar with threads.
well,it is the easist way that I have found.
the following is my final code:
**************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;
public class SimpleScannerApplet extends JApplet {
// Declare the GUI components used by the applet
TextField portTextField = new TextField("25",5);
TextArea resultsTextArea = new TextArea(2,60);
JButton scanButton = new JButton("scan");
// JButton helpButton = new JButton("help");
JLabel helpLabel = new JLabel("warning:set the port to be null,you can stop the program!");
JLabel portLabel = new JLabel("port: ");
JLabel resultsLabel = new JLabel("result: ");
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
public SimpleScannerApplet() {
// Layout the applet's components
Border etched = BorderFactory.createEtchedBorder();
// Add an etched border to the main panel
mainPanel.setBorder(BorderFactory.createTitledBorder(etched, "local port scanner"));
// Host and port fields in top panel
topPanel.add(portLabel);
topPanel.add(portTextField);
topPanel.add(helpLabel);
// Scan button and result field in bottom panel
bottomPanel.add(scanButton);
bottomPanel.add(resultsLabel);
bottomPanel.add(resultsTextArea);
// Add top and bottom panels to main panel
mainPanel.add(topPanel);
mainPanel.add(bottomPanel);
// Set up the button's event handler
scanButton.addActionListener(new Scanner());
// Add the main panel to the applet's content pane
getContentPane().add(mainPanel);
}
// The Scan button's event handler does the actual scanning
private class Scanner implements ActionListener {
// Declare fields used as scan parameters
InetAddress address;
int port;
// Handle the button's action event
public void actionPerformed(ActionEvent ev) {
new Thread(){
public void run(){
out("check parameters ...");
while(validParameters()) {
out("scannering...");
try {
// Try to connect to the host/port
Socket s = new Socket("localhost",port);
// No exception: SUCCESS
out(" "+s+"\n");
s.close();
}catch(Exception ex) {
// Could not connect
if(ex instanceof SecurityException) out(ex.getMessage());
else out("Port "+port+" is closed ");
}
try{
Thread t =Thread.currentThread();
Thread.sleep(1000);
}
catch(InterruptedException e) {System.out.println("thread has wrong"); }
}
}
}.start();
}
String portString = portTextField.getText();
// Convert the portString to an int
port = Integer.decode(portString).intValue();
// Make sure that it's in range
if(port > 65535) throw new ScannerException("Invalid port.");
}catch(Exception e) {
// Handle any validation-related exceptions
out(e.getMessage());
return false;
}
return true;
// Used to identify invalid scan parameters
private class ScannerException extends Exception {
public ScannerException(String msg) {
super(msg);
}
}
}
*********************************
the html code is:
**************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html>
<head><title>Local port scanner</title></head>
<body>
<div style="text-align: center">
<h1>Local port scanner</h1>
<applet name="ScanApplet" code="SimpleScannerApplet.class"
width="600" height="125">
Java not supported.
</applet>
</div>
</body>
</html>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.