socket_create

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

socket_createErzeugt einen Socket (Endpunkt für die Kommunikation)

Beschreibung

socket_create(int $domain, int $type, int $protocol): Socket|false

Erzeugt eine Socket-Instanz (auch als Kommunikations-Endpunkt bezeichnet) und gibt sie zurück. Eine typische Netzwerkverbindung besteht aus zwei Sockets, von denen der eine die Rolle des Clients und der andere die Rolle des Servers übernimmt.

Parameter-Liste

domain

Der domain-Parameter legt die Protokollfamilie fest, die für die Kommunikation vom Socket benutzt wird.

Folgende Adressen/Protokollfamilien sind verfügbar:
Domain Beschreibung
AF_INET IPv4-basierte Internet-Protokolle. TCP und UDP sind allgemein gebräuchliche Protokolle dieser Familie.
AF_INET6 IPv6-basierte Internet-Protokolle. TCP und UDP sind allgemein gebräuchliche Protokolle dieser Familie.
AF_UNIX Protokollfamilie für die lokale Kommunikation. Hohe Effizienz und ein geringer Overhead machen sie zur ersten Wahl für die IPC (Inter-Process Communication).
type

Der Parameter type bestimmt den Kommunikationstyp, den der Socket verwenden soll.

Mögliche Socket-Typen
Typ Beschreibung
SOCK_STREAM Stellt zuverlässige, sequentielle verbindungsorientierte Byte-Streams im Vollduplexmodus zur Verfügung. Auch bandexterne Mechanismen zur Datenübertragung können unterstützt werden. Das TCP-Protokoll beruht auf diesem Socket-Typ.
SOCK_DGRAM Unterstützt Datagramme (verbindunglos, Nachrichten vorgegebener Maximallänge, deren Übertragung nicht zuverlässig ist). Das UDP-Protokoll beruht auf diesem Socket-Typ.
SOCK_SEQPACKET Stellt einen zuverlässigen, sequentiellen, bidirektionalen, verbindungsorientierten Übertragungsweg für Datagramme vorgegebener Maximallänge zur Verfügung. Ein Empfänger muss bei jeder Leseanforderung das gesamte Datenpaket lesen.
SOCK_RAW Stellt einen Zugang mit einem nicht bestimmten Netzwerkprotokoll zur Verfügung. Dieser spezielle Socket-Typ kann dazu benutzt werden, um manuell ein Protokoll eines beliebigen Typs zu erstellen. Solche Sockets werden üblicherweise benutzt, um ICMP-Requests zu senden (wie etwa bei ping).
SOCK_RDM Stellt eine zuverlässige Schicht für Datagramme zur Verfügung, in der die Reihenfolge der Datagramme nicht garantiert wird. Dies ist in den meisten Betriebssytemen nicht implementiert.
protocol

Der Parameter protocol setzt das spezifische Protokoll innerhalb der angegebenen domain, das vom zurückgegebenen Socket für die Kommunikation benutzt wird. Der passende Wert kann ermittelt werden, indem der Name des Protokolls an die Funktion getprotobyname() übergeben wird. Wenn TCP oder UDP gewünscht werden, können auch die entsprechenden Konstanten SOL_TCP und SOL_UDP benutzt werden.

Gebräuchliche Protokolle
Name Beschreibung
icmp Das Internet Control Message Protocol wird hauptsächlich von Gateways und Hosts benutzt, um Kommunikationsfehler bei Datagrammen zu melden. Das Kommando "ping" (das in den meisten modernen Betriebssystemen enthalten ist) ist ein Beispiel für eine Anwendung von ICMP.
udp Das User Datagram Protocol ist ein verbindungsloses, unzuverlässiges Protokoll, mit dem Datensätze fester Länge versendet werden können. Aufgrund dieser geringen Anforderungen braucht UDP nur einen minimalen Protokoll-Overhead.
tcp Das Transmission Control Protocol ist ein zuverlässiges, verbindungsorientiertes, Stream-orientiertes Protokoll für den Vollduplex-Betrieb. Bei TCP ist garantiert, dass alle Datenpakete in derselben Reihenfolge empfangen werden, in der sie gesendet wurden. Falls während der Übertragung ein Datenpaket verlorengeht, wird es von TCP erneut versendet, und zwar solange, bis der Empfängerhost den Eingang dieses Paktes zurückmeldet. Aus Gründen der Zuverlässigkeit und Leistung ist TCP so implementiert, dass es eigenständig die Oktet-Grenzen der darunterliegenden Datagramm-Kommunikationschicht bestimmen kann. Aus diesem Grund müssen TCP Anwendungen in der Lage sein, auch Teilübertragungen empfangen zu können.

