PHP. socket_read с 25 порта (smtp) выполняется 5(!) минут
Код:
class SMTPclient{
private $address;
private $port;
private $sock;
function Init($address="localhost", $port=25)
{
$this->address = $address;
$this->port = $port;
echo 'Create socket ...';
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->sock < 0)
throwException('socket_create() failed: ');
else
echo "OK\n";
}
function Destroy()
{
if (isset($this->sock)) {
echo 'Close socket ... ';
socket_close($this->sock);
echo "OK\n";
}
}
function setAddress( $address ){ $this->address = $address; }
function getAddress( $address ){ return $this->address; }
function setPort( $port ){ $this->port = $port; }
function getPort( $port ){ return $this->port; }
function Connect()
{
echo "Connecting to socket at $this->address : $this->port ... ";
$result = socket_connect($this->sock, $this->address, $this->port);
if ($result === false)
throwException('socket_connect() failed: ');
else echo "OK\n";
}
function ReadResponse( $len=256 )
{
$out = socket_read($this->sock, $len);
return $out;
}
function SendRequest($msg)
{
socket_write($this->sock, $msg, strlen($msg));
}
}
private $address;
private $port;
private $sock;
function Init($address="localhost", $port=25)
{
$this->address = $address;
$this->port = $port;
echo 'Create socket ...';
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->sock < 0)
throwException('socket_create() failed: ');
else
echo "OK\n";
}
function Destroy()
{
if (isset($this->sock)) {
echo 'Close socket ... ';
socket_close($this->sock);
echo "OK\n";
}
}
function setAddress( $address ){ $this->address = $address; }
function getAddress( $address ){ return $this->address; }
function setPort( $port ){ $this->port = $port; }
function getPort( $port ){ return $this->port; }
function Connect()
{
echo "Connecting to socket at $this->address : $this->port ... ";
$result = socket_connect($this->sock, $this->address, $this->port);
if ($result === false)
throwException('socket_connect() failed: ');
else echo "OK\n";
}
function ReadResponse( $len=256 )
{
$out = socket_read($this->sock, $len);
return $out;
}
function SendRequest($msg)
{
socket_write($this->sock, $msg, strlen($msg));
}
}
Следующим образом создаём объект и пытаемся поздороваться с сервером (sendmail):
Код:
header('Content-Type: text/plain;');
error_reporting(E_ALL ^ E_WARNING);
set_time_limit(0);
ob_implicit_flush();
$smtp = new SMTPclient("localhost", 25);
try {
$smtp->Init();
$smtp->Connect();
echo $smtp->ReadResponse(256);
$smtp->SendRequest("EHLO localhost");
echo $smtp->ReadResponse(256);
$smtp->Destroy();
} catch (Exception $e) {
echo "\nError: ".$e->getMessage();
}
error_reporting(E_ALL ^ E_WARNING);
set_time_limit(0);
ob_implicit_flush();
$smtp = new SMTPclient("localhost", 25);
try {
$smtp->Init();
$smtp->Connect();
echo $smtp->ReadResponse(256);
$smtp->SendRequest("EHLO localhost");
echo $smtp->ReadResponse(256);
$smtp->Destroy();
} catch (Exception $e) {
echo "\nError: ".$e->getMessage();
}
Чтение первого ответа (после коннекта) имеет место мгновенно.
Второе-же обращение к $smtp->ReadResponse длится ровно 5 минут.
Поиски в гугле ни к чему толком не привели.