Php code for server listen to the port 29999

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ushachandran
    New Member
    • Sep 2006
    • 1

    #1

    Php code for server listen to the port 29999

    Hi

    Please let me know what is the error in the following code. I am doing a tracking project. My vehicle unit has a simcard with sms message stored on it. When the unit powers up it reads the sms and enables the GPRS module(xxxgprs. com is my APN). It establishes the GPRS TCP/IP connection to my server on port 29999.

    I created a php program which listens the port 29999 to receive the data transmitted by the unit.

    When I run this program, I could notice the status bar it is started executing, displays nothing. When I refresh the program again, I get the following error
    Warning: could not bind address[98].Address already in use.

    I do not know what error? If this program is wrong please give me a php code to listen to the port 29999 to receive the values.

    [code=php]
    #!/usr/local/bin/php –q

    <?php

    // Server IP address
    $address = "72.29.86.1 67";

    // Port to listen
    $port = 29999;

    $mysock = socket_create(A F_INET, SOCK_STREAM, SOL_TCP);

    socket_bind($my sock,$address, $port) or die('Could not bind to address');
    socket_listen($ mysock, 5);
    $client = socket_accept($ mysock);

    // read 1024 bytes from client
    $input = socket_read($cl ient, 1024);

    // write received data to the file
    writeToFile('ab c.txt', $input);

    socket_close($c lient);
    socket_close($m ysock);
    ?>

    <?
    /**
    * write string to file
    */
    function writeToFile($st rFilename, $strText)
    {
    if($fp = @fopen($strFile name,"w "))
    {
    $contents = fwrite($fp, $strText);
    fclose($fp);
    return true;
    }else{
    return false;
    }

    }
    ?> [/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    I have a dedicated server with Linux installed using a php 4.4.3.



    Regards
    Ushachandran
    Last edited by pbmods; Jun 10 '07, 03:03 PM. Reason: Added code tags.
  • awdigital
    New Member
    • Jun 2007
    • 1

    #2
    The script worked fine for me, although it of course quits after the data is received. I adapted the code into a loop so that after manipulating the data received from the tracker it keeps listening for the next message.

    I am also doing a tracking project so if you want to swap ideas feel free to get in touch.

    Cheers

    Adam Williams
    AW Digital Consulting

    Comment

    Working...