downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

SSH2> <socket_strerror
Last updated: Fri, 14 Aug 2009

view this page in

socket_write

(PHP 4 >= 4.1.0, PHP 5)

socket_writeÉcrit dans une socket

Description

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

socket_write() écrit dans la socket socket les données du buffer buffer .

Liste de paramètres

socket

buffer

Le buffer à écrire.

length

Le paramètre optionnel length peut spécifier explicitement la taille des données qui doivent être écrites. Si cette longueur est plus grande que la taille du buffer, elle sera ramenée automatiquement à la taille du buffer lui-même.

Valeurs de retour

socket_write() retourne le nombre d'octets qui ont pu être écrits dans la socket, ou bien FALSE en cas d'erreur. Le code d'erreur généré peut être obtenu en appelant la fonction socket_last_error(). Ce code d'erreur peut être passé à la fonction socket_strerror() pour obtenir un message d'erreur, humainement lisible.

Note: Il est parfaitement valide pour socket_write() de retourner zéro, ce qui signifie qu'aucun octet n'a été écrit. Soyez bien sûr d'utiliser l'opérateur === pour comparer le retour de la fonction avec FALSE, et détecter un cas d'erreur.

Notes

Note: socket_write() n'écrit pas nécessairement tous les octets du buffer fourni. Il est valide que, suivant certaines configuration de buffer réseau, seulement une certaine quantité de données, même un octet, soit écrit, y compris si votre buffer est plus grand. Vous devez alors vous assurer que vous n'avez pas oublié de transmettre le reste de vos données.

Voir aussi



SSH2> <socket_strerror
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
socket_write
slyv at poczta dot onet dot pl
13-Feb-2009 10: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-Aug-2008 06: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-Aug-2006 05: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-Aug-2002 11: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.

SSH2> <socket_strerror
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites