2010-07-22 132 views
0

我有寫一個文件指針$fp這是使用fsockopen打開下面的PHP代碼:PHP的fwrite的停止寫入套接字文件指針

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:"); 

$bytes = 0; 
while ($bytes < strlen($buf) && ($w = @fwrite($fp, substr($buf, $bytes)))) 
{ 
    syslog(LOG_INFO, " - " . $w . " bytes written to socket"); 
    $bytes += $w; 
} 

if ($bytes != strlen($buf)) 
{ 
    syslog(LOG_INFO, "error while writing to socket"); 
    exit(); 
} 

此代碼工作正常只要$buf尺寸小足夠。大量的數據不能完全寫入。我得到以下輸出:

Write 4900360 bytes to socket: 
    - 11096 bytes written to socket 
error while writing to socket 

btw。 fwrite的返回值是0而不是false

有沒有人有一個想法可能是什麼問題?非常感謝您的回答

在FWRITE前取下@時,我得到了以下聲明:

Notice: fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer in /root/test.php on line 10 

Notice: fwrite(): send of 8192 bytes failed with errno=32 Broken pipe in /root/test.php on line 10 

我只是聞聞TCP流,我想通了,我得到一個

HTTP/1.1 413 Request Entity Too Large 

有沒有解決這個問題?我使用的lighttpd/1.4.22服務器

+0

刪除fwrite之前的@,它幾乎肯定會打印出具有相關細節的警告。 – nothrow 2010-07-22 13:12:33

+0

fwrite不會產生任何警告。返回值是0而不是false,這意味着該命令沒有失敗,但沒有寫入任何字節(根據文檔) – 2010-07-22 13:24:19

+0

我現在把error_reporting(E_ALL)放在我的代碼前面,我得到以下通知: ():發送8192字節失敗,errno = 104在第10行的/root/test.php中由對等端重置連接 注意:fwrite():發送8192字節失敗,errno = 32中斷管道在/root/test.php在線10 我很高興爲任何翻譯:) ...但爲什麼fwrite的返回值不是false? – 2010-07-22 15:42:46

回答

0

嘗試就像是想得等這個

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:"); 

$bytes = 0; 
do 
{ 
    $w = fwrite($fp, substr($buf, $bytes)); 
    $bytes += $w; 
      syslog(LOG_INFO, "written: ".$w); 
    if ($w === 0) 
    { 
     // end of write 
     break; 
    } 
    if ($w === false) 
    { 
      syslog(LOG_INFO, "error while writing to socket"); 
      exit(); 
    } 
} while (true); 

我相信你的while循環是問題之前,並沒有返回true ..

見當它停止寫入時

+0

感謝您的回覆,但我在這裏得到了同樣的結果。當我在do之後打印$字節時,雖然我像之前一樣獲得了像11096這樣的東西 – 2010-07-22 15:15:01

+0

您可以發佈更多代碼,即$ fp = fsockopen部分嗎?你可以編輯出服務器名稱等等,但需要看看你在做什麼,因爲這應該工作。 對於我發佈的那個,它對「寫入:xx」行有什麼意義,多於1個? – 2010-07-22 17:18:57

+0

確定現在閱讀您的編輯和評論:D,您可以發佈您的fsock命令,需要查看您正在打開的內容以及您要發送的標題類型。再次,你可以去掉任何敏感的東西。 – 2010-07-22 17:33:10