Python — клиент сокета PHP не возвращает полный ответ

У меня есть сервер сокетов Python и клиент сокетов PHP. Клиент отправляет команду на сервер и затем отображает ответ на странице. Он работает идеально почти все время. Теперь у меня проблема в том, что когда речь идет о длинных ответах, ответ, кажется, сокращается.

Это не сервер Python, поскольку вы получаете полный ответ через Telnet. Это что-то со сценарием PHP, что я не могу на всю жизнь, понять.

Вывод из Telnet:

{
"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(!@socket_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;
}

Если кто-нибудь знает, как решить эту проблему, пожалуйста, помогите мне.
Заранее спасибо!

1

Решение

Вы должны проверить возвращаемое значение socket_write() было ли отправлено столько данных, сколько было сказано, то есть strlen($cmd) байт. Если это не так, запишите остаток в сокет, пока все данные не будут отправлены.

От socket_write()Документация:

socket_write () не обязательно записывать все байты из данного буфера. Действительно, в зависимости от сетевых буферов и т. Д. Записывается только определенный объем данных, даже один байт, хотя ваш буфер больше. Вы должны остерегаться, чтобы не забыть непреднамеренно передать остальные данные.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector