How to get input from socket client

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



  Stream and networking programming seems like a rock on the way to
ZCE for most people, so I'm learning some socket examples before I sit
in the room for exam.
Here is the script for server

<?php
$socket = stream_socket_server("tcp://127.0.0.1:1037", $errno, $errstr);
if(!$socket)
{
	echo "$errstr ($errno) \n";
}
else
{
	while($conn = stream_socket_accept($socket))
	{
		echo "New connection \n";
		fwrite($conn, "Hello World \n");
		fwrite($conn, "Do you want to close server? Yes(Y)/No(N) \n");
		if(!feof($conn))
		{
			$answer = fread($conn, 1);
			if($answer == 'Y')
			{
				echo "User choose to close server. \n";
				break;
			}
			elseif($answer == 'N')
			{
				echo "Service continue... \n";
			}
			else
			{
				echo "Unknown answer '$answer'. \n";
			}
		}
		fclose($conn);
	}
	fclose($socket);
}

and the client

<?php
$socket = stream_socket_client('tcp://127.0.0.1:1037', $errno, $errstr);
if(!$socket)
{
	echo "$errstr ($errno) \n";
}
else
{
	$answer = "Y";// fgets(STDIN)
	fwrite($socket, $answer);
	while(!feof($socket))
	{
		echo fread($socket, 100);
	}
}

everything works, but if I write after read in client

<?php
$socket = stream_socket_client('tcp://127.0.0.1:1037', $errno, $errstr);
if(!$socket)
{
	echo "$errstr ($errno) \n";
}
else
{
	while(!feof($socket))
	{
		echo fread($socket, 100);
	}
	$answer = "Y";// fgets(STDIN)
	fwrite($socket, $answer);
}

server&client hangs after output and time out later.

Can any1 point out whats the reason and the more correct way to get
socket client input in socket server?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux