socket_recvfrom

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

socket_recvfromReceives data from a socket whether or not it is connection-oriented

Description

socket_recvfrom(
    Socket $socket,
    string &$data,
    int $length,
    int $flags,
    string &$address,
    int &$port = null
): int|false

The socket_recvfrom() function receives length bytes of data in data from address on port port (if the socket is not of type AF_UNIX) using socket. socket_recvfrom() can be used to gather data from both connected and unconnected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function.

The address and port must be passed by reference. If the socket is not connection-oriented, address will be set to the internet protocol address of the remote host or the path to the UNIX socket. If the socket is connection-oriented, address is null. Additionally, the port will contain the port of the remote host in the case of an unconnected AF_INET or AF_INET6 socket.

Note: This function is binary-safe.

Parameters

socket

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

data

The data received will be fetched to the variable specified with data.

length

Up to length 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 length 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.
address

If the socket is of the type AF_UNIX type, address is the path to the file. Else, for unconnected sockets, address is the IP address of, the remote host, or null if the socket is connection-oriented.

port

This argument only applies to AF_INET and AF_INET6 sockets, and specifies the remote port from which the data is received. If the socket is connection-oriented, port will be null.

Return Values

socket_recvfrom() 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.

Changelog

Version Description
8.0.0 socket is a Socket instance now; previously, it was a resource.

Examples

Example #1 socket_recvfrom() example

<?php

$socket 
socket_create(AF_INETSOCK_DGRAMSOL_UDP);
socket_bind($socket'127.0.0.1'1223);

$from '';
$port 0;
socket_recvfrom($socket$buf120$from$port);

echo 
"Received $buf from remote address $from and remote port $portPHP_EOL;
?>

This example will initiate a UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host.

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial