socket server problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mani_k
Forum Newbie
Posts: 1
Joined: Fri Feb 11, 2005 5:46 am

socket server problem

Post by mani_k »

I have the folowing socket server:

Code: Select all

<?php
$start = date("Y-m-d H:i:s");
echo "Skriptyt e startiran: $start\n";
include("../includes/config.inc.php");

//Iztrivame wsichki online users - initialisation
//$db->query("TRUNCATE online_users");

// VKLIUCHVAME FUNKCIITE ZA OBRABOTKA NA XML.               
include ("xml_parser_functions.php");

$file = "xml.log";

// VZEMAME ID-to NA USER-a.
function get_uid_by_username($username) &#123;
    global $db1;
    $query = "SELECT id FROM users WHERE username = '$username'";
    $db1->query($query);
    if (!$db1->nf())    &#123;
        $uid = 0;
    &#125; else &#123;
        $db1->next_record();
        $uid = $db1->f("id");
    &#125;
    return $uid;
&#125;

// VZEMAME ACCESS-a NA USER-a.
function get_uaccess_by_username($username) &#123;
    global $db1;
    $query = "SELECT access FROM users WHERE username = '$username'";
    $db1->query($query);
    if (!$db1->nf())    &#123;
        $uaccess = 0;
    &#125; else &#123;
        $db1->next_record();
        $uaccess = $db1->f("access");
    &#125;
    return $uaccess;
&#125;

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();


if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) &#123;
   echo "socket_server.php line 52; socket_create() failed: reason: " . socket_strerror($sock) . "\n";
&#125;

if (($ret = socket_bind($sock, $net_address, $portnr)) < 0) &#123;
   echo "socket_server.php line 56; socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
     socket_close($sock);
&#125;

if (($ret = socket_listen($sock, 5)) < 0) &#123;
   echo "socket_server.php line 61; socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
     socket_close($sock);
&#125;
// Iztrivame vsichki zapisi ot tablica "online_users" ako ima takiva.
$q = "DELETE FROM online_users";
$db->query($q);