Rückgabewerte

Bei Erfolg gibt socket_create() eine Socket-Instanz zurück, bei einem Fehler wird false zurückgegeben. Der aktuelle Fehlercode kann mit socket_last_error() ermittelt und an die Funktion socket_strerror() übergeben werden, um eine textuelle Beschreibing des Fehlers zu erhalten.

Fehler/Exceptions

Falls eine ungültige domain oder ein ungültiger type angegbeben wird, fällt socket_create() auf seine Standardeinstellungen AF_INET bzw. SOCK_STREAM zurück, und gibt zusätzlich eine E_WARNING-Meldung aus.

Changelog

Version Beschreibung
8.0.0 Bei Erfolg gibt diese Funktion nun eine Socket-Instanz zurück; zuvor wurde eine Ressource zurückgegeben.

Siehe auch

add a note add a note

User Contributed Notes 12 notes

up
41
kyle gibson
19 years ago
Took me about 20 minutes to figure out the proper arguments to supply for a AF_UNIX socket. Anything else, and I would get a PHP warning about the 'type' not being supported. I hope this saves someone else time.

<?php
$socket
= socket_create(AF_UNIX, SOCK_STREAM, 0);
// code
?>
up
11
ab1965 at yandex dot ru
12 years ago
It took some time to understand how one PHP process can communicate with another by means of unix udp sockets. Examples of 'server' and 'client' code are given below. Server is assumed to run before client starts.

'Server' code
<?php
if (!extension_loaded('sockets')) {
    die(
'The sockets extension is not loaded.');
}
// create unix udp socket
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
if (!
$socket)
        die(
'Unable to create AF_UNIX socket');

// same socket will be used in recv_from and send_to
$server_side_sock = dirname(__FILE__)."/server.sock";
if (!
socket_bind($socket, $server_side_sock))
        die(
"Unable to bind to $server_side_sock");

