Tema: socket php
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/01/2014, 06:02
pjnoguerol
 
Fecha de Ingreso: enero-2014
Mensajes: 6
Antigüedad: 10 años, 3 meses
Puntos: 0
Respuesta: socket php

Tambien tengo este otro enfoque pero me choco contra el mismo problema, es que necesito conectar una pistola de lector de codigo de barras con windows CE con mysql y php es el lenguaje que se, si sabeis de otro lenguaje que no sea muy dificil u otra forma, si me la indicais os lo agradeceria mucho


Código PHP:
error_reporting(~E_ALL);
set_time_limit (0);
 
$address "172.16.10.75";
$port 4545;
$max_clients 10;

function 
baja ($ubicacion){
    
    
    return 
$ubicacion;
    
}

function 
socket_read_normal($socket$end=array("\n""\r")){
    if(
is_array($end)){
        foreach(
$end as $k=>$v){
            
$end[$k]=$v{0};
        }
        
$string='';
        while(
TRUE){
            
$char=socket_read($socket,1);
            
$string.=$char;
            foreach(
$end as $k=>$v){
                if(
$char==$v){
                    return 
$string;
                }
            }
        }
    }else{
        
$endr=str_split($end);
        
$try=count($endr);
        
$string='';
        while(
TRUE){
            
$ver=0;
            foreach(
$endr as $k=>$v){
                
$char=socket_read($socket,1);
                
$string.=$char;
                if(
$char==$v){
                    
$ver++;
                }else{
                    break;
                }
                if(
$ver==$try){
                    return 
$string;
                }
            }
        }
    }
}
 
if(!(
$sock socket_create(AF_INETSOCK_STREAM0)))
{
    
$errorcode socket_last_error();
    
$errormsg socket_strerror($errorcode);
     
    die(
"Couldn't create socket: [$errorcode] $errormsg \n");
}
 
echo 
"Socket created \n";
 
// Bind the source address
if( !socket_bind($sock$address $port) )
{
    
$errorcode socket_last_error();
    
$errormsg socket_strerror($errorcode);
     
    die(
"Could not bind socket : [$errorcode] $errormsg \n");
}
 
echo 
"Socket bind OK \n";
 
if(!
socket_listen ($sock 10))
{
    
$errorcode socket_last_error();
    
$errormsg socket_strerror($errorcode);
     
    die(
"Could not listen on socket : [$errorcode] $errormsg \n");
}
 
echo 
"Socket esta en modo escucha \n";
 
echo 
"Esperando nuevas conexiones entrantes... \n";
 
//array of client sockets
$client_socks = array();
 
//array of sockets to read
$read = array();
  
$y=0;
//start loop to listen for incoming connections and process existing connections
while (true
{
    
//prepare array of readable client sockets
    
$read = array();
     
    
//first socket is the master socket
    
$read[0] = $sock;
     
    
//now add the existing client sockets
    
for ($i 0$i $max_clients$i++)
    {
        if(
$client_socks[$i] != null)
        {
            
$read[$i+1] = $client_socks[$i];
            
        }
    }
     
    
//now call select - blocking call
    
if(socket_select($read $write $except null) === false)
    {
        
$errorcode socket_last_error();
        
$errormsg socket_strerror($errorcode);
     
        die(
"Could not listen on socket : [$errorcode] $errormsg \n");
    }
     
    
//if ready contains the master socket, then a new connection has come in
    
if (in_array($sock$read)) 
    {
        for (
$i 0$i $max_clients$i++)
        {
            if (
$client_socks[$i] == null
            {
                
$client_socks[$i] = socket_accept($sock);
                 
                
//display information about the client who is connected
                
if(socket_getpeername($client_socks[$i], $address$port))
                {
                    echo 
"Client $address : $port esta conectado. \n";
                }
                 
                
//Send Welcome message to client
               
$msg "Bienvenido cliente {$key[0]} \n\r";
         
$msg .= "1.-Crear Paleta\n\r";
         
$msg .= "2.-Ubicar Paleta\n\r";
         
$msg .= "exz.-Salir\n\r";
                
socket_write($client_socks[$i] , $msg);
                
                break;
            }
        }
    }

    
//check each client if they send any data
    
for ($i 0$i $max_clients$i++)
    {
        
        if (
in_array($client_socks[$i] , $read))
        {
                    
                   
            if (
false === ($input socket_read_normal($client_socks[$i]))) {
            echo 
"socket_read() failed: reason: " socket_strerror(socket_last_error($msgsock)) . "\n";
            break 
2;
        }
             
           
            echo 
"el cliente $i dice : ";
            echo 
"$input";
             
            
//send response to client
            
socket_write ($client_socks[$i], "cliente $i dice: ");
            
socket_write ($client_socks[$i], $input);
            
            
socket_write ($client_socks[$i], "\r");
            
$y++;
        }
    }