Reputation: 503
I am new to Socket programming. I am writing an application on a client machine to occassionaly make a request for some data or send some data. This exchange will take place a-periodically and in-frequently once or twice a month at best. From the research I have been doing into socket programming, I need a script whose execution never ends. Instintivelly I think that this will unnecessarily consume resources for such an in-frequent use, but PHP has no mechanism for interrupts. The ideal script would sit there doing nothing and only when communication via the port takes place, does the script wake up and execute. This in other languages would be done via an interrupt.
Can anyone propose an approach that I could take around this problem please?
Thanks John
Upvotes: 0
Views: 991
Reputation: 60007
John,
In your question that you mention that this possible transaction will occur at most one or twice a month. Have you considered using cron (or the windows equivalent) on the client end. Perhaps run it once a hour to check?
As the data is so infrequent is it very mush time dependent?
Upvotes: 0
Reputation: 15962
You can use socket_select
with a null
fourth parameter to put your script to sleep indefinitely and have it wake up when there's data on the socket to be read.
The other, less efficient, way is to poll, sleep for n minutes, poll again, and so on.
Upvotes: 1
Reputation: 41
You might try a combined approach. Write the PHP script so that it polls/waits for a limited time and use the scheduler to run at certain times, say once every 15 minutes. I wrote a very simple socket program to talk to a medical device. It doesn't take up a lot of resource for it to sit and wait, but if you are concerned you can limit with the technique above.
Upvotes: 0