How can I determine when multiple instances of my program are running on the same machine?

John Mitchell

The most portable, general way is for your program to attempt to create a server socket on a particular port. The first instance will be able to create the port and so it will know that it's the first. Subsequent instances won't be able to bind to that server socket and so they'll know that they aren't the first.

If you need to track or otherwise manage all of the instances then you can change things such that if they can't bind to the server socket that they then try to connect to the first instance of the program using a client connect through the socket and you can do whatever you want from there.

Of course, if you're worried about controlling the number of copies of your program for "copy protection" purposes or you're wanting to control multiple instances of unsigned applets then you need to mediate the connections through a trusted server that you control.