Foros del Web » Programando para Internet » PHP »

Comportamiento extraño en socker server simple al conectar a base de datos

Estas en el tema de Comportamiento extraño en socker server simple al conectar a base de datos en el foro de PHP en Foros del Web. Hola a todos, tengo un extraño problemilla con el servidor de socket que estoy probando en php, el server funciona correctamente pero cuando hago una ...
  #1 (permalink)  
Antiguo 22/04/2011, 11:35
 
Fecha de Ingreso: junio-2010
Mensajes: 8
Antigüedad: 13 años, 10 meses
Puntos: 0
Comportamiento extraño en socker server simple al conectar a base de datos

Hola a todos, tengo un extraño problemilla con el servidor de socket que estoy probando en php, el server funciona correctamente pero cuando hago una conexión a mi bae de datos, el servidor crea nuevos "clientes fantasma" los cuales realmente no existen, he creado una condición para que cuando un cliente escriba !database el script se conecte a la base de datos y se pueda ver el problema, por ejemplo:

Si abro el servidor y conecto 1 cliente via telnet esta será registrado como Resource id #3 que es siempre el primero que se conecta, si conecto otro cliente más este será Resource id #4 y así consecutivamente pero si probamos a hacer unas conexiones a la base de datos escribiendo desde cualquier cliente !database 4 veces crea un cliente fantasma en cada conexión, es decir, si abro otra sesión de telnet y conecto un nuevo cliente después de haber hecho 4 conexiones a la base de datos, este nuevo cliente es ahora el cliente Resource id #9 en vez de el cliente Resource id #5 que debería haber sido.

Adjunto el código del server, estoy probando con un socket script server bastante popular de Raymond Fain.

Agradecería mucho que alguien pudiese ayudarme.

Un saludo.

Código PHP:
#!/usr/local/bin/php -q
<?php

/*
    Raymond Fain
    Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com
    For any questions or concerns, email me at [email protected]
    or simply visit the site, www.php.net, to see if you can find an answer.
*/

// E_ALL is a named constant (bit of 2047)
// error_reporting(E_ALL) ensures any errors will be displayed
error_reporting(E_ALL);

// set_time_limit() is set to 0 to impose no time limit
// set_time_limit(0);

// ob_implicit_flush() will push the whole message to flash (including any extra markups like terminating zeros)
ob_implicit_flush();

// address set to internal port for the socket to bind to and port set to something above 1024 so flash will connect
$address 'xx.xx.xx.xx';
$port 12744;

//---- Function to Send out Messages to Everyone Connected ----------------------------------------
// $allclient is an array of resource id's that php will refer to for the sockets
// $socket contains the resource id for the flash client who sent the message
// $buf is just the message they sent, we are sending to everyone using a foreach loop

function send_Message($allclient$socket$buf) {
   foreach(
$allclient as $client) {

        
// socket_write uses the $client resource id (which calls on the socket) and sends it our message (which is the second flag)

       
socket_write($client"$socket wrote: $buf");

   }
}

//---- Start Socket creation for PHP 5 Socket Server -------------------------------------

// socket_create function is pretty simple, depending on what you want it to do, you can just rig it for that,
// AF_INET is a "kind of" default address/protocol family that I have used,
// then socket_create takes and uses SOCK_STREAM, another default available socket type, 
// and uses a SOL_TCP protocol to have a guaranteed data sending. 
if (($master socket_create(AF_INETSOCK_STREAMSOL_TCP)) < 0) {
   echo 
"socket_create() failed, reason: " socket_strerror($master) . "\n";
}

// this little beauty is hard to understand, but very useful to our kind of chat
// we want to be able to reuse our sockets, so we set our $master socket with a level at SOL_SOCKET, 
// with it reusable (SO_REUSEADDR), and a "1" in the mixed optval spot to make it true.
socket_set_option($masterSOL_SOCKET,SO_REUSEADDR1);

// now we need to bind our socket to the address and port specified.
if (($ret socket_bind($master$address$port)) < 0) {
   echo 
"socket_bind() failed, reason: " socket_strerror($ret) . "\n";
}

// to allow our socket server to listen for incoming calls made by our flash client, we need
// to use socket_listen on our $master socket. The next flag indicates that only 5 calls at a time.
// any others will be refused until there is sufficient room to process a new call.
if (($ret socket_listen($master200)) < 0) {
   echo 
"socket_listen() failed, reason: " socket_strerror($ret) . "\n";
}


// this will allow an easy way to keep track of sockets connected to our server
$read_sockets = array($master);

//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true) {
    
   
$changed_sockets $read_sockets;
   
   
// socket_select is used here to see if there were any changes with the sockets that were connected before
   // $num_changed_sockets is here so you can check to see if the change is true or not with a subsequent function
   
$num_changed_sockets socket_select($changed_sockets$write NULL$except NULLNULL);
   
   
// we run a foreach function on $changed_sockets to see whether it needs to be added (if new), determine the message it sent
   // or determine if there was a socket disconnect
   
foreach($changed_sockets as $socket) {
           
        
// now if the $socket currently being checked is the $master socket, we need to run some checks
        // if not then we will skip down to else and check the messages that were sent
       
if ($socket == $master) {
               
            
// socket_accept will accept any incoming connections on $master, and if true, it will return a resource id
            // which we have set to $client. If this did not work then return an error, but if it worked, then add in 
            // $client to our $read_sockets array at the end.
           
if (($client socket_accept($master)) < 0) {
               echo 
"socket_accept() failed: reason: " socket_strerror($msgsock) . "\n";
               continue;
           }
           else {
               
array_push($read_sockets$client);
           }
       }
       else {
       
               
// socket_recv just receives data from $socket as $buffer, with an integer length of 2048, and a mystery flag set to 0
            // that mystery flag has no real documentation so setting it to 0 will solve it as others have done.
            // we've also set it as a var $bytes in case we need to ensure data sending with socket_write, which is optional
           
$bytes socket_recv($socket$buffer20480);
           
           
           
$index array_search($socket$read_sockets);
           
           
// if the $bytes we have is 0, then it is a disconnect message from the socket
           // we will just search for it as an index and unset that socket from our $read_sockets and
           // finish up with using socket_close to ensure it is closed off
           
if ($bytes == 0) {
               unset(
$read_sockets[$index]);
               
socket_close($socket);
           }
           else{
               
$buffer_trimmed trim($buffer);
               if (
$buffer_trimmed == '!parar_server') {
                   
socket_close($master);
                   break 
2;
                }
                
                elseif (
$buffer_trimmed == '!database') {
                    
                  
$conexion mysql_connect("localhost""USER""PASSWORD");
                  
mysql_select_db("DATABASE"$conexion);
                  
                  
mysql_close($conexion);   
                }
                
                
// we need to make sure $read_sockets isn't messed with, so setting up a new variable called $allclients
                // will ensure this. We then shift the array so that our $master socket is not included to the count when
                // we send our data to all the other sockets in $allclients.
                  
$allclients $read_sockets;
                   
array_shift($allclients);
                
                
// just a simple call on our premade function 'send_Message' with $allclients, $socket (current one), and $buffer (the message)
                   
send_Message($allclients$socket$buffer);
                   
//send_Message($index, $buffer);
            
}
       }
      
   }
}

Última edición por jpinedo; 11/05/2011 a las 03:58
  #2 (permalink)  
Antiguo 22/04/2011, 14:45
 
Fecha de Ingreso: enero-2010
Mensajes: 191
Antigüedad: 14 años, 4 meses
Puntos: 7
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

Me ha sucedido que mysql_close, no cierra en realidad la conexion, proba con:

Código PHP:
$conexion NULL
Despues de mysql_close y sin mysql_close, para ver los resultados, de todas maneras, el numero de socket puede ser por otra cosa, chequea que realmente esten creado y no se hallan cerrado.

[bash]
/proc/net/tcp -a
[/bash]

Lo otro es que no cierres la conexion y recorda de utilizar autoconnect y/o conexiones persistentes
  #3 (permalink)  
Antiguo 25/04/2011, 16:31
 
Fecha de Ingreso: junio-2010
Mensajes: 8
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

Hola, abcdefghi, muchas gracias por tu ayuda,

El cierre de la conexión a mi me lo hace bien, de hecho el server antes se me caia dando el error "Mysql server has gone" porque no cerraba bien la conexión pero ahora va bien.

Puede ser que el número de socket suba pero se estén cerrando porque no noto carga aunque el contador suba mucho el server sigue funcionando bien, ¿como puedo comprobar si el socket existe o ya no es válido?

Saludos y muchas gracias por la ayuda.
  #4 (permalink)  
Antiguo 25/04/2011, 20:10
 
Fecha de Ingreso: junio-2010
Mensajes: 8
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

He estado haciendo unas pruebas y efectivamente aunque suba el contador de sockets estos no estan abiertos, es facil comprobarlo recorriendo el array $allclients.

Saludos y gracias por la ayuda.
  #5 (permalink)  
Antiguo 03/05/2011, 06:45
 
Fecha de Ingreso: junio-2010
Mensajes: 8
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

Tengo otro problemilla con el server, de vez en cuando me da el siguiente error y se cierra:

Cita:
PHP Warning: socket_recv() [<a href='function.socket-recv'>function.socket-recv</a>]: unable to read from socket [54]: Connection reset by peer in /usr/XXX/gs1.php on line XXX
La linea a la que se refiere es esta:

Cita:
$bytes = socket_recv($socket, $buffer, 2048, 0);
¿Alguien puede decirme como prevenir este error?

Saludos y gracias.

Última edición por Gush_ALK; 03/05/2011 a las 12:07
  #6 (permalink)  
Antiguo 04/05/2011, 04:56
 
Fecha de Ingreso: junio-2010
Mensajes: 8
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

He probado con esto
Cita:
if (false !== ($bytes = socket_recv($socket, $buffer, 2048, 0))) { }
pero sigue dando el error y echando el server abajo.

Seguro que se puede prevenir de alguna forma tonta pero no se cual es.
  #7 (permalink)  
Antiguo 06/05/2011, 08:47
 
Fecha de Ingreso: enero-2010
Mensajes: 191
Antigüedad: 14 años, 4 meses
Puntos: 7
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

Cita:
Iniciado por Gush_ALK Ver Mensaje
socket [54]: Connection reset by peer
Hay una infinidad de problemas.

Lo más usual es problema con la conexión, otras causas serían que el servidor este sobrecargado o que hallas alcanzado un timeout, tambien si es un cliente en flash es que el mismo se haya ido de la página.

La solución, chapucera, es ejecutar desde el cliente otra vez hasta alcanzar el estado correcto y con el tiempo encontrar la solución, no suelen ser fáciles de solucionar, lamento no poder ayudarte más.
  #8 (permalink)  
Antiguo 11/05/2011, 03:25
 
Fecha de Ingreso: junio-2010
Mensajes: 8
Antigüedad: 13 años, 10 meses
Puntos: 0
Respuesta: Comportamiento extraño en socker server simple al conectar a base de datos

Ok, muchas gracias, revisaré bien todo el código a ver por que puede ser.

Etiquetas: comportamiento, extraño, server, simple
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:09.