Ver Mensaje Individual
  #4 (permalink)  
Antiguo 26/10/2011, 17:09
vito25
 
Fecha de Ingreso: marzo-2008
Mensajes: 63
Antigüedad: 18 años, 8 meses
Puntos: 0
Respuesta: Problema con Socket Php - No Escucha

El Código del servidor es:

Código PHP:
<?
// ponemos unos variables
$host = "127.0.0.1";
$port = 1234;
// no se vensera el tiempo
set_time_limit(0);
// crea el socket

while(true){

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("No se pudo crear el socket\n");
// bind el socket al port
$result = socket_bind($socket, $host, $port) or die("No se pudo hacer el bind\n");
// Comiensa a escuchar las coneciones
$result = socket_listen($socket, 3) or die("No se pudo escuchar las coneciones\n");
// acepta la conecion
$spawn = socket_accept($socket) or die("No se pudo aseptar la conecion\n");
// Lee el imput del cliente
$input = socket_read($spawn, 1024) or die("No se pudo leer el imput\n");

echo 
$input;



// limpia el imput del string
$input = trim($input);
// Checa por donde llego el imput y lo manda para atras
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("No se pudo escribir el imput\n");
// cierra la conecion
socket_close($spawn);
socket_close($socket);
}
?>

Código Cliente

Código PHP:
<?php 
/* 
Ejemplo 2, conexión via socket 
*/ 

// DEFINICIÓN DE VARIABLES 
$puerto = 1234; 
$host = "127.0.0.1"; 

// CREANDO EL SOCKET: (IP PROTOCOL[IPV4], TYPE SOCKET[TCP], PROTOCOL[TCP]) 
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
$conexion = socket_connect($socket, $host, $puerto); 

// DEFINICIÓN DE VARIABLES 
$buffer = "enviando parametro"; 
$salida = ' '; 

// ESCRIBIENDO EL BUFFER EN EL SOCKET 
socket_write($socket, $buffer); 

// LEYENDO RESPUESTA DEL SOCKET 
print "leyendo...\n"; 
while (
$salida = socket_read($socket, 2048)){ 
echo 
$salida;} 

// CERRANDO LA CONEXIÓN 
socket_close($socket); 

?>

Al servidor agregue un while(true), eso está correcto???

Última edición por vito25; 26/10/2011 a las 17:35