PHP Socket Client - Issue with while (!feof($fp)) loop

Posted on 2009-04-17
Last Modified: 2013-12-13

I'm writing a PHP script to communicate with an application through TCP/IP.

I'm sending queries to the application and I need to read the data the server sends back.

My problem is with the while(end of file) loop. The script freezes when I'm using it. It works with a simple For loop but I need a dynamic loop as the numbers of records to return will vary with time.

Here is an exemple of the server response

--After Connection:
Avantage - Intýgration 16.6,Server 4.3,067738

--After login

--After query

I need to parse all the data after the query in an array so I can use those value to populate a drop down list in a form.

Any ideas on how to fix the loop?
        //Create the socket connection

        $fp = fsockopen('XXXX.XXXX.XXXX.XXXX', 2131, $errno, $errstr);


        //LOGIN query

	$message = "LOGIN,10,OPERATEUR,****\n";


	//Data query

	$message2 = "RQ01,R01,,CNT,CONUM,,/COINACTIF==\"F\",CONUM\n";


        if (!$fp) 


            echo "$errstr ($errno)<br />\n";




            // get the welcome message 

            $line1 = fgets($fp, 1024);

            echo 'got first line:  '.$line1;


            // write the login query to the socket

            fwrite($fp, $message, strlen($message)) or die('Error sending the input string.');

            echo '<br/>sent msg:        '.$message;


            // get the result

            $result .= fgets($fp, 1024);

            echo "<br/>got response: $result\n";


	    // write the data query to the socket

            fwrite($fp, $message2, strlen($message2)) or die('Error sending the input string.');

            echo '<br/>sent msg:        '.$message2;


			// read all records returned by the server - SCRIPT FREEZES HERE


			$projectNo = array();

			$cpt = 0;


			while (!feof($fp)) 


				$projectNo[$cpt] = fgets($fp, 1024);







				echo $Array[$i]; 




       //close the connection

        echo 'closing connection'."\n";


        echo 'connection closed';

Open in new window

Question by:EnDscx
    1 Comment

    Accepted Solution

    Well I solved my own problem using the code below.

    By putting every record in a string variable in the loop and checking if that string contains the word "END" everytime it loops the script will exit the loop when the server sends the line containing END which means it sent all the records (which it always do after every query).
        $noProjet = fgets($fp, 1024);
        $projectNo[$cpt] = $noProjet;
    while(substr_count($noProjet,"END") != 1);
    //Looping through the array I can see all the records I needed
    //Easy to put those in a drop down list now :)
        echo $projectNo[$i]; 
        echo "<br/>";			

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now