while(
1) // server never exits
{
// receive query
if (!socket_set_block($socket))
        die(
'Unable to set blocking mode for socket');
$buf = '';
$from = '';
echo
"Ready to receive...\n";
// will block to wait client query
$bytes_received = socket_recvfrom($socket, $buf, 65536, 0, $from);
if (
$bytes_received == -1)
        die(
'An error occured while receiving from the socket');
echo
"Received $buf from $from\n";

$buf .= "->Response"; // process client query here

// send response
if (!socket_set_nonblock($socket))
        die(
'Unable to set nonblocking mode for socket');
// client side socket filename is known from client request: $from
$len = strlen($buf);
$bytes_sent = socket_sendto($socket, $buf, $len, 0, $from);
if (
$bytes_sent == -1)
        die(
'An error occured while sending to the socket');
else if (
$bytes_sent != $len)
        die(
$bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected');
echo
"Request processed\n";
}
?>

'Client' code
<?php
if (!extension_loaded('sockets')) {
    die(
'The sockets extension is not loaded.');
}
// create unix udp socket
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
if (!
$socket)
        die(
'Unable to create AF_UNIX socket');

// same socket will be later used in recv_from
// no binding is required if you wish only send and never receive
$client_side_sock = dirname(__FILE__)."/client.sock";
if (!
socket_bind($socket, $client_side_sock))
        die(
"Unable to bind to $client_side_sock");

// use socket to send data
if (!socket_set_nonblock($socket))
        die(
'Unable to set nonblocking mode for socket');
// server side socket filename is known apriori
$server_side_sock = dirname(__FILE__)."/server.sock";
$msg = "Message";
$len = strlen($msg);
// at this point 'server' process must be running and bound to receive from serv.sock
$bytes_sent = socket_sendto($socket, $msg, $len, 0, $server_side_sock);
if (
$bytes_sent == -1)
        die(
'An error occured while sending to the socket');
else if (
$bytes_sent != $len)
        die(
$bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected');

// use socket to receive data
if (!socket_set_block($socket))
        die(
'Unable to set blocking mode for socket');
$buf = '';
$from = '';
// will block to wait server response
$bytes_received = socket_recvfrom($socket, $buf, 65536, 0, $from);
if (
$bytes_received == -1)
        die(
'An error occured while receiving from the socket');
echo
"Received $buf from $from\n";

// close socket and delete own .sock file
socket_close($socket);
unlink($client_side_sock);
echo
"Client exits\n";
?>
up
8
rhollencamp at gmail dot com
15 years ago
Note that if you create a socket with AF_UNIX, a file will be created in the filesystem. This file is not removed when you call socket_close - you should unlink the file after you close the socket.
up
2
geoff at spacevs dot com
14 years ago
Here is a ping function for PHP without using exec/system/passthrough/etc... Very useful to use to just test if a host is online before attempting to connect to it. Timeout is in seconds.

<?PHP
       
function ping($host, $timeout = 1) {
               
/* ICMP ping packet with a pre-calculated checksum */
               
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
               
$socket  = socket_create(AF_INET, SOCK_RAW, 1);
               
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
               
socket_connect($socket, $host, null);

               
$ts = microtime(true);
               
socket_send($socket, $package, strLen($package), 0);
                if (
socket_read($socket, 255))
                       
$result = microtime(true) - $ts;
                else   
$result = false;
               
socket_close($socket);

                return
$result;
        }
?>
up
0
Jean Charles MAMMANA
16 years ago
I've written the ping() function using socket_create() with SOCK_RAW.
(on Unix System, you need to have the root acces to execute this function)

<?php

/// start ping.inc.php ///

$g_icmp_error = "No Error";

// timeout in ms
function ping($host, $timeout)
{
       
$port = 0;
       
$datasize = 64;
        global
$g_icmp_error;
       
$g_icmp_error = "No Error";
       
$ident = array(ord('J'), ord('C'));
       
$seq   = array(rand(0, 255), rand(0, 255));

    
$packet = '';
    
$packet .= chr(8); // type = 8 : request
    
$packet .= chr(0); // code = 0

    
$packet .= chr(0); // checksum init
    
$packet .= chr(0); // checksum init

       
$packet .= chr($ident[0]); // identifier
       
$packet .= chr($ident[1]); // identifier

       
$packet .= chr($seq[0]); // seq
       
$packet .= chr($seq[1]); // seq

       
for ($i = 0; $i < $datasize; $i++)
               
$packet .= chr(0);

       
$chk = icmpChecksum($packet);

       
$packet[2] = $chk[0]; // checksum init
       
$packet[3] = $chk[1]; // checksum init

       
$sock = socket_create(AF_INET, SOCK_RAWgetprotobyname('icmp'));
       
$time_start = microtime();
   
socket_sendto($sock, $packet, strlen($packet), 0, $host, $port);
   

   
$read   = array($sock);
       
$write  = NULL;
       
$except = NULL;

       
$select = socket_select($read, $write, $except, 0, $timeout * 1000);
        if (
$select === NULL)
        {
               
$g_icmp_error = "Select Error";
               
socket_close($sock);
                return -
1;
        }
        elseif (
$select === 0)
        {
               
$g_icmp_error = "Timeout";
               
socket_close($sock);
                return -
1;
        }

   
$recv = '';
   
$time_stop = microtime();
   
socket_recvfrom($sock, $recv, 65535, 0, $host, $port);
       
$recv = unpack('C*', $recv);
       
        if (
$recv[10] !== 1) // ICMP proto = 1
       
{
               
$g_icmp_error = "Not ICMP packet";
               
socket_close($sock);
                return -
1;
        }

        if (
$recv[21] !== 0) // ICMP response = 0
       
{
               
$g_icmp_error = "Not ICMP response";
               
socket_close($sock);
                return -
1;
        }

        if (
$ident[0] !== $recv[25] || $ident[1] !== $recv[26])
        {
               
$g_icmp_error = "Bad identification number";
               
socket_close($sock);
                return -
1;
        }
       
        if (
$seq[0] !== $recv[27] || $seq[1] !== $recv[28])
        {
               
$g_icmp_error = "Bad sequence number";
               
socket_close($sock);
                return -
1;
        }

       
$ms = ($time_stop - $time_start) * 1000;
       
        if (
$ms < 0)
        {
               
$g_icmp_error = "Response too long";
               
$ms = -1;
        }

       
socket_close($sock);

        return
$ms;
}

function
icmpChecksum($data)
{
       
$bit = unpack('n*', $data);
       
$sum = array_sum($bit);

        if (
strlen($data) % 2) {
               
$temp = unpack('C*', $data[strlen($data) - 1]);
               
$sum += $temp[1];
        }

       
$sum = ($sum >> 16) + ($sum & 0xffff);
       
$sum += ($sum >> 16);

        return
pack('n*', ~$sum);
}

function
getLastIcmpError()
{
        global
$g_icmp_error;
        return
$g_icmp_error;
}
/// end ping.inc.php ///
?>
up
-1
alexander dot krause at ed-solutions dot de
16 years ago
On UNIX systems php needs /etc/protocols for constants like SOL_UDP and SOL_TCP.

This file was missing on my embedded platform.
up
-1
jens at surefoot dot com
18 years ago
Please be aware that RAW sockets (as used for the ping example) are restricted to root accounts on *nix systems. Since web servers hardly ever run as root, they won't work on webpages.

On Windows based servers it should work regardless.
up
-1
evan at coeus hyphen group dot com
22 years ago
Okay I talked with Richard a little (via e-mail). We agree that getprotobyname() and using the constants should be the same in functionality and speed, the use of one or the other is merely coding style. Personally, we both think the constants are prettier :).

The eight different protocols are the ones implemented in PHP- not the total number in existance (RFC 1340 has 98).

All we disagree on is using 0- Richard says that "accordning to the official unix/bsd sockets 0 is more than fine." I think that since 0 is a reserved number according to RFC 1320, and when used usually refers to IP, not one of it's sub-protocols (TCP, UDP, etc.)
up
-3
Anonymous
18 years ago
Here's a ping function that uses sockets instead of exec().  Note: I was unable to get socket_create() to work without running from CLI as root.  I've already calculated the package's checksum to simplify the code (the message is 'ping' but it doesn't actually matter).

