Is if possible to find out what service that listen on a port?
Hello!Below is a code that performs som port scanning. But I wonder if it is possible to find out what kind of service ther is if the port is open.
I know:
// int p = 21; // ftp
// int p = 23; // telnet
// int p = 25; // smtp
int p = 80; // http
// int p = 110; // pop3
...But can I find this out by code dynamically?
Best regards
Fredrik
package portscanner;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.net.ssl.*;
import java.io.*;
import java.security.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.security.cert.*;
import com.dstc.security.smime.*;
import com.dstc.security.provider.*;
public class PortScannerApplication extends JFrame implements ActionListener
{
JLabel hostjLabel = new JLabel("Host");
JLabel fromjLabel = new JLabel("From-port");
JLabel tojLabel = new JLabel("To-port");
JTextField hostjTextField = new JTextField("192.168.0.1");
JTextField fromjTextField = new JTextField("0");
JTextField tojTextField = new JTextField("8000");
JTextArea resultjTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane(resultjTextArea);
JButton jButton = new JButton("Scan");
public PortScannerApplication()
{
setSize(400, 400);
getContentPane().setLayout(null);
setBackground(new Color(200, 200, 200));
hostjLabel.setBounds(5, 5, 100, 25);
fromjLabel.setBounds(5, 35, 100, 25);
tojLabel.setBounds(5, 65, 100, 25);
jButton.setBounds(5, 155, 100, 25);
hostjTextField.setBounds(110, 5, 280, 25);
fromjTextField.setBounds(110, 35, 280, 25);
tojTextField.setBounds(110, 65, 280, 25);
jScrollPane.setBounds(110, 95, 280, 270);
jButton.addActionListener(this);
getContentPane().add(hostjLabel);
getContentPane().add(fromjLabel);
getContentPane().add(tojLabel);
getContentPane().add(jButton);
getContentPane().add(hostjTextField);
getContentPane().add(fromjTextField);
getContentPane().add(tojTextField);
getContentPane().add(jScrollPane);
}
public void actionPerformed(ActionEvent e)
{
try
{
scan();
//resultjTextArea.setText();
}
catch (Exception ex)
{
resultjTextArea.setText("No response from server");
}
}
public void scan() throws Exception
{
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Scanning at: " + hostjTextField.getText());
stringBuffer.append("\nBetween ports: " + fromjTextField.getText() + "-" + tojTextField.getText());
resultjTextArea.setText(stringBuffer.toString());
int startPort = Integer.parseInt(fromjTextField.getText());
int stopPort = Integer.parseInt(tojTextField.getText());
InetAddress inetAddress = InetAddress.getByName(hostjTextField.getText());
for(int i = startPort; i <= stopPort; i++)
{
stringBuffer = new StringBuffer( resultjTextArea.getText() );
try
{
Socket socket = new Socket(inetAddress, i);
stringBuffer.append("\nports nr: ");
stringBuffer.append(i);
stringBuffer.append(" host is listening");
socket.close();
}
catch (IOException e)
{
stringBuffer.append("\nports nr: ");
stringBuffer.append(i);
stringBuffer.append(" host is not listening");
}
resultjTextArea.setText(stringBuffer.toString());
}
}
public static void main(String[] args)
{
PortScannerApplication portScannerApplication = new PortScannerApplication();
portScannerApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
portScannerApplication.setVisible(true);
}
}
