做了一个端口扫描程序,可是每次执行到connection=new Socket(host,i);这句时,程序就会停在这里不再执行。请问如何解决,问题出在哪里?程序清单如下:
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;public class PortScanner extends JFrame{
private JLabel address=new JLabel("ip address");

private JTextField ipField=new JTextField("172.18.80.37");
private JTextArea res;

public PortScanner(){
super("PortScanner");
Container container=this.getContentPane();
container.setLayout(new BorderLayout());

JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,2));

ipField.addActionListener(new scan());
panel.add(address);
panel.add(ipField);

container.add(panel,BorderLayout.NORTH);

res=new JTextArea();
res.setEditable(false);

JScrollPane scr=new JScrollPane(res);

container.add(scr,BorderLayout.CENTER);


this.setSize(400,500);
this.setVisible(true);

}

class scan implements ActionListener{
public void actionPerformed(ActionEvent evt){
String host=ipField.getText();
Socket connection=null;



try{
InetAddress theAddress=InetAddress.getByName(host);
for(int i=5000;i<65536;i++){
res.append("start");
try{
   
   connection=new Socket(host,i);
   }
catch(UnknownHostException ex){
res.append("no the host"+i);

}
   res.append("end");
   res.append("this is a server on port "+i+" of "+host);
if(connection!=null) connection.close();
if(connection==null) res.append("NO "+i+"of "+host);
}

}
catch(IOException ex){
;
}
}


public static void main(String[] args){
PortScanner port=new PortScanner();
}
}

解决方案 »

  1.   

    没有人帮你测试你怎么连接啊
    shocket连接是基于服务器和客户端的,你只写了客户端但是没有写服务器啊,所以假如你检测的主机的5000端口(也就是你检测的第一个端口)没有在等待的话,你的程序就老是在等待连接啊,当等到一定时间的时候,发现连接不上就退回。
      

  2.   

    要不你可以先测试你自己的啊,去写一个服务器shocket,然后accpet端口,如果能连接就表示你的客户端程序没问题咯。