tags:

views:

72

answers:

2

I have a GPS unit that can send data over a TCP connection, but I don't have the ability to modify the message that it sends so it would come to my server in the form of an HTTP request - it can only send a message in a predefined format.

So, I have the following questions:

1) Is it possible to have Apache handle a TCP connection that doesn't come in the form of an HTTP request, and have the message that is sent be processed by a PHP script?

2) If #1 isn't possible, how would you recommend I handle the data being sent to my server?

I will potentially have hundreds, if not thousands, of these GPS units sending data to my server so I need an efficient way to handle all of the connections coming in (which is why I wanted Apache or some other production worthy server to handle the TCP connections). I would like to be able to deal with the message sent over the connection with PHP since that is what the rest of my application runs on, and I will need to insert the data sent into a database (and PHP is really good at doing that kind of thing).

In case it matters, the GPS unit can send data over a UDP connection, but from what I have read Apache doesn't work with UDP connections.

Any suggestions would be welcome.

+3  A: 

To answer your questions:

1) Not without major modification

2) Build your own server. This is easily done with several platforms and in several languages. I personally like to use the Twisted Framework because Python is relatively simple to use and the framework is very flexible.

Andrew Sledge
+2  A: 

Using Apache wouldn't be practical as it's using a nuclear bomb when a firecracker will suffice. Creating a PHP server is quite simple on Linux with the help of xinetd.

Modify /etc/services. Say you want your service to run on port 456789. In /etc/services, add the line:

gpsservice   456789/tcp

In /etc/xinet.d/, create a file named gpsservice:

service gpsservice 
{
    socket_type             = stream
    protocol                = tcp
    wait                    = no
    user                    = yourusername
    server                  = /path/to/your/script
    log_on_success          = HOST PID
    disable                 = no
}

Create your PHP script (chmod it to be executable):

#!/usr/bin/php
<?php
// do stuff
?>

Restart xinetd service xinetd restart

You now have a quick TCP server written in PHP.

webbiedave
Thanks - I will look into this. Quick question though - what would it take to make this work with UDP connections? And, would this simple solution handle thousands of requests an hour?
Scotty
You could change `tcp` to `udp` in the service file and gpsservice file. It's not recommended to use UDP as it can be an unreliable protocol: http://www.mindcontrol.org/~hplus/udp-vs-tcp.html
webbiedave