?
Solved

Check status of a port

Posted on 2011-10-12
25
Medium Priority
?
385 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:dshrenik
  • 8
  • 7
  • 6
  • +1
25 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36958456
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
 
LVL 8

Expert Comment

by:H
ID: 36958463
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36958469


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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dshrenik
ID: 36958476
I want to do it through java code. Thanks!
0
 
LVL 8

Expert Comment

by:H
ID: 36958482
Java or Javascript?
0
 

Author Comment

by:dshrenik
ID: 36958488
Java
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36958493
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
 

Author Comment

by:dshrenik
ID: 36958495
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36958514

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
 
LVL 8

Accepted Solution

by:
H earned 2000 total points
ID: 36958515
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36958541
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
 

Author Comment

by:dshrenik
ID: 36958606
@hollecar:
Can you tell me how I can find out what service is using the port in case the port is unavailable. Thanks!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36958625
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
 

Author Comment

by:dshrenik
ID: 36958629
I want to make sure the right service is using the port.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36958659

>>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
 
LVL 8

Expert Comment

by:H
ID: 36958661
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
 
LVL 8

Expert Comment

by:H
ID: 36958675
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
 

Author Comment

by:dshrenik
ID: 36958682
@CEHJ:
Could you please elaborate a little more. If possible, please provide some sample code. Thanks!
0
 
LVL 8

Expert Comment

by:H
ID: 36958685
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
 
LVL 8

Expert Comment

by:H
ID: 36958761
Here is a useful list of mac os X commands that you could use in a java shellcommand

http://ss64.com/osx/


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36958783
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
 
LVL 8

Expert Comment

by:H
ID: 36958787
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36958808
You might look at Nmap and related:

http://seclists.org/nmap-dev/2010/q2/536
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36965105
Not sure how that accepted solution is relevant to your question. Can you explain?
0
 

Author Comment

by:dshrenik
ID: 36965126
The code tells me if a given port is available or nor. Right?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question