请使用准确的文字描述作为标题
Your next post without a proper Subject will be removed.
我刚学java,按书上的一个检查机子端口的程序来练习。第一个例子可以运行。检查出了21,80等端口。但是第二个就不行了。(第二个例子和第一个一样的。只是第二个例子用了swing界面,第一个没有)请帮忙搞清楚这是为什么。谢了。本人有初步认为这是因为第二个例子中用了awt组件。也行是awt为设计成安全的。是不是不能用来检查端口???附上两个例子代码:
第一个:
package javacode;
import java.net.*;
import java.io.*;
public class FingerClient {
public static void main(String[] args) {
String host = "220.229.102.154";
if (args.length > 0) {
host = args[0];
}
Socket connection = null;
try {
InetAddress theAddress = InetAddress.getByName(host);
for (int i = 1; i < 65536; i++) {
try {
connection = new Socket(host, i);
System.out.println("There is a server on port "
+ i + " of " + host);
}
catch (IOException e) {
// must not be a server on this port
}
} // end for
} // end try
catch (UnknownHostException e) {
System.err.println;
}
finally {
try {
if (connection != null) connection.close();
}
catch (IOException e) {}
}
} // end main
} // end PortScanner
第二个:
package portscanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.Calendar;
public class PortScanner extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField(20);
JButton jButton1 = new JButton();
Socket socket =null;
String str ="220.229.102.154";
//Construct the frame
public PortScanner() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("端口扫描程序");
jLabel1.setText("请输入地址:");
jTextField1.setText("220.229.102.154");
jButton1.setText("扫描");
jButton1.addActionListener(new PortScanner_jButton1_actionAdapter(this));
contentPane.add(jScrollPane1, BorderLayout.WEST);
contentPane.add(jTextArea1, BorderLayout.CENTER);
contentPane.add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(jLabel1, null);
jPanel1.add(jTextField1, null);
jPanel1.add(jButton1, null);
}
public void scannerPort()
{
try
{
InetAddress iAddress = InetAddress.getByName(str);
jTextArea1.setText(iAddress.getHostAddress());
for(int i=1;i<65536;i++)
{
socket =new Socket(str,i);
jTextArea1.append("\n"+i+"prot on this server");
}
}
catch(UnknownHostException e)
{
jTextArea1.append("\n"+e.toString());
}
catch(IOException e)
{
jTextArea1.append("\n"+e.toString());
}
finally
{
try
{
socket.close();
}
catch(IOException e)
{
jTextArea1.append("\n"+e.toString());
}
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent;
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
//str =jTextField1.getText();
scannerPort();
}
}
class PortScanner_jButton1_actionAdapter implements java.awt.event.ActionListener {
PortScanner adaptee;
PortScanner_jButton1_actionAdapter(PortScanner adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed;
}
}