2014-09-20 90 views
1

我有一個Python套接字服務器和一個PHP套接字客戶端。客戶端向服務器發送命令,然後在頁面上顯示響應。它幾乎可以一直運行。現在我遇到的問題是,當涉及到長時間的迴應時,迴應似乎被削減了。PHP套接字客戶端沒有返回完整響應

它不是Python服務器,因爲您通過Telnet獲得完整響應。這是PHP腳本的東西,我不能爲我的生活,搞清楚。從遠程登錄

輸出:

{ 
    "status":1, 
    "ramPerc":25, 
    "console":"[12:49:18 INFO]: --------- Help: Index ---------------------------\n[12:49:18 INFO]: Use /help [n] to get page n of help.\n[12:49:18 INFO]: Aliases: Lists command aliases\n[12:49:18 INFO]: Bukkit: All commands for Bukkit\n[12:49:18 INFO]: Minecraft: All commands for Minecraft\n[12:49:18 INFO]: /achievement: Gives the specified player an achievement or changes a statistic value. Use '*' to give all achievements.\n[12:49:18 INFO]: /ban: Prevents the specified player from using this server\n[12:49:18 INFO]: /ban-ip: Prevents the specified IP address from using this server\n[12:49:18 INFO]: /banlist: View all players banned from this server\n[12:49:18 INFO]: /clear: Clears the player's inventory. Can specify item and data filters too.\n[12:49:18 INFO]: /defaultgamemode: Set the default gamemode\n[12:49:18 INFO]: /deop: Takes the specified player's operator status\n[12:49:18 INFO]: /difficulty: Sets the game difficulty\n[12:49:18 INFO]: /effect: Adds/Removes effects on players\n[12:49:18 INFO]: /enchant: Adds enchantments to the item the player is currently holding. Specify 0 for the level to remove an enchantment. Specify force to ignore normal enchantment restrictions\n[12:49:18 INFO]: /gamemode: Changes the player to a specific game mode\n[12:49:18 INFO]: /gamerule: Sets a server's game rules\n[12:49:18 INFO]: /give: Gives the specified player a certain amount of items\n[12:49:18 INFO]: /help: Shows the help menu\n[12:49:18 INFO]: /kick: Removes the specified player from the server\n[12:49:18 INFO]: /kill: Commits suicide, only usable as a player\n[12:49:18 INFO]: /list: Lists all online players\n[12:49:18 INFO]: /me: Performs the specified action in chat\n[12:49:18 INFO]: /minecraft:achievement: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:ban: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:ban-ip: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:banlist: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:clear: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:defaultgamemode: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:deop: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:difficulty: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:effect: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:enchant: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:gamemode: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:gamerule: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:give: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:help: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:kick: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:kill: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:list: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:me: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:op: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:pardon: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:pardon-ip: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:playsound: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:say: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:scoreboard: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:seed: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:setidletimeout: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:setworldspawn: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:spawnpoint: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:spreadplayers: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:tell: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:testfor: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:time: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:toggledownfall: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:tp: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:weather: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:whitelist: A Mojang provided command.\n[12:49:18 INFO]: /minecraft:xp: A Mojang provided command.\n[12:49:18 INFO]: /netstat: A Mojang provided command.\n[12:49:18 INFO]: /op: Gives the specified player operator status\n[12:49:18 INFO]: /pardon: Allows the specified player to use this server\n[12:49:18 INFO]: /pardon-ip: Allows the specified IP address to use this server\n[12:49:18 INFO]: /playsound: Plays a sound to a given player\n[12:49:18 INFO]: /plugins: Gets a list of plugins running on the server\n[12:49:18 INFO]: /reload: Reloads the server configuration and plugins\n[12:49:18 INFO]: /restart: Restarts the server\n[12:49:18 INFO]: /save-all: Saves the server to disk\n[12:49:18 INFO]: /save-off: Disables server autosaving\n[12:49:18 INFO]: /save-on: Enables server autosaving\n[12:49:18 INFO]: /say: Broadcasts the given message as the sender\n[12:49:18 INFO]: /scoreboard: Scoreboard control\n[12:49:18 INFO]: /seed: Shows the world seed\n[12:49:18 INFO]: /setblock: A Mojang provided command.\n[12:49:18 INFO]: /setidletimeout: Sets the server's idle timeout\n[12:49:18 INFO]: /setworldspawn: Sets a worlds's spawn point. If no coordinates are specified, the player's coordinates will be used.\n[12:49:18 INFO]: /spawnpoint: Sets a player's spawn point\n[12:49:18 INFO]: /spreadplayers: Spreads players around a point\n[12:49:18 INFO]: /stop: Stops the server with optional reason\n[12:49:18 INFO]: /summon: A Mojang provided command.\n[12:49:18 INFO]: /tell: Sends a private message to the given player\n[12:49:18 INFO]: /tellraw: A Mojang provided command.\n[12:49:18 INFO]: /testfor: Tests whether a specifed player is online\n[12:49:18 INFO]: /testforblock: A Mojang provided command.\n[12:49:18 INFO]: /time: Changes the time on each world\n[12:49:18 INFO]: /timings: Manages Spigot Timings data to see performance of the server.\n[12:49:18 INFO]: /toggledownfall: Toggles rain on/off on a given world\n[12:49:18 INFO]: /tp: Teleports the given player (or yourself) to another player or coordinates\n[12:49:18 INFO]: /tps: Gets the current ticks per second for the server\n[12:49:18 INFO]: /version: Gets the version of this server including any plugins in use\n[12:49:18 INFO]: /weather: Changes the weather\n[12:49:18 INFO]: /whitelist: Manages the list of players allowed to use this server\n[12:49:18 INFO]: /xp: Gives: the specified player a certain amount of experience. Specify <amount>L to give levels instead, with a negative amount resulting in taking levels.\n", 
    "ram":517, 
    "players":[ 

    ], 
    "ramMax":2048, 
    "cpu":1 
} 

