Is there anyway to prevent multiple instances of a program from being spawned? Unlike say Internet Explorer which you can have multiple browsers running. There must be a way but im not too sure how i would go about doing this. :confused:
Printable View
Is there anyway to prevent multiple instances of a program from being spawned? Unlike say Internet Explorer which you can have multiple browsers running. There must be a way but im not too sure how i would go about doing this. :confused:
i'm not sure if this will work, but you can try it anyway. When your program starts up, lookup the names of all processes running, if you see one that is already up, close the program.
one cunning way i've heard of is to check to see if a port is open. If it is close the program if its not open that port.
You obviously want to make sure you pick a port that is rarely used.
i have thought of doing this in java. In vb and c++ on windows this is cake. I can think of no reason why the port trick should not work so I would try that approach.
When you guys say port are you refering to ports like in sockets and ports? I thought the purpose of a port was for incoming and outgoing data transfer?
Yes, sockets. If you put a listener on a particular port #, when another instance of the program is run, it is prevented from creating a listener on that same port #.
Heh, you could even choose the commonly used port for Back Orifice, which, if you were in Windows, would prevent a script kiddie from being able to install B.O. on your computer! Might freak out your virus detection software though.
Also, I suppose the listener shouldn't actually do anything. It should just wait but ignore any attempts at a connection. Part of your testing should include a test to ensure that if someone does a:
telnet <your_server> <port_number_of_your_choosing>
That it doesn't stop or disrupt your program!
cudabean
Cudabean it sounds like you are talking about a remote user running an application off of a server. I was thinking of just a stand alone application. :p
Doesn't matter, you can use some of the features of sockets even if you're writing a stand-alone ap, unless you're in like DOS or Windows 3.1 that doesn't have Winsock.
cudabean
Dilenger4 - You don't have to use this port/sockets approach. It's just one idea suggested by Mrs. Kensington. I think it's a good way to go because other approaches like writing to a file are prone to failure. If the computer goes down because somebody pulled the plug, you still have this message in a file that says that "the program is running" when the program is NOT running.
Also checking for processes would be a real pain, I think, compared to the port/socket approach.
cudabean
So let me see if i can understand this better. When the user
fires up the application. I would have to find what port it is running on?
Sort of, when the program starts up, it tries to listen on port 7823. If it can't listen on 7823, that means that some other program is already listening on that port and it shuts down.
cudabean
use the prevInstance property of App, it returns true if another instance is running, false if it is not....
Private Sub Form_Load()
If App.PrevInstance Then
Unload Me
Exit Sub
End If
'continue
or use in your code and exit function another instance is running...
lord
I think where im a little bit fuzzy is were and how Windows runs programs. Ill have to look into this further. Thanks. :)
lordsty yes that is what i am looking for. But i have to find a way to do it in Java not VB. Im sure there is a way i just have to search through my books. :rolleyes: :)
Lordsty -
This is Java, not VB
Dillenger -
Don't think Windows because this approach will work in Unix and Windows, so long as sockets exists, which is 99% of computers. Plus, it doesn't matter if you are connected to the internet at the time or not. :)
cudabean
Lordsty you are in the wrong forum.
Cuda is right, when the app starts up grab a port and hold on to it, if the port is in use, display a message saying the app is already up. When the app is shut down, release the port. Simple as that. The jvm and windows will handle clearing the port on wierd shutdowns.
I would not go the processes route, or if you want to you will lose the platform independcy.
Files do not work.
you specify the port that you want your socket to listen on, if it cant it will error.
Oh ok i get it. Every time my prog fires up i assign it to the same port. So if another instanace of the application wants to run, it cant because the port is taken from the first time the program was ran. :p
yup, you got it, if the app is up already and has the port the new one should throw an i/o exception....
:eek:
isn't opening ports a secruity risk?
no, just dont let the socket accept any connections...
if the program does accept connections, but doesn't do anything else, how hard is it for someone to remotely get control of the computer through that port?
I'm writing a networking app. now, and security is important to me, so i'm trying to learn as much as i can on the issue.
I don't think you'd have a security risk. Most port listeners do implement some sort of protocol like ftp or telnet or somesuch, but since this port listener is dumb as a post even the M3G4-733T script kiddie gets bored after a very short time.
For example:
telnet i_will_own_you.com 7823
Connection Refused
telnet i_will_own_you.com 7823
Connection Refused
"Dang, I'm bored"
cudabean
Yep! As long as the socket refuses connections you are fine with security.