PHP Doku:: Schreibt in einen Socket - function.socket-write.html

Verlauf / Chronik / History: (19) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteSocketsSocket-Funktionensocket_write

Ein Service von Reinhard Neidl - Webprogrammierung.

Socket-Funktionen

<<socket_strerror

Secure Shell2>>

socket_write

(PHP 4 >= 4.1.0, PHP 5)

socket_writeSchreibt in einen Socket

Beschreibung

int socket_write ( resource $socket , string $buffer [, int $length = 0 ] )

Die Funktion socket_write() schreibt aus dem Puffer buffer in den Socket socket.

Parameter-Liste

socket

buffer

Der Puffer, dessen Inhalt geschrieben werden soll.

length

Mit dem optionalen Parameter length kann man alternativ eine Anzahl von Bytes angeben, die in den Socket geschrieben werden sollen. Falls diese Anzahl größer ist, als der Puffer, wird sie stillschweigend auf die Puffergröße gekürzt.

Rückgabewerte

Gibt die Anzahl der erfolgreich in den Socket geschriebenen Bytes zurück oder FALSE, falls ein Fehler aufgetreten ist. Der Fehlercode kann mit der Funktion socket_last_error() ermittelt werden. Dieser Fehlercode kann an die Funktion socket_strerror() übergeben werden, um eine textuelle Beschreibung des Fehlers zu erhalten.

Hinweis:

Es ist durchaus möglich, dass socket_write() den Wert 0 zurckgibt, was bedeutet, dass kein einziges Byte geschrieben wurde. Vergewissern Sie sich, dass Sie den === Operator benutzen, um im Falle eines Fehlers auf FALSE zu testen.

Anmerkungen

Hinweis:

socket_write() schreibt nicht notwendigerweise alle Bytes aus dem angegebenen Puffer. Es ist auch möglich, dass, abhängig von den Puffern im Netzwerk usw., nur eine bestimmte Datenmenge, sogar auch nur ein Byte, geschrieben wird, obwohl der Puffer größer ist. Dies müssen Sie beobachten, damit Sie nicht versehentlich versäumen, den Rest Ihrer Daten zu senden.

Siehe auch


5 BenutzerBeiträge:
- Beiträge aktualisieren...
revelable at hotmail dot com
21.12.2010 2:08
Here we have the same function to write a socket but with improved performance.

If the messager are not larger, they will be written entirely with a single socket_write() call. And is not needed to call the substr() function for the first bucle.

<?php
$st
="Message to sent";
$length = strlen($st);
       
    while (
true) {
       
       
$sent = socket_write($socket, $st, $length);
           
        if (
$sent === false) {
       
            break;
        }
           
       
// Check if the entire message has been sented
       
if ($sent < $length) {
               
           
// If not sent the entire message.
            // Get the part of the message that has not yet been sented as message
           
$st = substr($st, $sent);
               
           
// Get the length of the not sented part
           
$length -= $sent;

        } else {
           
            break;
        }
           
    }
?>
slyv at poczta dot onet dot pl
13.02.2009 11:16
"socket_write() does not necessarily write all bytes from the given buffer."
So I wrote the following code to correctly write message to the socket

<?php
$message
="Message to sent";
$len = strlen($message);
$offset = 0;
while (
$offset < $len) {
   
$sent = socket_write($socket, substr($message, $offset), $len-$offset);
    if (
$sent === false) {
       
// Error occurred, break the while loop
       
break;
    }
   
$offset += $sent;
}
if (
$offset < $len) {
   
$errorcode = socket_last_error();
   
$errormsg = socket_strerror($errorcode);
    echo
"SENDING ERROR: $errormsg";
} else {
       
// Data sent ok
}
?>
masterwaster at gmail dot com
26.08.2008 20:42
Hi,
if you got same problems like i have

<?php
@socket_write($xd, "Good Bye!\n\r");
@
socket_shutdown($xd, 2);
@
socket_close($xd);
?>

wont'tx send "Good Bye!\n\r" to the opened socket.

but if you put a
usleep or something like echo "";
between write and shutdown its working.
webmaster at you-are-infected dot com
23.08.2006 19:27
If you connect to a Server in a way like you do with telnet or some similar protokoll you may have problems with sending data to the server. I found out that at some servers there is a different between:

<?php
   
    socket_write
($my_socket, $line, strlen ($line));
   
socket_write ($my_socket, "\r\n", strlen ("\r\n"));
   
?>
witch worked at least, and
<?php
    socket_write
($my_socket, $line."\r\n", strlen ($line."\r\n"));
?>
wich made the server stop sending any data.

I hope this helps to save a lot of time. I needed about two days to find out, that this was the problem ;)
gtk at linux dot online dot no
20.08.2002 13:43
from http://www.manualy.sk/sock-faq/unix-socket-faq-2.html
read() is equivalent to recv() with a flags parameter of 0. Other values for the flags parameter change the behaviour of recv(). Similarly, write() is equivalent to send() with flags == 0.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)