ping 没有指定端口这个选项吧,他是网络层的,端口可是传输层的概念啊。

解决方案 »

  1.   

    你用socket去和其它机器连接,使用for循环,扫描目标机器的端口,一般机器的端口1024为系统使用,你就扫描它们。
    如果连接成功说明该端口是打开的,否则是关闭的。
    但是这种作法是不好的,就好像明目张胆的到别人家里去打劫,十分不隐蔽。具体程序是在书上,我可没有时间打给你,你听了我的思路,自己写一下,很短的程序。
      

  2.   

    jbuilder里面的sample里面有telnet的例子你可以看看
    windows里面就是用telnet ip port 格式的
      

  3.   

    //------------------------------------------------------------------------------
    // Copyright (c) 2000 /n-software inc.  All Rights Reserved.
    //------------------------------------------------------------------------------import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import ipworks.*;public class TelnetDemo extends JFrame {
      JPanel contentPane;
      Telnet telnet1 = new Telnet();
      JScrollPane jScrollPane1 = new JScrollPane();
      JTextArea terminalTextArea = new JTextArea();
      TitledBorder titledBorder1;
      TitledBorder titledBorder2;
      Border border1;
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      JTextField hostTextField = new JTextField();
      JButton connectDisconnectButton = new JButton();
      JLabel hostLabel = new JLabel();  /**Construct the frame*/
      public TelnetDemo() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(new Color(194, 255, 255),new Color(95, 134, 128)),"");
        titledBorder2 = new TitledBorder("");
        border1 = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(new Color(194, 255, 255),new Color(95, 134, 128)),BorderFactory.createEmptyBorder(0,12,0,12));
        contentPane.setLayout(gridBagLayout1);
        this.setSize(new Dimension(536, 323));
        this.setTitle("Frame Title");
        telnet1.addTelnetEventListener(new ipworks.TelnetEventListener() {
          public void command(TelnetCommandEvent e) {
          }
          public void connected(TelnetConnectedEvent e) {
            telnet1_connected(e);
          }
          public void dataIn(TelnetDataInEvent e) {
            telnet1_dataIn(e);
          }
          public void disconnected(TelnetDisconnectedEvent e) {
            telnet1_disconnected(e);
          }
          public void doDo(TelnetDoDoEvent e) {
            telnet1_doDo(e);
          }
          public void dont(TelnetDontEvent e) {
          }
          public void error(TelnetErrorEvent e) {
          }
          public void readyToSend(TelnetReadyToSendEvent e) {
          }
          public void subOption(TelnetSubOptionEvent e) {
          }
          public void will(TelnetWillEvent e) {
            telnet1_will(e);
          }
          public void wont(TelnetWontEvent e) {
          }
        });
        terminalTextArea.setLineWrap(true);
        terminalTextArea.setNextFocusableComponent(connectDisconnectButton);
        terminalTextArea.setBorder(null);
        terminalTextArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        terminalTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
          public void keyTyped(KeyEvent e) {
            terminalTextArea_keyTyped(e);
          }
        });
        hostTextField.setBorder(BorderFactory.createEtchedBorder());
        hostTextField.setNextFocusableComponent(connectDisconnectButton);
        connectDisconnectButton.setAlignmentX((float) 0.5);
        connectDisconnectButton.setBorder(BorderFactory.createRaisedBevelBorder());
        connectDisconnectButton.setMaximumSize(new Dimension(80, 23));
        connectDisconnectButton.setMinimumSize(new Dimension(80, 23));
        connectDisconnectButton.setNextFocusableComponent(terminalTextArea);
        connectDisconnectButton.setPreferredSize(new Dimension(80, 23));
        connectDisconnectButton.setMargin(new Insets(1, 1, 1, 1));
        connectDisconnectButton.setText("Connect");
        connectDisconnectButton.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            connectDisconnectButton_actionPerformed(e);
          }
        });
        hostLabel.setText("Remote Host:  ");
        contentPane.add(hostTextField,     new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
                ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 2, 5), 0, 0));
        contentPane.add(connectDisconnectButton,   new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 10, 2, 5), 0, 0));
        contentPane.add(jScrollPane1,  new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0
                ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 5, 5), 0, 0));
        jScrollPane1.getViewport().add(terminalTextArea, null);
        contentPane.add(hostLabel,   new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 2, 10), 0, 0));
      }
      public static void main(String[] args) {
        (new TelnetDemo()).setVisible(true);
      }  /**Overridden so we can exit when window is closed*/
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  void connectDisconnectButton_actionPerformed(ActionEvent e) {
        terminalTextArea.setText("");
        try {
          connectDisconnectButton.setEnabled(false);
          if (!telnet1.isConnected()) {
            telnet1.setTimeout(10);
            telnet1.setRemoteHost(hostTextField.getText());
            telnet1.setConnected(true);
          } else {
            telnet1.setConnected(false);
          }
        } catch (IPWorksException ipwe) {
          connectDisconnectButton.setEnabled(true);
          ipwe.printStackTrace();
        }
      }  void telnet1_dataIn(TelnetDataInEvent e) {
        terminalTextArea.append(new String(e.text));
        terminalTextArea.setCaretPosition(terminalTextArea.getText().length());
      }  void telnet1_doDo(TelnetDoDoEvent e) {
        try {
          telnet1.setWontOption(e.optionCode);
        } catch (IPWorksException ipwe) {
        }
      }  void telnet1_will(TelnetWillEvent e) {
        try {
          if (e.optionCode == 38)
            telnet1.setDontOption(38);
        } catch (IPWorksException ipwe) {
        }
      }  void terminalTextArea_keyTyped(KeyEvent e) {
        try {
          byte[] toSend = new byte[1];
          toSend[0] = (byte)e.getKeyChar();
          telnet1.setDataToSend(toSend);
          e.consume();
        } catch (IPWorksException ipwe) {
        }
      }  void telnet1_connected(TelnetConnectedEvent e) {
        if (telnet1.isConnected())
          connectDisconnectButton.setText("Disconnect");    connectDisconnectButton.setEnabled(true);
      }  void telnet1_disconnected(TelnetDisconnectedEvent e) {
        connectDisconnectButton.setText("Connect");
        connectDisconnectButton.setEnabled(true);
      }
    }
      

  4.   

    用telnet <host> <port>不就行了,如果能连上,说明host的port端口是打开的。