English 中文(简体)
How to scan remote host port that in used?
原标题:

this is the code that i get so far...

import java.net.*;

public class PortScanner {

    public static void main(String args[]) {
        int startPortRange = 0;
        int stopPortRange = 0;

        startPortRange = Integer.parseInt(args[0]);
        stopPortRange = Integer.parseInt(args[1]);

        for (int i = startPortRange; i <= stopPortRange; i++) {
            try {
                Socket ServerSok = new Socket("127.0.0.1", i);

                System.out.println("Port in use: " + i);

                ServerSok.close();
            } catch (Exception e) {

            System.out.println("Port not in use: " + i);
        }
    }
}

} for example on smtp server the system use port 25, and i need to scan port on remote client that connected to the smtp server. how can i do that?

please help me.

问题回答

Your code should look like this:

    for (int i = startPortRange; i <= stopPortRange; i++) {
            try {
                    Socket serverSok = new Socket("127.0.0.1", i);

                    System.out.println("Port in use: " + i);

                    serverSok.close();
            } catch (Exception e) {
                    System.out.println("Port not in use: " + i);
            }
    }

If I understood your problem:

  • a process S runs on machine A and listens on port P for incoming connections
  • a process C runs on machine B and connects from port T to P on A
  • your program F runs on machine A
  • F is not S

Do you want to discover T in F using Java? You cannot!

You have to read the operative system table of open sockets. This is an operative system dependent operation and it is not supported by java.

Otherwise if F is S, you can change the code of S to discover T. If S uses java.net.ServerSocket class, you can search ServerSocket.accept() and discover T using getPort() on the returned Socket instance.

LLP, Andrea

Firstly there are some methodological problems with this way of doing it since a port can be in use, but not listening.

If you re actually just looking for listening ports, and you want to use the approach above, at least spawn off some threads so that you don t have to wait for each port to time out sequentially.

The best way would be to mimic something like a syn scan rather than doing a full TCP handshake, but I m not sure you can get that close to the metal in Java.

Neat question though.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签