给主要的思路。另外有代码的给高分我在网上下了一个有木马所以下想自己写一个。。如果有无毒的也可以给链接最好有代码并告诉怎么样编写

解决方案 »

  1.   

    似乎可以这样
    import java.net.Socket;public class Test {
    public static void main(String args[]) {
    for (int i = 1; i <= 65535; i++) {
    try {
    Socket s = new Socket("127.0.0.1", i);
    System.out.println("port   " + i + "   ok");
    s.close();
    } catch (Exception ex) {
    System.out.println(i+" is in use");
    }
    }
    }
    }
      

  2.   

    这样。
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.SocketAddress; /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *//**
     * Project  : Test
     * Package  : 
     * File     : PortTest.java
     * Author   : solosky < [email protected] >
     * Created  : 2010-2-19
     * License  : Apache License 2.0 
     *//**
     *
     *
     * @author solosky <[email protected]>
     */
    public class PortTest
    {

    public static void main(String[] args)
    {
    for(int i=0; i< 65535; i++){
         try{
          ServerSocket s = new ServerSocket(i);
          //System.out.println("port "+i+" can be used.");
          s.close();
         }catch(IOException e){
         if(e instanceof java.net.BindException) {
         System.out.println(i+" aready in use.");
         }else {
         System.err.println(" other error..");
         }
         }
    }
    }
    }
      

  3.   

    你要想检测那个程序使用的端口,JAVA是办不到的,要使用WinAPI才行。
      

  4.   

    这个程序是测试服务器的port是否可用,要想知道使用该端口的程序很难的。
    首先java没有这个api,要使用操作系统的api.
    不一定是windows的系统,而且要有server的权限才行。不然就是入侵了。
      

  5.   

    这种东西何必要自己写呢,用netatat命令看一下不就行了。
      

  6.   

    那如果要是写windows的应该有相关的文章吧
      

  7.   

    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    #pragma comment(lib,"ws2_32.lib")
    void main(int argc ,char * argv[]) {
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;wVersionRequested = MAKEWORD( 2, 2 );err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
      return;
    }if ( LOBYTE( wsaData.wVersion ) != 2 ||
       HIBYTE( wsaData.wVersion ) != 2 ) {                                 
      WSACleanup( );
      return; 
    }if (argc != 4) {
      printf("缺少命令行参数!\n");
      printf("用法如下:scan.exe IP 开始端口 终止端口\n");
      exit(-1);

    int startport = atoi(argv[2]);
    int endport = atoi(argv[3]);
    SOCKET s = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.S_un.S_addr = inet_addr(argv[1]);
    for (int i = startport;i < endport;i ++) {  addr.sin_port = htons(i);
      if (connect(s,(struct sockaddr*)&addr,sizeof(addr)) != SOCKET_ERROR) {
       printf(" port %d is open!\n",i);
      } else {
       printf("port %d is closed!\n",i);
      }
    }
    }
      

  8.   

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package exp1;import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;/**
     *
     * @author 波
     */
    public class NewClass {public static void main(String[] args) 
    {
    Socket s=null;
    int i;
    for(i=20000;i<20006;i++)
        {
                try {
                    s = new Socket("127.0.0.1", i); //IP 要扫描的
                } catch (UnknownHostException ex) {
                   // Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                   // Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
                }
            System.out.println(" 端口" + i + "is open");
                  
        }
    try {
         s.close();
        } catch (IOException ex) {
         // Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    }这写法貌似所有的端口都是open的
      

  9.   

    2L那个 太强悍了 如果LZ只是在Windows下使用的话 可以这样
    package test;import java.io.BufferedReader;
    import java.io.InputStreamReader;public class TestNetatat {
    public static void main(String[] args) throws Exception {
    BufferedReader reader = reader = new BufferedReader(
    new InputStreamReader(Runtime.getRuntime().exec("netstat -an")
    .getInputStream()));
    String line = null;
    while ((line = reader.readLine()) != null) {
    System.out.println(line);
    }
    }
    }
      

  10.   

    /**
         * 服务器网络配置
         * @param strIp
         * @param strPort
         * @return
         */
        public static String telnetConnect(String strIp,String strPort){
            if(UtilPub.isEmpty(strIp)||UtilPub.isEmpty(strPort)) return "请配置好IP和端口!";
            Socket server=null;
            try {
                server=new Socket();
                InetSocketAddress address = new InetSocketAddress(strIp, Integer.valueOf(strPort).intValue());
                server.connect(address, 5000);
                return "服务连接正常";
           } catch (UnknownHostException e) {
                return "服务连接不通";
            } catch (IOException e) {
                return "服务连接不通";
            } finally {
                if(null!=server){
                    try {
                        server.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
      

  11.   

    package com.test1;
    /*
     * 这是第一个服务端程序,让它在9999端监听
     * 可以接受从客户短发过来的数据
     */
    import java.io.IOException;
    import java.net.*;
    public class MyServer1 { public static void main(String []args){
    MyServer1 ms1=new MyServer1();
    }
    public MyServer1(){
    try {
    //在9999号端口监听
    ServerSocket ss=new ServerSocket(9999);
    System.out.println("我是服务器,在9999端口监听..");
    //等待某个客户端来连接,该函数会返回一个Socket连接
     Socket s=ss.accept();
    System.out.println("11");  
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
    package com.test1;
    /*
     * 这是一个客户端
     */import java.net.*;
    public class MyClient1 { public static void main(String[] args) { MyClient1 mc1=new MyClient1();
    }
    public void MyClient1(){
    try {
    Socket s=new Socket("127.0.0.1",9999);
    } catch (Exception e) {
    e.printStackTrace();

    }
    }
    为什么先运行第一个再运行第二个 只能打印出我是服务器,在9999端口监听..
    不能打印出 11呢   谢谢打架了