<?php

function ping($host) {
   
$package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67";

   
/* create the socket, the last '1' denotes ICMP */   
   
$socket = socket_create(AF_INET, SOCK_RAW, 1);
   
   
/* set socket receive timeout to 1 second */
   
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));
   
   
/* connect to socket */
   
socket_connect($socket, $host, null);
   
   
/* record start time */
   
list($start_usec, $start_sec) = explode(" ", microtime());
   
$start_time = ((float) $start_usec + (float) $start_sec);
   
   
socket_send($socket, $package, strlen($package), 0);
   
    if(@
socket_read($socket, 255)) {
        list(
$end_usec, $end_sec) = explode(" ", microtime());
       
$end_time = ((float) $end_usec + (float) $end_sec);
   
       
$total_time = $end_time - $start_time;
       
        return
$total_time;
    } else {
        return
false;
    }
   
   
socket_close($socket);
}

?>
up
-5
david at eder dot us
20 years ago
Seems there aren't any examples of UDP clients out there.  This is a tftp client.  I hope this makes someone's life easier.

<?php
 
function tftp_fetch($host, $filename)
  {
   
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

   
// create the request packet
   
$packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0);
   
// UDP is connectionless, so we just send on it.
   
socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69);

   
$buffer = '';
   
$port = '';
   