// Inicializirame broqcha, chrez koito tyrsim svobodni portove.
$i = $min_sock;
do &#123;
    if (($msgsock = socket_accept($sock)) < 0) &#123;
        echo "socket_server.php line 72; socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
        socket_close($sock);
      break;
    &#125;
    /* Send instructions. */
    echo "New user connected!\n";
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'exit'.\n";
    socket_write($msgsock, $msg, strlen($msg));
    
    $msg_u_p = "\nEnter user and pass :";
    socket_write($msgsock, $msg_u_p, strlen($msg_u_p));
    do &#123;
        if (false === ($buf = @socket_read($msgsock, 2048, PHP_NORMAL_READ))) &#123;
            echo "socket_server.php line 86; socket_read() failed: reason: " . socket_strerror($ret) . "\n";
            socket_close($msgsock);
            break 2;
        &#125;
        if (!($buf = trim($buf))) &#123;
            continue;
        &#125;
    &#125; while (!$buf);
           
    // VZIMAME IP-to NA KLIENTA.
    socket_getpeername ($msgsock, $rip);
    $rip = trim($rip);
    
    // ZAPISVAME VHODQSHTATA INFORMACIQ V LOG FILE.
    $f = fopen($file, "a");
    $message = "INPUT :\n";
    $message .= $buf;
    $message .= "\n\n";
    fwrite($f, $message, strlen($message));
    fclose($f);
    
    // ZAPOCHVAME PARSVANETO NA XML-A, KOITO SE NAMIRA V BUFERA.
    include("xml_parse.php");
    // KRAI NA PARSVANETO NA XML-A, KOITO SE NAMIRA V BUFERA.
    
    $username = $els&#1111;"login"]&#1111;"username"];
    $pass = $els&#1111;"login"]&#1111;"password"];
    $pass = md5($pass);
    
    $q = "SELECT * FROM users WHERE username = '$username' AND password = '$pass' AND active = '1'";
    $db->query($q);
    if (!$db->nf()) &#123;
        echo "Greshen Username ili Parola!\n";
        $out = "GRESHEN USERNAME OR PASSWORD !";
        socket_write($msgsock, $out, strlen($out));
        $msg_u_p = "\nCONNECTION IS CLOSED !";
        socket_write($msgsock, $msg_u_p, strlen($msg_u_p));
        continue;
    &#125; else    &#123;
        echo "Uspeshen whod!\n";
        $uid = get_uid_by_username($username);
        $uaccess = get_uaccess_by_username($username);
        if (($sock1 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) &#123;
            echo "socket_server.php line 130; socket_create() failed: reason: " . socket_strerror($sock1) . "\n";
        &#125;
        if (($sock2 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) &#123;
            echo "socket_server.php line 133; socket_create() failed: reason: " . socket_strerror($sock2) . "\n";
        &#125;
       
        // NAMIRAME PYRVITE 2 SVOBODNI PORT-a.
        do &#123;
            if ($i > $max_sock) $i = $min_sock;
            if (($ret = socket_bind($sock1, $net_address, $i)) < 0) &#123;
                echo "socket_server.php line 140; socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
                $i++;
            &#125; else &#123;
                socket_close($sock1);
                // PORT, NA KOITO CLIENTA SHTE PRASHTA ZAQVKI NA SERVER-a I SHTE POLUCHAVA OTGOVOR.
                $port1 = $i;
                $j = $i + 1;
                do &#123;
                    if ($j > $max_sock)$j = $min_sock;
                    if (($ret = socket_bind($sock2, $net_address, $j)) < 0) &#123;
                        echo "socket_server.php line 150; socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
                        $j++;
                    &#125; else &#123;   
                        socket_close($sock2);
                        // PORT, NA KOITO CLIENTA SHTE POLUCHAVA DANNI NA VSEKI 20 SEC.
                        $port2 = $j;
                        // STARTIRANE NA SCRIPTA, KOITO SHTE RABOTI S CLIENT-a.
                        $q = "DELETE FROM online_users WHERE uid = '$uid'";
                        $db->query($q);
                        $q = "INSERT INTO online_users (uid, ip, port1, port2, connected) VALUES ('$uid', '$rip', '$port1', '$port2', 'false')";
                        $db->query($q);
                        echo "Startirane na market_child_socket.php  $port1 $rip $uid $uaccess >> log &\n";
                        exec ("php market_child_socket.php $port1 $rip $uid $uaccess >> log &");
                        echo "Startirane na market_xml_send.php $uid >> /var/log/steinx/market_xml_send.log &\n";
                        exec ("php send/market_xml_send.php $uid $port2 >> /var/log/steinx/market_xml_send.log &");
                        break;
                    &#125;
                &#125; while (true);
                $i = $j + 1;
                break;
            &#125;
        &#125; while (true);
       
        // PRASHTAME XML NA KLIENTA I ZAPISVAME IZHODQSHTATA INFORMACIQ V LOG FILE.
        $xml_out = "<ports><port1>$port1</port1><port2>$port2</port2></ports>";              
        socket_write($msgsock, $xml_out, strlen($xml_out));
        $msg_u_p = "\nCONNECTION IS CLOSED !";
        socket_write($msgsock, $msg_u_p, strlen($msg_u_p));
        $f = fopen($file, "a");
         $message = "OUTPUT :\n";
         $message .= $xml_out;
         $message .= "\n\n";
         fwrite($f, $message, strlen($message));
         fclose($f);
         $xml_out = "";
        unset ($els);   
    &#125;   
    socket_close($msgsock);
&#125; while (true);

socket_close($sock);
?>
Time to time this socket server restarts with the folowind message:

Code: Select all

Warning: socket_read(): unable to read from socket &#1111;104]: Connection reset by peer in /home/pub/market/xml_files/market_socket_server.php on line 86
socket_server.php line 86; socket_read() failed: reason: Operation not permitted
after restart it shows the folowing message:

Code: Select all

Warning: socket_bind(): unable to bind address &#1111;98]: Address already in use in /home/pub/market/xml_files/market_socket_server.php on line 56
and i have to disconnect all the clients that are curently connected and restart the server again...

Cann someon help me with this server.... to make it more reliable. :)


feyd | learn to use formatting where available
Post Reply