0

As a tangent to a larger project, I'm trying to implement some code to scan for open ports so that I may eventually run a packet test. At the moment I am just trying to connect to google.com and I know that port 80 accepts a connection, so I've formatted my code to scan a range of ports including this one. However, whilst I can connect directly to port 80, the scanner code seems to pass it by. I was wondering if anyone had any thoughts?

public class PortScanner {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        String host = "www.google.com";

        System.out.println("Attempting to connect to " + host + " at port 80...");
        try {
            socket.connect(new InetSocketAddress(host, 80), 5000);
            System.out.println("Connection Successful.");
        }
        catch (Exception e) {
            //Just bypass the exceptions for now
        }

        System.out.println("Scanning for open ports on " + host);


        for (int i = 75; i < 85; i++) {
            System.out.println("Attempting to connect to " + host + " on port " + i);
            try {
                socket.connect(new InetSocketAddress(host, i), 5000);
                System.out.println("Open port found at " + i);
            }
            catch (ConnectException e) {
                System.err.println("Connect Exception Thrown on Port " + i);
                continue;
            }
            catch (SocketException e) {
                System.err.println("Socket Exception Thrown on Port " + i);
                continue;
            }
            catch (UnknownHostException e) {
                System.err.println("Unknown host at " + host);
                System.exit(1);
            }
        }

        System.out.println("Closing connection to " + host);
        socket.close();
        System.out.println("Connection closed");
    }
}

In fact, the loop reports that there is a SocketException thrown at port 80:

Attempting to connect to www.google.com at port 80...
Connection Successful.
Scanning for open ports on www.google.com
Socket Exception Thrown on Port 75
Socket Exception Thrown on Port 76
Socket Exception Thrown on Port 77
Socket Exception Thrown on Port 78
Socket Exception Thrown on Port 79
Socket Exception Thrown on Port 80
Socket Exception Thrown on Port 81
Socket Exception Thrown on Port 82
Socket Exception Thrown on Port 83
Attempting to connect to www.google.com on port 75
Socket Exception Thrown on Port 84
Attempting to connect to www.google.com on port 76
Attempting to connect to www.google.com on port 77
Attempting to connect to www.google.com on port 78
Attempting to connect to www.google.com on port 79
Attempting to connect to www.google.com on port 80
Attempting to connect to www.google.com on port 81
Attempting to connect to www.google.com on port 82
Attempting to connect to www.google.com on port 83
Attempting to connect to www.google.com on port 84
Closing connection to www.google.com
Connection closed

The fact that the console logs are coming out in the order that they are made me wonder if the connection attempts were perhaps coming in too quickly and thus causing the program to miss the ports that are available, but I'm not really sure what to do about that.

Any thoughts appreciated!

lordchancellor
  • 3,847
  • 4
  • 26
  • 26
  • 1
    Have you considered that Google might be blocking you because you're performing an unauthorised port scan against one of it's machines. You should be testing this against your own kit, not someone elses. – Philip Whitehouse Feb 10 '15 at 12:00
  • I had not considered this! In larger context, I'm trying to build a ping tester, which I understand in Java means you need a socket connection. If you don't know exactly what port to connect to, and you can't scan the ports, what other option is there? – lordchancellor Feb 10 '15 at 12:04
  • 1
    There might be no other option. But Google will advertise the services they provide openly so will feel justified in blocking clients that try to look for other stuff. There is much work gone into testing for presence of services while not triggering detection systems. It's a game of cat and mouse to some degree. You might consider investigating tools like nmap. – Philip Whitehouse Feb 10 '15 at 12:07
  • Thanks for your thoughts @PhilipWhitehouse, I appreciate your input. I'll have to rethink how I'm going to achieve my goal, but I suppose that's all part of the fun... – lordchancellor Feb 10 '15 at 14:47

1 Answers1

1

A Socket can only be used to 1 active connection at the most! And since you connect at the beginning of your port scan (and only close it at the end), any further attempt to connect (by calling the connect() method) will throw a SocketException.

Print the message of your SocketException to verify this which will be: "already connected".

Suggestion: Create a new Socket before each attempt. Note that a Socket cannot be reused for multiple/new connections. Quoting from the Javadoc of Socket.close():

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

icza
  • 389,944
  • 63
  • 907
  • 827
  • Thank you so much for your explanation, it has been a huge help. I'm dabbling in completely new territory here! Just out of curiosity, if I change the code to create an anonymous instance of Socket each time, how do I close these instances (i.e. with no name to refer to?) – lordchancellor Feb 10 '15 at 14:46
  • @lordchancellor Do not create it anonymously. Use a `try-with-resources` block which will automatically close the `Socket` once execution leaves the block. – icza Feb 10 '15 at 14:49