首页 >函数列表 >socket_recv

socket_recv

socket_recv

(PHP 4 >= 4.1.0, PHP 5)

socket_recvReceives data from a connected socket

说明

int socket_recv ( resource $socket , string &$buf , int $len , int $flags )

The socket_recv() function receives len bytes of data in buf from socket. socket_recv() can be used to gather data from connected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function.

buf is passed by reference, so it must be specified as a variable in the argument list. Data read from socket by socket_recv() will be returned in buf.

参数

socket

The socket must be a socket resource previously created by socket_create().

buf

The data received will be fetched to the variable specified with buf. If an error occurs, if the connection is reset, or if no data is available, buf will be set to NULL.

len

Up to len bytes will be fetched from remote host.

flags

The value of flags can be any combination of the following flags, joined with the binary OR (|) operator.

Possible values for flags
Flag Description
MSG_OOB Process out-of-band data.
MSG_PEEK Receive data from the beginning of the receive queue without removing it from the queue.
MSG_WAITALL Block until at least len are received. However, if a signal is caught or the remote host disconnects, the function may return less data.
MSG_DONTWAIT With this flag set, the function returns even if it would normally have blocked.

返回值

socket_recv() returns the number of bytes received, or FALSE if there was an error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error.

范例

Example #1 socket_recv() example

This example is a simple rewrite of the first example from 范例 to use socket_recv().

<?php
error_reporting
(E_ALL);

echo 
"<h2>TCP/IP Connection</h2> ";


$service_port getservbyname('www''tcp');


$address gethostbyname('www.example.com');


$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
if (
$socket === false) {
    echo 
"socket_create() failed: reason: " socket_strerror(socket_last_error()) . " ";
} else {
    echo 
"OK. ";
}

echo 
"Attempting to connect to '$address' on port '$service_port'...";
$result socket_connect($socket$address$service_port);
if (
$result === false) {
    echo 
"socket_connect() failed. Reason: ($result) " socket_strerror(socket_last_error($socket)) . " ";
} else {
    echo 
"OK. ";
}

$in "HEAD / HTTP/1.1 ";
$in .= "Host: www.example.com ";
$in .= "Connection: Close ";
$out '';

echo 
"Sending HTTP HEAD request...";
socket_write($socket$instrlen($in));
echo 
"OK. ";

echo 
"Reading response: ";
$buf 'This is my buffer.';
if (
false !== ($bytes socket_recv($socket$buf2048MSG_WAITALL))) {
    echo 
"Read $bytes bytes from socket_recv(). Closing socket...";
} else {
    echo 
"socket_recv() failed; reason: " socket_strerror(socket_last_error($socket)) . " ";
}
socket_close($socket);

echo 
$buf " ";
echo 
"OK. ";
?>

The above example will produce something like:

<h2>TCP/IP Connection</h2>
OK.
Attempting to connect to '208.77.188.166' on port '80'...OK.
Sending HTTP HEAD request...OK.
Reading response:

Closing socket...HTTP/1.1 200 OK
Date: Mon, 14 Sep 2009 08:56:36 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: "b80f4-1b6-80bfd280"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8

OK.


  • socket_accept
  • socket_bind
  • socket_clear_error
  • socket_close
  • socket_connect
  • socket_create
  • socket_create_listen
  • socket_create_pair
  • socket_getpeername
  • socket_getsockname
  • socket_get_option
  • socket_last_error
  • socket_listen
  • socket_read
  • socket_recv
  • socket_recvfrom
  • socket_select
  • socket_send
  • socket_sendto
  • socket_set_block
  • socket_set_nonblock
  • socket_set_option
  • socket_shutdown
  • socket_strerror
  • socket_write
  • PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道