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 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.*;
import java.net.*;
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
topPanel.add(portLabel);
topPanel.add(portTextField);
// 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) {
out("check parameters...");
if(validParameters()) {
out("scannering ...");
try {
while(true){
ServerSocket s = new ServerSocket(port, 5);
// No exception: SUCCESS
out("scan:\n");
out(" "+s+"\n");
s.close();
}
}
catch(Exception ex) {
// Could not connect
if(ex instanceof SecurityException) out(ex.getMessage());
else out("Port "+port+" is closed ");
}
}
}
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;
}
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.
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 not.it seens to make the program pause some time,then run,not always run,I am not sure.
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).
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 hou007xz@hotmail.com,tomorrow I'll go traveling for 3 days,maybe we could communicate when I go back.Anyway,thank you very much .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.