而從PHP腳本輸出:

{ 
    "status": 1, 
    "ramPerc": 16, 
    "console": "[12:57:42 INFO]: --------- Help: Index ---------------------------\n[12:57:42 INFO]: Use /help [n] to get page n of help.\n[12:57:42 INFO]: Aliases: Lists command aliases\n[12:57:42 INFO]: Bukkit: All commands for Bukkit\n[12:57:42 INFO]: Minecraft: All commands for Minecraft\n[12:57:42 INFO]: /achievement: Gives the specified player an achievement or changes a statistic value. Use '*' to give all achievements.\n[12:57:42 INFO]: /ban: Prevents the specified player from using this server\n[12:57:42 INFO]: /ban-ip: Prevents the specified IP address from using this server\n[12:57:42 INFO]: /banlist: View all players banned from this server\n[12:57:42 INFO]: /clear: Clears the player's inventory. Can specify item and data filters too.\n[12:57:42 INFO]: /defaultgamemode: Set the default gamemode\n[12:57:42 INFO]: /deop: Takes the specified player's operator status\n[12:57:42 INFO]: /difficulty: Sets the game difficulty\n[12:57:42 INFO]: /effect: Adds/Removes effects on players\n[12:57:42 INFO]: /enchant: Adds enchantments to the item the player is currently holding. Specify 0 for the level to remove an enchantment. Specify force to ignore normal enchantment restrictions\n[12:57:42 INFO]: /gamemode: Changes the player to a specific game mode\n[12:57:42 INFO]: /gamerule: Sets a server's game rules\n[12:57:42 INFO]: /give: Gives the specified player a certain amount of items\n[12:57:42 INFO]: 

我搜索周圍的幾個星期和嘗試了一切,但找不出解決問題。

下面是這一切背後的PHP腳本:

public function send($cmd, $host, $port, $timeout = 1.5){ 
    // Get time of function start 
    $startTime = time(); 

    //Try to create a socket 
    if(!($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) 
    { 
     $errorcode = socket_last_error(); 
     $errormsg = socket_strerror($errorcode); 
     //If failed, die with error. (Check docs) 
     die("Error (#300)"); 
    } 

    if(!socket_set_nonblock($socket)){ 
     die("Error (#301)"); 
    } 

    while([email protected]_connect($socket, $host, $port)){ 
     if ((time() - $startTime) >= $timeout){ 
      die("Error (#302)"); 
     } 
     continue; 
    } 

    if(!socket_set_block($socket)){ 
     die("Error (#303)"); 
    } 

    if(!socket_write($socket, $cmd, strlen($cmd))) 
    { 
     $errorcode = socket_last_error(); 
     $errormsg = socket_strerror($errorcode); 

     die("Error (#304)"); 
    } 
    $reply = socket_read($socket, 16384) 
      or die("Error (#305)"); 

    socket_close($socket); 
    return $reply; 
} 

如果有人知道如何解決這個問題,請幫助我。
在此先感謝!

回答

0

您應該測試socket_write()的返回值是否發送的數據量與發送的數據量相同,即strlen($cmd)字節。如果不是這種情況,則將其餘的寫入套接字,直到發送完所有數據爲止。

socket_write()的文檔:

socket_write()不一定寫從給定的緩衝器中的所有字節。根據網絡緩衝區等的不同,只有一定數量的數據(即使是一個字節)被寫入,儘管緩衝區更大,這是有效的。你必須小心,這樣你纔不會無意中忘記傳輸其餘的數據。

+0

感謝您的建議!它似乎發送儘可能多的數據,因爲它被告知。 'cmd bytes:44 |寫的字節:44' 這很奇怪,因爲一切正常工作沒有問題,除非我們正在處理一個大字符串 – Corl 2014-09-21 11:50:41

+0

嗯......你顯示的輸入是非常大,然後44bytes。 @RsMusicRs – alk 2014-09-21 11:53:59

+0

我在問題中提出的是輸出,而不是輸入:) 輸入不是很長。這是發送到服務器的命令:'4159a4030602995553945d2e2b807d46console help'。 @alk – Corl 2014-09-21 11:56:45