Send data from php on webserver to c++ app on local machine

Posted on 2005-03-10
Medium Priority
Last Modified: 2008-03-10
I'm working on a project that needs to get data from a php script on a webserver and send it to a c++ app that is running on a local hardrive (not same computer as webserver). I know there are plenty of resources on wiritng sockets in php and c, however I need help understanding the specifics of making this work (if it is possible). First, the machine runninig the script will be connected to the internet of course. I know how to find the ip address of my pc, but I cannot seem to create a connection from a php script to it. Are there settings I need to alter or could there be a  block from the network i'm connected to? How do I choose a port and find out if it is open to send and receive on both ends?

My hack for the moment is to save the data i need to a file on the server and have the c app check that file. It does not seem to make sense to do it this way when I do not need to save the data but just transmit it. I also want to have the  c app just listening for incoming data, not actively accessing a file ona server.

Thanks for any help!
Question by:csugrue
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 17

Expert Comment

ID: 13514046
- I'd recommend a 'pull' not a 'push'.  That is, have the client pc connect to the server, not have the server try and push to the client.  That's just a general suggestion, usually helpful when the server/client might have particular firewall settings that make it difficult.

- In either case, yes, firewall or proxy could get in the way of opening a socket.  Depending on where the client PC is, opening a particular port could be blocked by the PC, or company (if internal) firewall, or ISP (if home) firewalling.

- The advantage of having the client PULL is that your webserver is already running, and accessible.  Thus, opening an HTTP connection is already possible -- so you use it.

- The C++ app and the webserver can obviously use a number of security methods to ensure safety of data -- assuming it's data that needs to be secured.  That, of course, would be a question in and of itself! ;)

I just had a user trying to use sockets with the opposite-direction problem -- he was trying to grab a page from a public website, but turned out HE was behind a firewall and proxy that was blocking the connection...

How often does data need to be transmitted between the machines?  Can you poll instead of push?  i.e., is there a reason you are trying to push from the server specifically?  I can think of things like say e-commerce orders on a website, and you want them pushed directly to you -- however, in most situations, you could do a check every 5 minutes, and batch-download any updates since the last check.

If you can detail the basic process, and where the client machine resides (home, work, etc.), I can give even more specifics.  But I think the above outlines the basic thought process.


Author Comment

ID: 13514422
Thanks for the comments!

Well, this for digital art installation that allows multiple users to connect via mobile phone to the application which is projected inside a gallery. It uses vxml to capture keypad input and the php script would then send it (potentially) to the c++ application. The cellphone becomes a remote control to move things around in the projection so it needs to have a quick response time to the users input.

Right now it functions by saving the data on the server and looking at that  data in intervals (I think this is what you are suggesting). This works fine with some delay, but I thought it would be faster and more logical if the php (it could also be a perl or c app on the webserver) sent to the c++ app directly. Does that seem to make sense?

The webserver is on a university server and the machine running the app will be on seperate network inside a gallery...
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13515720
I'd say this sounds like it should be done using a continuous delivery protocol like IRC or similar instead of HTTP, so that your PHP can send data immediately as it arrives. You'll get much better responsiveness that way. However, you can't reliably run a PHP script continuously from a web server - it needs to be a standalone process.You'll need to open a socket that the PHP process can connect to and keep open.  You can pass info to it via shared memory and kick it into action by sending it signals from scripts that are exposed to the web. This way you can do the 'push' technique you're after - maximum efficiency, minimum latency
LVL 17

Accepted Solution

davebytes earned 1000 total points
ID: 13519574
You can do that with standard WAP/HTML inputs, that's easy.  As Squinky notes, a custom application running on the webserver that keeps an open socket to the gallery box might be a more straightforward approach.

However, so long as your networking is fast enough, php should be able to receive the input from the user, open a socket to the gallery box, send the command, and close down, in an interval of a second or two.  At some level, it actually depends MORE on the wap/web connectivity of the phones TO the webserver than it does the webserver to the gallery box.  Phone browsers and latency from their network to the live net are notoriously slow, so the further delta opening a socket on the fly to the gallery box shouldn't be much.

CERTAINLY, you can get to a completed, working prototype opening and closing the socket for every input.  That will have you at a stable implementation of nearly everything.  If that proves to be too slow, FIRST prove the slowness is the PHP->C++ socket stuff, and NOT the cellphone side of the equation.

If it is due to opening sockets on the fly across the net to the gallery, there are a few approaches -- my suggestion, in order to keep things best-abstracted, would be to simply change the PHP code to open a LOCAL socket on the webserver box to an always-running daemon/application (on an internal-only-needed port number, like up in the 20K range or something), post the SAME EXACT data you'd post to the gallery box, and close down.  The daemon would have a continuously-open connection to the gallery box, sending keep-alive pings every N seconds (and re-establishing connectivity if dropped, as well as possibly kicking off email/etc. to you!), and has a listening socket for the PHP script to connect to.  When the script connects, it literally just passes the data along -- basically, acting as a conduit, doesn't even need to look at the payload.

But this way, START with building the php->c++ process as an open-send-close socket process, and see where your issues/bottlenecks really are before doing further enhacement -- as you should then at least be at full functionality, though it might be slow/hesitant with commands.


Author Comment

ID: 13520208
great, yess that sounds like the approach I'm looking for. I'm going to give these suggestions a try. thanks!

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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