Я новичок в программировании на PHP-сокет, и я нашел пример для экспериментов, но когда я разговариваю с моим сервером, требуется время, чтобы получить ответ до закрытия серверного сокета.Почему для получения ответа от сервера требуется MINUTE?
У меня есть следующий код: SERVER.php
<?php
$host = "127.0.0.1";
$port = 1234;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Как заставить его реагировать сразу? Спасибо
Когда я запускаю клиентский код в терминале, я сразу же получаю ответ. Но когда я добавляю текстовое поле для его запуска из браузера, для получения ответа на мой браузер требуется ровно 1 минута.
Если вам нужно посмотреть на мой CLIENT.php Вот она ...
<html>
<head>
</head>
<body>
<form action="<? echo $PHP_SELF; ?>" method="post">
Enter some text:<br>
<input type="Text" name="message" size="15"><input type="submit" name="submit" value="Send">
</form>
<?php
if (isset($_POST['submit']))
{
// form submitted
// where is the socket server?
$host="127.0.0.1";
$port = 1234;
// open a client connection
$fp = fsockopen ($host, $port, $errno, $errstr);
if (!$fp)
{
$result = "Error: could not open socket connection";
}
else
{
// get the welcome message
fgets ($fp, 1024);
// write the user string to the socket
fputs ($fp, $_POST['message']);
// get the result
$result .= fgets ($fp, 1024);
// close the connection
fputs ($fp, "exit");
fclose ($fp);
// trim the result and remove the starting ?
$result = trim($result);
// now print it to the browser
}
?>
Server said: <b><? echo $result; ?></b>
<?
}
?>
</body>
</html>
Вы получаете сообщение об ошибке ?? –
Внимательно прочитайте вопрос. ОП спрашивает, почему ответ занимает 1 минуту вперед. –
Почему это занимает минуту, я тебя понимаю? –