I am trying to connect to a server using sockets, and echo the data back. The server I am trying to connect to below automatically sends back a string of text when a client connects. The client does not have to send anything for that string, so upon connecting, there should be a string of data echoed. Here is my code so far:

<?

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$connection = socket_connect($socket,'207.38.11.34',29900);
while($data = socket_read($socket,2046,PHP_NORMAL_READ))
{
echo $data;
}

?>

My problem is that when I load the page with this code, it continously loads and never displays any data. I was reading some tutorials on where you have to use \r\n, but I have no idea where to put that without messing up my code.

If I take out this code:

while($data = socket_read($socket,2046,PHP_NORMAL_READ))
{
echo $data;
}

.. the page loads just fine. So I basically need a simple way to echo the data received.

Thanks.

    The bit about sending \r\n is probably just to verify that the server knows you're there. If the server truly needs to input as you say, try this:

    <?php
    
    $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    $connection = socket_connect($socket,'207.38.11.34',29900);
    
    for($i = 0; $i <= 3; $i++) {
    	$data = socket_read($socket, 2046, PHP_NORMAL_READ);
    
    if($data === FALSE)
    	'Error: ' . socket_last_error($socket) . '<br/>';
    else if($data === '')
    	sleep(1);
    else
    	break;
    }
    
    if(empty($data))
    	echo 'Could not retrieve data.<br/>';
    else
    	echo $data;
    
    ?>

      Thanks for the reply, but I am still getting a costant page loading with that code you provided. Any other suggestions?

        Perhaps the output is being buffered while it is being read in; does putting a call to [man]flush/man after the echo help?

          Still nothing. Is there a different way to echo received data?

            are you sure the socket is returning any data at all? try dropping the 2nd arg of socket_read to something much smaller like 1 or 8 or 16 or something.

            it also might be helpful to set up some code to write a log file so you can get a better idea of what's going on.

              When you connect to this particular server and port, it automatically sends out a string that should look something like this: \lc\1\challenge\ABCDEFGHIJ\id\1\final[/I]

              I know this because I used visual basic's winsock control to connect and display any received data in a text box (basically what we are trying to do here with php). So i know when you connect with a php socket, it should also send the server a string like the one above.

              I think we just need to find the correct way to display received data. I tried sneakyimps method, with and without the flush(); command weedpacket recommended, and it didnt work. I also tried values of 1, 8 and 16.

              If any of you have a webhost, you could create the php file and try to get what i am looking for, and if you get it to send back data, you could let us know what code you used.

              But you shouldnt have to send any data, it should automatically send data upon connecting.

              Thanks for the help so far though, i really appriciate this! 😃

                Yup, it works - but only do ONE read, as after that your socket blocks, and that's why you're sitting there with no output.

                \lc\1\challenge\ISMJWTXDXO\id\1\final\

                Each output gets a different string of chars in that main bit. Look into [man]socket_select[/man] for knowing when sockets are ready for reading / writing.

                Man, I remember the horror I had learning the ins and outs of sockets. It is not a fun learning curve.

                You can always cheat and do this:

                define('ADDRESS', '207.38.11.34');
                define('PORT', 29900);
                
                $s = fsockopen(ADDRESS, PORT, $err, $str, 10);
                
                echo 'FSock Method: '.fread($s, 512).'<br /><br />';
                  Drakla wrote:

                  You can always cheat and do this:

                  define('ADDRESS', '207.38.11.34');
                  define('PORT', 29900);
                  
                  $s = fsockopen(ADDRESS, PORT, $err, $str, 10);
                  
                  echo 'FSock Method: '.fread($s, 512).'<br /><br />';

                  Wahoo! That works perfectly, and is EXACTLY what I wanted. Thank you 🆒

                    Write a Reply...