$ret = '';
    do
    {
     
// $buffer and $port both come back with information for the ack
      // 516 = 4 bytes for the header + 512 bytes of data
     
socket_recvfrom($socket, $buffer, 516, 0, $host, $port);

     
// add the block number from the data packet to the ack packet
     
$packet = chr(0) . chr(4) . substr($buffer, 2, 2);
     
// send ack
     
socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);

     
// append the data to the return variable
      // for large files this function should take a file handle as an arg
     
$ret .= substr($buffer, 4);
    }
    while(
strlen($buffer) == 516);  // the first non-full packet is the last.
   
return $ret;
  }
?>
up
-5
david at eder dot us
19 years ago
Sometimes when you are running CLI, you need to know your own ip address.

<?php

  $addr
= my_ip();
  echo
"my ip address is $addr\n";

  function
my_ip($dest='64.0.0.0', $port=80)
  {
   
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
   
socket_connect($socket, $dest, $port);
   
socket_getsockname($socket, $addr, $port);
   
socket_close($socket);
    return
$addr;
  }
?>
up
-4
Sakrizz
8 years ago
Here's a solution for icmpv6 ping with php, dropping it here in case if someone has problems with icmpv6 with php.

<?php
$host
= "2a03:2880:f11b:83:face:b00c:0:25de";
$timeout = 100000;
$count = 3;
echo
"Latency: ". round(1000 * pingv6($host,$timeout,$count),5) ." ms \n";

function
pingv6($target,$timeout,$count) {
    echo
"target is ipv6 address, ". getprotobyname('ipv6-icmp'). " \n";
   
/* create the socket, the last '1' denotes ICMP */
   
$socket = socket_create(AF_INET6, SOCK_RAW, getprotobyname('ipv6-icmp'));
   
/* set socket receive timeout to 1 second */
   
$sec=intval($timeout/1000);
   
$usec=$timeout%1000*1000;
   
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$sec, "usec"=>$usec));
   
/* socket package parameters */
   
$type = "\x80";
   
$seqNumber = chr(floor($i/256)%256) . chr($i%256);
   
$checksum= "\x00\x00";
   
$code = "\x00";
   
$identifier = chr(rand(0,255)) . chr(rand(0,255));
   
$msg = "!\"#$%&'()*+,-./1234567";
   
$package = $type.$code.$checksum.$identifier.$seqNumber.$msg;
       
$checksum = icmpChecksum($package);
       
$package = $type.$code.$checksum.$identifier.$seqNumber.$msg;
   
/* socket connect */
   
if(@socket_connect($socket, $target, null)){
        for(
$i = 0; $i < $count; $i++){
                list(
$start_usec, $start_sec) = explode(" ", microtime());
               
$start_time = ((float) $start_usec + (float) $start_sec);
               
$startTime = microtime(true);
               
socket_send($socket, $package, strLen($package), 0);
                while (
$startTime + $timeout*1000 > microtime(true)){
                        if(
socket_read($socket, 255) !== false) {
                                list(
$end_usec, $end_sec) = explode(" ", microtime());
                               
$end_time = ((float) $end_usec + (float) $end_sec);
                               
$total_time = $end_time - $start_time;
                                echo
"round trip time (".$i."): ". $total_time ."\n";
                                return
$total_time;
                                break;
                        }else{
                                return
"null";
                                echo
"Timed out (".$i."), Got no echo reply\n";
                                break;
                        }
                }
           
usleep($interval*1000);
        }
       
socket_close($socket);
    }
}

function
icmpChecksum($data){
if (
strlen($data)%2) $data .= "\x00";
$bit = unpack('n*', $data);
$sum = array_sum($bit);
while (
$sum >> 16)
 
$sum = ($sum >> 16) + ($sum & 0xffff);
return
pack('n*', ~$sum);
}

?>
To Top