I'm working on a web application that will receive gps data and split received string to detailed information and then store it in some database tables.

Every piece of application is ready, now I need to setup my new server to accept data from gps device.

In my gps device, we have to set an IP and a port that will point to my host. -( my devices are using TCP )-
Server OS is linux CentOS 6.5 and there's a directadmin installed on it.

After a couple of days research, I figured it out that I have to open a port -(31334 in my cases, because the previuos app was working using this)-.
BUT
I can't find out how should I forward inbound data from this port to the file that will handle packets.
Let's say :
I wrote a php file named : gettcp.php
The device will send data using 1.2.3.4 : 31334 (my host ip and port)
How server should find out incoming packets from 31334 have to go to gettcp.php ...???
What's the scenario ...?
AND what should I configure in my host? Should I use directadmin or my linux shell?

Dani AI

Generated

Short answer: the built‑in PHP web server (what suggested) and Apache/NGINX speak HTTP. Many GPS trackers, however, open a raw TCP socket and send NMEA or vendor frames — not HTTP. That mismatch is why the server logged an invalid/malformed HTTP request for . Decide first whether your tracker sends HTTP GET/POST or raw TCP. If it is HTTP, configure Apache to listen on the new port and use normal PHP. If it is raw TCP, you need a TCP listener (a small daemon) that accepts the socket, reads the raw lines, and passes them to your parser/DB code.

A minimal pattern (run under a supervisor or init script, not the PHP dev server) is to create a TCP socket and loop accepting connections, then validate and store each message. Example skeleton:

<?php
$addr = '0.0.0.0';
$port = 31334;
$server = stream_socket_server("tcp://{$addr}:{$port}", $errno, $errstr);
if (!$server) { error_log("socket failed: $errstr ($errno)"); exit(1); }

while ($conn = @stream_socket_accept($server)) {
    $raw = '';
    while (!feof($conn)) {
        $chunk = fgets($conn, 1024);
        if ($chunk === false) break;
        $raw .= $chunk;
    }
    // parse $raw, validate, insert via PDO prepared statements
    fclose($conn);
}
fclose($server);

Production tips: run that script as a supervised service (supervisord, monit, or an init script on CentOS 6), open the server port in the host firewall, bind to the correct interface (0.0.0.0 or the external IP), run as a non‑root user, log raw packets for debugging, and harden/parsing (length checks, rate limiting). For quick testing reproduce the device behavior with netcat from another machine.

Recommended Answers

All 4 Replies

If you run "php --help" you will see that one of the options is to run PHP directly as a web server (no LAMP stack required), which is the "-S <addr>:<port>" option. IE, you can do this directly in PHP by starting it with the correct address and port information. You should then get the data directly. Try this

php -S 1.2.3.4:31334 gettcp.php

directly from the command line. You can do it in the shell. I don't think you have to run it as admin. I used to do this to test my PHP code at Nokia. In some cases, we would just use it as-is and not bother with Apache or Ngnix.

You can also run it in the background, but that isn't necessary.

commented: Thanks, It would be great if you check my new answer +0

Thanks for your reply.

Is this possible that this command corrupt or misconfigure my current running php? Or it just change setting for this special port?

I'm asking because my site is running on the same server?

Well, I did test it and it seems OK temporarily, because when I hit Ctrl + c, the process will be gone.
May be I have to run this in the background, as you mentioned above.
I need this to be working permanently.
Either I have another problem that the command message says:

[Sat Dec  5 21:31:27 2015] 5.107.127.148:1334 Invalid request (Malformed HTTP request)

Can anyone help me to fix that?

Well, I would need to see the incoming http request to determine why it is "malformed". FWIW, once we were satisfied that the php code was working correctly, we could deploy it in a real web server (Apache) without problems, but you need to tell Apache that it needs to listen on the new port.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.