Check status of a port

Please let me know how I can check if a port is available or not. If it is not available, i want to know what service is using it. I want to do this on both mac and windows.

Thanks!
dshrenikAsked:
Who is Participating?
 
HDatabase AdministratorCommented:
sample code
public static boolean available(int port) {
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

Open in new window

0
 
for_yanCommented:
You need to run network utility with Runtime.getRuntime().exec which will list ports and services
and then get the output into back to java and anlyze it.
And it would be pdifferent on Windows and Mac.
Let me dig what is the utility.
0
 
HDatabase AdministratorCommented:
if you use the dos telnet command and a blank dos window pops up the port is open..

telnet 127.0.1.1:portnumber

telnet 127.0.0.1:40

Also there are tons of opensource portscanners available if you want more details..

http://sourceforge.net/search/?type_of_search=soft&words=port+scanner
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
for_yanCommented:


netstat  on windows:
http://security.fnal.gov/handouts/IdentifyingOpenPortsWinXP-2003.pdf

I guess Mac is like Unix they probably also have netstat. let's dig more
0
 
dshrenikAuthor Commented:
I want to do it through java code. Thanks!
0
 
HDatabase AdministratorCommented:
Java or Javascript?
0
 
dshrenikAuthor Commented:
Java
0
 
for_yanCommented:
If your goal is to find one port to use then trying telnet will be better
if your goal is to know which piorts are doing what then you need some utility to use
0
 
dshrenikAuthor Commented:
I want to find out if a given port number is available. If it is not, I want to know what's using the port.
Thanks!
0
 
for_yanCommented:

this is waht prints the open ports on Mac
"sudo lsof ..." (see caommand in there)
http://juretta.com/log/2007/08/08/list_open_ports_on_your_machine_mac_os_x_/
0
 
for_yanCommented:
You probably want to find options in these commands which will list you only your particular port which you are intesrested in.
So if you hget nothing - then your port is not used, otherwise you'll read from the output waht it is used for

0
 
dshrenikAuthor Commented:
@hollecar:
Can you tell me how I can find out what service is using the port in case the port is unavailable. Thanks!
0
 
for_yanCommented:
Why do you need to know hwat service?
that's the difference - if you want to know if it is available - that;s mucvh easier inside java,
if you want to know who uses it - it is more dofficultb to do through pure java
0
 
dshrenikAuthor Commented:
I want to make sure the right service is using the port.
0
 
CEHJCommented:

>>I want to make sure the right service is using the port.

You can only do that by getting a shell to the host on which the service is running and checking what process is involved
0
 
HDatabase AdministratorCommented:
In windows and mac

netstat -o

This will give you the PID

MAC syntax may be different try looking at help on netstat

If you need this in Java also you could use a shell command
0
 
HDatabase AdministratorCommented:
Example...
final String cmd = "netstat -ano";

int pid = -1;

try {
    // Run netstat
    Process process = Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
    e.printStackTrace(System.err);
}

Open in new window

0
 
dshrenikAuthor Commented:
@CEHJ:
Could you please elaborate a little more. If possible, please provide some sample code. Thanks!
0
 
HDatabase AdministratorCommented:
WIndows

After you get the PID you can also use TASKLIST to get more details...

MAC

You may need a 3rd party tasklist utility

0
 
HDatabase AdministratorCommented:
Here is a useful list of mac os X commands that you could use in a java shellcommand

http://ss64.com/osx/


0
 
CEHJCommented:
Actually i retract that comment. You can only be sure by connecting to it and successfully implementing its protocol as a client.

btw, are you able to get a shell to the server?
0
 
HDatabase AdministratorCommented:
Also you may need to test what o/s the program is on prior to executing any shell commands...Here is a sample I refer to occasionally....


public class OpertingSystemInfo 
{
  public static void main(String[] args)
  {
  String nameOS = "os.name";  
  String versionOS = "os.version";  
  String architectureOS = "os.arch";
  System.out.println("\n  The information about OS");
  System.out.println("\nName of the OS: " + 
System.getProperty(nameOS));
  System.out.println("Version of the OS: " + 
System.getProperty(versionOS));
  System.out.println("Architecture of THe OS: " + 
System.getProperty(architectureOS));
  }
}

Open in new window

0
 
CEHJCommented:
You might look at Nmap and related:

http://seclists.org/nmap-dev/2010/q2/536
0
 
CEHJCommented:
Not sure how that accepted solution is relevant to your question. Can you explain?
0
 
dshrenikAuthor Commented:
The code tells me if a given port is available or nor. Right?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.