News for hackers. Stuff that matters.
Re:Moving from PHP to C saved my web startup. by earlyriser on Friday October 22, 2010 @01:21PM (#1820510)
could you give more info about how you did it? I need to implement something like this for a frequent Ajax refresh.
Re:Moving from PHP to C saved my web startup. by BuckToBid on Friday October 22, 2010 @01:33PM (#1820570)
You could google how to do a simple fork server in c/c++ and after you have that working you just need to do something like this:

stringstream response; response << "HTTP/1.1 200 OK\r\n" << "Server: BTB Auction Updater 1.0\r\n" << "X-Powered-By:BTB Update Engine\r\n" << "Content-Length: " << msg.length() << "\r\n" << "Content-Type: text/html\r\n\r\n" << msg;

  int n = write(s, response.str().c_str(), response.str().length());
  if(n < 0) error("Error writing to socket");
to write back a valid header that a browser will understand
Re:Moving from PHP to C saved my web startup. by chrisaycock on Friday October 22, 2010 @03:17PM (#1821066)
You could store the static (non-changing) text in a couple of C strings and then call writev() to communicate everything. That would save you the step of continuously reconstructing the header.
Re:Moving from PHP to C saved my web startup. by BuckToBid on Friday October 22, 2010 @03:58PM (#1821242)
Thanks for the suggestion!
Re:Moving from PHP to C saved my web startup. by BuckToBid on Friday October 22, 2010 @01:23PM (#1820526)
Sure thing, what part are you stuck on?
Re:Moving from PHP to C saved my web startup. by earlyriser on Friday October 22, 2010 @03:46PM (#1821180)
thanks for the reply. I haven't started to code, but maybe in some weeks. It's more a chatroom with long polling that I will be building. All your app is in one dedicated server?
Re:Moving from PHP to C saved my web startup. by BuckToBid on Friday October 22, 2010 @03:53PM (#1821217)
Yes, right now its all on one server. The parts of the app that serve the pages you see are separate from the 1 second updater and could be on separate servers.

Right now the C app listens on a different port that lighttpd will redirect to if a pertinent request comes in.

There is a parent process that will fork upon connection and pass the socket to child which pulls the requested data and quickly returns it.

The one thing you will also need if you go this route is a SIGALRM handler. You just basically call signal() and set a flag before each socket read and write. So if the socket takes too long to return the signal gets called and kills the child process. Then you clear the flag on the other side of the socket read/write call so that the signal doesn't kill the process if the call returns on time.

Re:Moving from PHP to C saved my web startup. by konad on Friday October 22, 2010 @01:26PM (#1820536)