Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-28-2004, 10:34 PM   #1
Registered: Jul 2004
Posts: 35

Rep: Reputation: 15
help!!!how to reduce the system resource needs ?

I wrote a simple java applet program to scan local port all the time,
the following is the code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class SimpleScannerApplet extends JApplet {

// Declare the GUI components used by the applet
JTextField portTextField = new JTextField("8080",5);
JTextArea resultsTextArea = new JTextArea(2,60);
JButton scanButton = new JButton("scane");
JLabel portLabel = new JLabel("port number: ");
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 prot scanner"));
// Host and port fields in top panel
// Scan button and result field in bottom panel
// Add top and bottom panels to main panel
// Set up the button's event handler
scanButton.addActionListener(new Scanner());
// Add the main panel to the applet's content pane

// 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) {
out("check parameters...");
if(validParameters()) {
out("scannering ...");
try {
ServerSocket s = new ServerSocket(port, 5);
// No exception: SUCCESS
out(" "+s+"\n");
catch(Exception ex) {
// Could not connect
if(ex instanceof SecurityException) out(ex.getMessage());
else out("Port "+port+" is closed ");

// 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
return false;
return true;

// Convenience method for writing results
private void out(String msg) {

// Used to identify invalid scan parameters
private class ScannerException extends Exception {

public ScannerException(String msg) {

but when I run the applet,the CPU always works 100%,how to
reduce the resource needs?

Last edited by nickhx; 07-28-2004 at 10:37 PM.
Old 07-28-2004, 11:06 PM   #2
Registered: Apr 2004
Location: berkeley, ca
Distribution: slk10, winxp
Posts: 313

Rep: Reputation: 30
after ServerSocket s = new ServerSocket(port, 5); i don't seem to see where you call: s.accept();
my guess is that will not only solve your problem, but also make your program correct.
Old 07-29-2004, 12:31 AM   #3
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
really?I'll try
Old 07-29-2004, 12:57 AM   #4
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
well,after I add the s.accept();
the program just output"scannering....."
my purpose is just to scan the local port
so I don't need to use s.accept();
"bind","listen" is enough.
I was told that because of the "while" sentence,my CPU run 100%,is there
any way to reduce resource requirement?I was told C could do that ,but he was not sure whether java could or seens to make the program pause some time,then run,not always run,I am not sure.

Last edited by nickhx; 07-29-2004 at 01:03 AM.
Old 07-29-2004, 02:10 AM   #5
Registered: Apr 2004
Location: berkeley, ca
Distribution: slk10, winxp
Posts: 313

Rep: Reputation: 30
to me, "scan" sounds like what a client would do, you know, like hackers scan the internet for servers that are accepting connections. so, if that's what you meant by "scan," then i think you should use Socket instead of ServerSocket. because it's hard for me to see the usefulness of a ServerSocket when it's not used to accept connections.
but then again, since you're working with a local port, you have to have some process accepting connections at that port.
also, i don't quite understand what you meant (my networking knowledge is limited to a quarter of java): ""bind","listen" is enough". isn't it the same as accepting?
basically, clients connect, and servers accept. but you're doing neither (or so it seems).

Last edited by rgiggs; 07-29-2004 at 02:12 AM.
Old 07-29-2004, 03:56 AM   #6
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
well,I just need to listen to my local port,so I don't need server and client,case I just listen to my local port,I don't need to connect to other computers.
ServerSocket s=new ServerSocket(port,5) could bind and listen to the local
port,so this is enough for my job.
s.accepts() Listens for a connection to be made to this socket and accepts it.But since I don't need to connect to others ,this is no use.
this is just what I thought,maybe I am wrong(case I am also lack of networking knowledges of java).why don't you test it and tell me the result?I am also puzzled why I add the s.accept()there is no result,maybe you could show me the reason.
well,my e-mail is,tomorrow I'll go traveling for 3 days,maybe we could communicate when I go back.Anyway,thank you very much .
Old 07-29-2004, 04:07 AM   #7
Registered: Jul 2004
Posts: 35

Original Poster
Rep: Reputation: 15
the pupose for this program is to listen to your local port all the time and show the situation of your local port.


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
system resource ust Linux - Software 1 10-20-2005 02:24 AM
System Resource Comparison? Randall Slack Linux - Software 2 04-08-2005 07:31 AM
want to reduce system boot / init time 964racer Linux - Laptop and Netbook 7 06-09-2004 03:11 AM
Finding system resource info? carlosinfl Fedora 4 05-17-2004 06:02 PM
command for checking system resource usage? yeahoo7 Linux - Networking 3 06-11-2003 11:31 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 AM.

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