我有一个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;
}
如果有人知道如何解决这个问题,请帮帮我 提前谢谢!
答案 0 :(得分:0)
您应该测试socket_write()
的返回值是否确实发送了与发送的数据一样多的数据,即strlen($cmd)
个字节。如果不是这种情况,请将其余部分写入套接字,直到所有数据都被发送出去。
来自socket_write()
的文档:
socket_write()不一定要写入给定缓冲区中的所有字节。它是有效的,根据网络缓冲区等,只有一定数量的数据,甚至一个字节,写入虽然你的缓冲区更大。您必须注意,以免无意中忘记传输其余数据。