LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-02-2004, 05:34 AM   #16
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492

you can call the applet's "stop" method which is made for that.
 
Old 08-03-2004, 02:41 AM   #17
nickhx
Member
 
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
if I don't want to use "stop",is there any method to implement it?
 
Old 08-03-2004, 02:56 AM   #18
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
 
Old 08-03-2004, 03:21 AM   #19
nickhx
Member
 
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
yeah,now I decide not add the "stop"button,just add a label,written"warning:if you want to stop program,just value the port to be null! ".^_^
 
Old 08-03-2004, 03:36 AM   #20
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You can have a stop button setting the port to be null (or zero?), that would be easier to use.
 
Old 08-03-2004, 03:45 AM   #21
nickhx
Member
 
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
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.
 
Old 08-03-2004, 04:11 AM   #22
ljqu_happy
LQ Newbie
 
Registered: Aug 2004
Posts: 18

Rep: Reputation: 0
OK, you can stop running the program by set the port number null.
 
Old 08-03-2004, 04:33 AM   #23
nickhx
Member
 
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
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();
}


// Validate scan parameters
private boolean validParameters() {

try {

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;

}
// Convenience method for writing results
private void out(String msg) {
resultsTextArea.setText(msg);
}
}


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

********************
you could try it^_^
 
Old 08-03-2004, 05:01 AM   #24
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
As I already told you, please use the "code" tag provided by the "Post Reply" Interface to post code samples.
Un-indented code is boring ...
 
Old 08-03-2004, 05:10 AM   #25
nickhx
Member
 
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
oh,I am sorry,I forgot.
next time I'll use "post reply".
 
  


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
middle button doesn't work laurentwahl Linux - Hardware 8 06-01-2005 03:35 PM
How come my CD eject button doesn't work? bad_andy Slackware 17 01-31-2005 03:56 PM
Getting a 7 button mouse to work in linux jax8 LinuxQuestions.org Member Success Stories 5 08-14-2004 11:21 PM
Mouse with a wheel button can't get it to work!! vador Linux - Hardware 12 05-17-2004 08:36 PM
lock button cant work yet ! black Linux - Newbie 3 11-22-2003 04:41 AM

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

All times are GMT -5. The time now is 07:42 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
Open Source Consulting | Domain Registration