views:

755

answers:

2

I'm trying to implement a socket server that will run in most shared PHP hosting.

The requirements are that the Socket server can be installed, started and stopped from PHP automatically without the user doing anything. It doesn't matter what language the socket server is written in, as long as it will run on the majority of shared hosting globally.

Currently, I've written a Socket Server with PHP that implements an Object Cache: http://code.google.com/p/php-object-cache/

source: http://code.google.com/p/php-object-cache/source/browse/trunk/socket.class.php

However, PHP has to be compiled with sockets support, and not many servers run with PHP sockets support.

My real question is: What language should I implement the socket server in, and have maximum platform support and be invokable from within PHP.

In other words, what scripting language is the most common on PHP enabled Servers?

Or do I have to write the socket server in a compiled language to have it works across all servers?

Lets leave IIS out of the picture at the moment, just Linux servers. I don't think many PHP sites are running on IIS...


edit:

Sorry I think my question is not clear.

I'd like to know, what languages is best suited for creating a socket server given the following requirements:

The language must exist in shared hosting, alongside PHP running in Apache (not CLI). The sockets support must be enabled natively, not via a required extension. PHP must be able to write the deamon to file as well as start and stop the deamon.

I'm not asking for a solution for a single server. It has to run natively on the majority of shared hosting servers.

+6  A: 

Any server can be stopped or started by PHP under Linux. Of course, if you are running a server which accepts sockets from the internet, then you can just connect directly to the server and tell it to shutdown. No need to go via PHP!

As for "starting a server from PHP", well, under Linux, anything can be started from pretty much anything. Just shell out to start the process and have it drop into daemon mode.

I'm a Perl fan myself. Not surprisingly, there's a Perl Daemon library available.

If your hosting provider offers Perl script support, then you probably have permission to use "system" or backticks command. Then you can very likely start a daemon. However, you will need to use a non-privileged port (over 1024). Also, you should ASK THEM FIRST! They may not appreciate you tying up ports on their server. This is very definitely something you should discuss with your hosting provider.

the.jxc
+1 for mentioning Perl::Daemon. Agreed that the OP needs to discuss with hosting provider before writing code, but I think the requirement that it works with generic providers services 'smells'.
Sinan Ünür
+1  A: 

It really depends on what the install requirements are. Often the easiest and most standard way to write a socket server is to write an inet.d service. This is a standard daemon on my unix machines, and it will fork a process and handle the socket level details. If you want your service to run on port below 1024 on Unix, this is one of the easier ways to get it done. However, the initial install requires root to configure inet.d.

If you shared hosting allows PHP to do an exec call, then you could start the daemon that way. Keep in mind though, it'll need to run above port 1024. You next need to decide if your program is going to be multi-threaded or multi-process. Typically Java programs are multi-threaded, while an Apache instance is normally multi-process.

Lastly, the host may have a firewall in place. This helps prevent shared hosting accounts from becoming part of a bot-net. If the firewall rules don't allow connections to other ports, you won't be able to connect to it remotely.

brianegge