stream_socket_accept
(PHP 5)
stream_socket_accept — Accept a connection on a socket created by stream_socket_server()
Description
$server_socket
[, float $timeout
= ini_get("default_socket_timeout")
[, string &$peername
]] )Accept a connection on a socket previously created by stream_socket_server().
Parameters
-
server_socket
-
The server socket to accept a connection from.
-
timeout
-
Override the default socket accept timeout. Time should be given in seconds.
-
peername
-
Will be set to the name (address) of the client which connected, if included and available from the selected transport.
Note:
Can also be determined later using stream_socket_get_name().
Return Values
Returns a stream to the accepted socket connection or FALSE
on failure.
Notes
This function should not be used with UDP server sockets. Instead, use stream_socket_recvfrom() and stream_socket_sendto().
See Also
- stream_socket_server() - Create an Internet or Unix domain server socket
- stream_socket_get_name() - Retrieve the name of the local or remote sockets
- stream_set_blocking() - Set blocking/non-blocking mode on a stream
- stream_set_timeout() - Set timeout period on a stream
- fgets() - Gets line from file pointer
- fgetss() - Gets line from file pointer and strip HTML tags
- fwrite() - Binary-safe file write
- fclose() - Closes an open file pointer
- feof() - Tests for end-of-file on a file pointer
- cURL Functions
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Потоки
- set_socket_blocking
- stream_bucket_append
- stream_bucket_make_writeable
- stream_bucket_new
- stream_bucket_prepend
- stream_context_create
- stream_context_get_default
- stream_context_get_options
- stream_context_get_params
- stream_context_set_default
- stream_context_set_option
- stream_context_set_params
- stream_copy_to_stream
- stream_encoding
- stream_filter_append
- stream_filter_prepend
- stream_filter_register
- stream_filter_remove
- stream_get_contents
- stream_get_filters
- stream_get_line
- stream_get_meta_data
- stream_get_transports
- stream_get_wrappers
- stream_is_local
- stream_notification_callback
- stream_register_wrapper
- stream_resolve_include_path
- stream_select
- stream_set_blocking
- stream_set_chunk_size
- stream_set_read_buffer
- stream_set_timeout
- stream_set_write_buffer
- stream_socket_accept
- stream_socket_client
- stream_socket_enable_crypto
- stream_socket_get_name
- stream_socket_pair
- stream_socket_recvfrom
- stream_socket_sendto
- stream_socket_server
- stream_socket_shutdown
- stream_supports_lock
- stream_wrapper_register
- stream_wrapper_restore
- stream_wrapper_unregister
Коментарии
This code could be very helpfull...
The following code is for the "server". It listen for a message until CTRL-C
<?php
while (true)
{
// disconnected every 5 seconds...
receive_message('127.0.0.1','85',5);
}
function receive_message($ipServer,$portNumber,$nbSecondsIdle)
{
// creating the socket...
$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);
if (!$socket)
{
echo "$errstr ($errno)<br />\n";
}
else
{
// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.
while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))
{
$message= fread($conn, 1024);
echo 'I have received that : '.$message;
fputs ($conn, "OK\n");
fclose ($conn);
}
fclose($socket);
}
}
?>
The following code is for the "client". It send a message, and read the respons...
<?php
send_message('127.0.0.1','85','Message to send...');
function send_message($ipServer,$portServer,$message)
{
$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);
if (!$fp)
{
echo "ERREUR : $errno - $errstr<br />\n";
}
else
{
fwrite($fp,"$message\n");
$response = fread($fp, 4);
if ($response != "OK\n")
{echo 'The command couldn\'t be executed...\ncause :'.$response;}
else
{echo 'Execution successfull...';}
fclose($fp);
}
}
?>
To check if there's a new connection waiting, without blocking, or (when using non-blocking mode) without notices), you can use stream_accept (as opposed to socket_select).
<?php
class GenericClass {
protected $resSocket=null;
function acceptConnections() {
# check that we still have a resource
if(is_resource($this->resSocket)) {
$arrRead=array($this->resSocket);
$arrWrite=array();
/** @warning Passing $arrRead,$arrWrite by reference */
if(stream_select($arrRead,$arrWrite,$arrWrite,0)) {
$resConnection=stream_socket_accept($this->resSocket,0);
# ... other stuff here
}
}
}
}
?>