EventHttp::accept

(PECL event >= 1.2.6-beta)

EventHttp::acceptMakes an HTTP server accept connections on the specified socket stream or resource

Beschreibung

public EventHttp::accept( mixed $socket ): bool

Makes an HTTP server accept connections on the specified socket stream or resource. The socket should be ready to accept connections.

Can be called multiple times to accept connections on different sockets.

Hinweis:

To bind a socket, listen , and accept connections on the socket in s single call use EventHttp::bind() . EventHttp::accept() is needed only if one already has a socket ready to accept connections.

Parameter-Liste

socket

Socket resource, stream or numeric file descriptor representing a socket ready to accept connections.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Beispiele

Beispiel #1 EventHttp::accept() example

<?php
$base 
= new EventBase();
$http = new EventHttp($base);

$addresses = array (
     
8091 => "127.0.0.1",
     
8092 => "127.0.0.2",
);
$i 0;

$socket = array();

foreach (
$addresses as $port => $ip) {
    echo 
$ip" "$portPHP_EOL;
    
$socket[$i] = socket_create(AF_INETSOCK_STREAMSOL_TCP);
    if (!
socket_bind($socket[$i], $ip$port)) {
        exit(
"socket_bind failed\n");
    }
    
socket_listen($socket[$i], 0);
    
socket_set_nonblock($socket[$i]);

    if (!
$http->accept($socket[$i])) {
        echo 
"Accept failed\n";
        exit(
1);
    }

    ++
$i;
}

$http->setCallback("/some-page", function() {
 echo 
"(some-page)\n";
    echo 
"URI: "$req->getUri(), PHP_EOL;
    
$req->sendReply(200"OK");
    echo 
"OK\n";
});

$http->setDefaultCallback(function($req) {
    echo 
"URI: "$req->getUri(), PHP_EOL;
    
$req->sendReply(200"OK");
    echo 
"OK\n";
});

$signal Event::signal($baseSIGINT, function () use ($base) {
    echo 
"Caught SIGINT. Stopping...\n";
    
$base->stop();
});
$signal->add();

$base->dispatch();
echo 
"END\n";
// We didn't close sockets, since Libevent already sets
// CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file 
// descriptor associated with the sockets.
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Client:
$ nc 127.0.0.1 8091
GET /about HTTP/1.0
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

Server:
127.0.0.1 8091
127.0.0.2 8092
URI: /about
OK

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Tutorial: Download

The Minecraft Jason Momoa leather jacket (https://www.celebsmoviejackets.com/a-minecraft-2025-jason-momoa-leather-jacket) is a bold fusion of rugg ...

Geschrieben von sumitguptill90 am 30.04.2025 14:52:40
Forum: Tutorials
Suche Netzwerktechnik Großhändler

Ja, da kann ich dir helfen! Ich habe gute Erfahrungen mit gemacht. Sie bieten ein großes Sortiment an hochwertiger Netzwerkausrüstung und einen ...

Geschrieben von urielvalerie am 29.04.2025 15:28:21
Forum: Ankündigungen
Keine Moderation?

I think the site you mentioned is no longer actively managed, as there is a lot of spam, outdated PHP scripts, and broken links without any cleanu ...

Geschrieben von rogerwarner219 am 29.04.2025 12:30:22
Forum: Fragen/Vorschläge zum Forum