Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache - handling TCP connections, but not HTTP requests

Tags:

php

tcp

apache

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.

like image 274
Scott Avatar asked Oct 12 '10 17:10

Scott


People also ask

Is HTTP connection TCP?

Opening a connection in HTTP means initiating a connection in the underlying transport layer, usually this is TCP. With TCP the default port, for an HTTP server on a computer, is port 80. Other ports can also be used, like 8000 or 8080.

Is every HTTP request a new TCP connection?

In HTTP/0.9 (not used anymore), each request used a separate TCP connection, and the end of a response was signalled by closing the connection. In HTTP/1.0, separate connections are still the official default.

Does TCP happen before HTTP?

Most applications don't need to worry about TCP. And, TCP is just the first layer beneath HTTP. After TCP at the transport layer comes IP as a network layer protocol.

Does HTTP use multiple TCP connections?

Explanation: HTTP may use different TCP connection for different objects of a webpage if non-persistent connections are used.


2 Answers

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.

like image 113
Andrew Sledge Avatar answered Oct 05 '22 11:10

Andrew Sledge


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 56789. In /etc/services, add the line:

gpsservice   56789/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.

like image 43
webbiedave Avatar answered Oct 05 '22 13:10

webbiedave