Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I'm using a cvs api and cannot send 2 cvs commands at a time.
I want to send the one cvs command and wait until the port is no longer in use before sending the second cvs command. The two commands happens on 2 different treads. Don't know much about threads so my guess is checking whether the port is in use.

Anyone know about a Class + function which I can make use of?
Posted
Comments
R. Erasmus 8-Apr-11 9:54am    
My problem was solved thanks to all... Eventually realised that it was not one of the things I thought but instead the path to where I was checking out the cvs data, being to long that caused a cvs error.

I am not a Java person, but irrespective of the programming language/library, the one way to check if a port on an IP address is available is to try connecting to it. That's what's done by port scanning software. You'll have to do the same in Java (search for Java's socket classes).
 
Share this answer
 
Comments
avigodse 8-Apr-11 7:58am    
Yes, agree with Nishant.
While trying to connect it, and if it in use, it'll throw exception about already in use.
Espen Harlinn 8-Apr-11 8:26am    
Good solution, my 5
Nish Nishant 8-Apr-11 8:27am    
Thank you!
Sergey Alexandrovich Kryukov 9-Apr-11 1:27am    
That is correct (I recently answered that for .NET), my 5.

I just added a useful note as a separate Answer, please see.
--SA
Nish Nishant 9-Apr-11 8:35am    
Thank you!
I would rather synchronize the threads than checking if a port is opened or not (while checking you'll consume resources (OS sockets) that are much more expensive than synchronization mechanism applied on threads).
Regards
 
Share this answer
 
Comments
Espen Harlinn 8-Apr-11 8:26am    
Good advice - my 5
Sergey Alexandrovich Kryukov 9-Apr-11 1:27am    
Seems reasonable. My 5.
--SA
Thanks all,
I did stumble accross a nifty function that checks whether a port is available or not:

public static boolean port_available(int 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;
  }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Apr-11 2:24am    
Seems to be a correct solution. My 5.
I just added a useful note as a separate Answer, please see.
--SA
This is a short addition to the other Answers:

Don't forget to limit your activity to the set of ports allowed by the regulations of IANA.
Always be compliant with this document: http://www.iana.org/assignments/port-numbers[^]. Pay attention to this statement:
The Dynamic and/or Private Ports are those from 49152 through 65535.
—SA
 
Share this answer
 
Comments
Nish Nishant 9-Apr-11 8:36am    
Useful additional info, my 5.
Sergey Alexandrovich Kryukov 9-Apr-11 18:28pm    
Thank you, Nishant,
--SA
Espen Harlinn 10-Apr-11 6:46am    
Good point, my 5
Sergey Alexandrovich Kryukov 10-Apr-11 20:31pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900