SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    socket server problem

    I have the folowing socket server:

    <?php
    include ("xml_parser_functions.php");

    function get_uid_by_username($username) {
    global $db1;
    $query = "SELECT id FROM users WHERE username = '$username'";
    $db1->query($query);
    if (!$db1->nf()) {
    $uid = 0;
    } else {
    $db1->next_record();
    $uid = $db1->f("id");
    }
    return $uid;
    }

    function get_uaccess_by_username($username) {
    global $db1;
    $query = "SELECT access FROM users WHERE username = '$username'";
    $db1->query($query);
    if (!$db1->nf()) {
    $uaccess = 0;
    } else {
    $db1->next_record();
    $uaccess = $db1->f("access");
    }
    return $uaccess;
    }

    error_reporting(E_ALL);

    set_time_limit(0);

    ob_implicit_flush();


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

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

    if (($ret = socket_listen($sock, 5)) < 0) {
    echo "socket_server.php line 61; socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
    socket_close($sock);
    }

    $i = $min_sock;
    do {
    if (($msgsock = socket_accept($sock)) < 0) {
    echo "socket_server.php line 72; socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
    socket_close($sock);
    break;
    }
    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 {
    if (false === ($buf = @socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
    echo "socket_server.php line 86; socket_read() failed: reason: " . socket_strerror($ret) . "\n";
    socket_close($msgsock);
    break 2;
    }
    if (!($buf = trim($buf))) {
    continue;
    }
    } while (!$buf);

    include("xml_parse.php");

    $username = $els["login"]["username"];
    $pass = $els["login"]["password"];
    $pass = md5($pass);

    $q = "SELECT * FROM users WHERE username = '$username' AND password = '$pass' AND active = '1'";
    $db->query($q);
    if (!$db->nf()) {
    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;
    } else {
    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) {
    echo "socket_server.php line 130; socket_create() failed: reason: " . socket_strerror($sock1) . "\n";
    }
    if (($sock2 = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
    echo "socket_server.php line 133; socket_create() failed: reason: " . socket_strerror($sock2) . "\n";
    }

    do {
    if ($i > $max_sock) $i = $min_sock;
    if (($ret = socket_bind($sock1, $net_address, $i)) < 0) {
    echo "socket_server.php line 140; socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
    $i++;
    } else {
    socket_close($sock1);
    $port1 = $i;
    $j = $i + 1;
    do {
    if ($j > $max_sock)$j = $min_sock;
    if (($ret = socket_bind($sock2, $net_address, $j)) < 0) {
    echo "socket_server.php line 150; socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
    $j++;
    } else {
    socket_close($sock2);
    $port2 = $j;
    exec ("php market_child_socket.php $port1 $rip $uid $uaccess >> log &");
    exec ("php send/market_xml_send.php $uid $port2 >> /var/log/steinx/market_xml_send.log &");
    break;
    }
    } while (true);
    $i = $j + 1;
    break;
    }
    } while (true);

    $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);
    }
    socket_close($msgsock);
    } while (true);

    socket_close($sock);
    ?>

    Time to time this socket server restarts with the folowind message:
    Warning: socket_read(): unable to read from socket [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:
    Warning: socket_bind(): unable to bind address [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.
    Last edited by mani_k; Feb 13, 2005 at 03:40.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •