Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: socket_write - Manual
[go: Go Back, main page]

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

search for in the

SSH2" width="11" height="7"/> <socket_strerror
Last updated: Fri, 13 Feb 2009

view this page in

socket_write

(PHP 4 >= 4.1.0, PHP 5)

socket_writeソケットに書き込む

説明

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

関数 socket_write() は、 buffer の内容をソケット socket に書き込みます。

パラメータ

socket

buffer

書き込まれるバッファ。

length

オプションのパラメータ length で、 ソケットに書き込むバイト数を指定することが可能です。 この値がバッファの長さより大きい場合、自動的にバッファのサイズに切り詰められます。

返り値

ソケットへの書き込みに成功したデータのバイト数を返します。エラー時には FALSE を返します。エラーコードは socket_last_error() を用いて取得することができ、この値を socket_strerror() に渡すことでエラー情報を文字列で取得可能です。

注意: socket_write() がゼロを返すことも十分ありえます。 これは、書き込むデータが存在しなかったことを意味します。 エラーをチェックするために FALSE かどうかを調べる際には、必ず === 演算子を使用しましょう。

注意

注意: socket_write() は、バッファの内容を必ずしもすべて 書き込むとは限りません。ネットワークバッファの状態にもよりますが、 たとえ 1 バイトだけ書き込まれたのであったとしても、それはエラーではなく正常な動作です。 そのため、データがすべて書き込まれたのかどうかに注意する必要があります。

参考



SSH2" width="11" height="7"/> <socket_strerror
Last updated: Fri, 13 Feb 2009
 
add a note add a note User Contributed Notes
socket_write
slyv at poczta dot onet dot pl
13-Feb-2009 07: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
}
?>
webmaster at you-are-infected dot com
24-Aug-2006 02: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 08: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" width="11" height="7"/> <socket_strerror
Last updated: Fri, 13 Feb 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites