PHP套接字客户端没有返回完整响应
我有一个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;
}
如果有人知道如何解决这个问题,请帮助我。
在此先感谢!
回答:
您应该测试socket_write()
的返回值是否发送的数据量与发送的数据量相同,即strlen($cmd)
字节。如果不是这种情况,则将其余的写入套接字,直到发送完所有数据为止。
从socket_write()
的文档:
socket_write()不一定写从给定的缓冲器中的所有字节。根据网络缓冲区等的不同,只有一定数量的数据(即使是一个字节)被写入,尽管缓冲区更大,这是有效的。你必须小心,这样你才不会无意中忘记传输其余的数据。
以上是 PHP套接字客户端没有返回完整响应 的全部内容, 来源链接: utcz.com/qa/263148.html