Link to home
Start Free TrialLog in
Avatar of netslt
netslt

asked on

Java connect to ports in list and return banner

Hi

How can I connect to a list of ports, note if the port is open or closed and if open read a server banner if one is there?

i have tried the code below, it works basically, but if a port is open it takes ages until it goes to the next port.

try {
                                InetAddress address = InetAddress.getByName(domain);
                                int[] ports = new int[]{21, 22, 23, 80, 81, 88, 8080, 443};
                                for (int i = 0; i < ports.length; i++) {
                                    int port = ports[i];
                                    Socket socket = null;
                                    try {
                                        socket = new Socket(address, port);
                                        socket.setSoTimeout(500);
                                        System.out.println("port " + port + " open");
                                        BufferedReader reader = new BufferedReader(
                                                new InputStreamReader(socket.getInputStream()));
                                        String line = reader.readLine();
                                        if (line != null) {
                                            System.out.println(line);
                                        }
                                        socket.close();
                                    } catch (SocketTimeoutException ex) {
                                        // port was open but nothing was read from input stream
                                        //ex.printStackTrace();
                                        System.out.println("error timeout socket: " + port);
                                    } catch (ConnectException ex) {
                                        // port is closed
                                        //ex.printStackTrace();
                                        System.out.println("error socket: " + port);
                                    } catch (IOException e) {
                                        //e.printStackTrace();
                                        System.out.println("error io socket: " + port);
                                    } finally {
                                        if (socket != null && !socket.isClosed()) {
                                            try {
                                                socket.close();
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                                System.out.println("error  socket close: " + port);
                                            }
                                        }
                                    }
                                }

                            } catch (Exception ex) {
                                //NewJFrame.showMessageDialog(null, ex.getMessage(),"Error");
                                System.out.println(">>>> error:" + ex.getMessage() + " " + ex.getStackTrace());
                            }

Open in new window


Thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

This is a good place in which to use multithreading. See

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection,%20long,%20java.util.concurrent.TimeUnit)

Don't use plain Sockets though - you'll get all the headers too, which you presumably don't want? Use URLConnection
Avatar of netslt
netslt

ASKER

I want all banners, I think they are the headers, ie

port 21 open
220 Webland FTP Server v3.10.0 (Build 2) ready...

port 21 open
220 FTP Server ready.

etc.

I think the socket stays open to long with my code, it can take a minute or so until the next loop will be called.

So how can the stream reader be closed faster? Before the socket sends a timeout?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)