Topic: 为什么java会这样的??不明请指教

  Print this page

1.为什么java会这样的??不明请指教 Copy to clipboard
Posted by: blygl
Posted on: 2005-06-28 15:36

请使用准确的文字描述作为标题
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.printlnEnvelope;
}
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.processWindowEventEnvelope;
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_actionPerformedEnvelope;
}
}

2.Re:为什么java会这样的??不明请指教 [Re: blygl] Copy to clipboard
Posted by: why
Posted on: 2005-06-28 19:35

> 第二个就不行
怎樣不行?
用甚麼環境?

> 也行是awt为设计成安全的。
請尊重別人的時間和眼睛健康,提問的文字要無誤清楚可解.

請細讀 [加贴须知], 提高发问技巧.

3.Re:为什么java会这样的??不明请指教 [Re: blygl] Copy to clipboard
Posted by: blygl
Posted on: 2005-06-29 00:26

OH,不好意思,是打错字了。我的意思是讲上面第一个例子可以运行检查到21,80等端口而第二个例子检查不了,会有异常。我用的jBuilderX。

4.Re:为什么java会这样的??不明请指教 [Re: blygl] Copy to clipboard
Posted by: fourfire
Posted on: 2005-06-30 17:53

把异常贴出来呀


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923