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
netsltAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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
0
netsltAuthor Commented:
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
0
CEHJCommented:
Oh, i think i now see what you mean by 'banners'. But you're not going to get banners as such from web servers (which are included in your list)

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.
I've already said: if you multi-thread your code, you won't have to wait for one read to complete before trying the next one
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.