NetworkStream缺少数据(NetworkStream missing data)
有问题的数据是一个PNG文件,其大小为int。
-Sending: ns.Write(BitConverter.GetBytes((int)data.Length),0,4); ns.Write(data, 0, data.Length); -Reading: byte[] sizearray = new byte[4]; ns.Read(sizearray, 0, 4); int dataSize = BitConverter.ToInt32(sizearray,0); byte[] data = new byte[dataSize]; ns.Read(data, 0, dataSize);
然后将收到的数据保存到文件中。 我也用BeginRead / EndRead尝试了相同的结果。
问题是,虽然这适用于大多数较小的图像,但它不会接收超过几KB的图像。 dataSize读取正确,但每次几千字节(~2900)后,其余的接收数据为0。
我是否忽视了某些事情,比如限制一次可以发送多少钱?
The data in question is a PNG file prefixed by it's size as an int.
-Sending: ns.Write(BitConverter.GetBytes((int)data.Length),0,4); ns.Write(data, 0, data.Length); -Reading: byte[] sizearray = new byte[4]; ns.Read(sizearray, 0, 4); int dataSize = BitConverter.ToInt32(sizearray,0); byte[] data = new byte[dataSize]; ns.Read(data, 0, dataSize);
The recieved data is then saved to a file. I've also tried this with BeginRead/EndRead with the same result.
The issue is, while this works for most smaller images, it doesn't receive an image which is more then a few KB. The dataSize reads correctly, but after a few thousand bytes each time (~2900), the rest of the received data is 0. Example
Have I overlooked something, like a limit on how much can be sent at once?
原文:https://stackoverflow.com/questions/9995714
最满意答案
您无需执行整个%1操作即可将捕获传递给您的函数。 string.gsub的一种可能的操作模式是将一个函数作为参数,并在每次找到匹配时向其传递表示捕获的字符串/字符串数组:
捕获值的最后一次使用可能是最强大的。 我们可以使用函数作为第三个参数调用string.gsub,而不是替换字符串。 当以这种方式调用时,string.gsub每次找到匹配时都会调用给定的函数; 此函数的参数是捕获,而函数返回的值用作替换字符串。
考虑到这一点,您可以从现有代码中删除一些字符,以便传递函数而不是调用它:
function stringModify(a) return string.gsub(a, '{(.-)}', stringDecide) end
正如您所看到的 ,您的
stringDecide
函数将不会被修改。在你现有的代码中,你想要发生的是
string.gsub
将为每个匹配调用stringDecide
,在每次调用时将捕获到string参数中的字符串替换为stringDecide
,但事实上发生的是stringDecide
被调用一次甚至在调用string.gsub
之前使用文字参数"%1"
,它按预期返回"ERROR"
,基本上将string.gsub
调用扩展到string.gsub(a, '{(.-)}', "ERROR")
。You don't have to do the whole %1 thing to pass the captures over to your function. One possible mode of operation of string.gsub takes a function as an argument and passes it a string/array of strings representing the capture(s) every time it finds a match:
The last use of captured values is perhaps the most powerful. We can call string.gsub with a function as its third argument, instead of a replacement string. When invoked this way, string.gsub calls the given function every time it finds a match; the arguments to this function are the captures, while the value that the function returns is used as the replacement string.
Taking this into account, you can just remove a few characters from your existing code so you pass the function rather than calling it:
function stringModify(a) return string.gsub(a, '{(.-)}', stringDecide) end
and your
stringDecide
function will work unmodified, as you can see.In your existing code, what you're wanting to happen is that
string.gsub
will callstringDecide
for each match, substituting the string captured into the string parameter tostringDecide
on each call, but what's in fact happening is thatstringDecide
is being called once with the literal parameter"%1"
beforestring.gsub
is even called, and it is returning"ERROR"
as expected, basically expanding yourstring.gsub
call in place tostring.gsub(a, '{(.-)}', "ERROR")
.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
字符串到Lua中的表(String to Table in Lua)[2022-05-24]
你可以使用loadstring来创建一个你可以执行的lua块。 eventData = [[ months = { 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', } ]] loadstring(eventData)( ... -
正如@EgorSkriptunoff在原帖的注释部分指出的那样,printTable函数中的一个错误是函数返回到for循环而不是内部函数。 这已经解决并致力于github! 感谢您的帮助! as @EgorSkriptunoff pointed out in the comment section of the original post it was a bug in the printTable function where the function returned to the for loop i ...
-
error_get_last() ,如名称所示,只会给你最后一个错误。 事实上,大多数错误会阻止你的脚本运行,只会让你只有最后一个,而没有以前的脚本。 但是您可以设置自己的处理程序来捕获每个抛出的错误和异常。 这是一个例子 //function for exception handling function handle_exception (Exception $exception) { //here you can save the exception to your database } ...
-
当我们在Lua中没有`|`运算符时如何验证这个字符串?(How to validate this string when we don't have the `|` operator in Lua?)[2023-09-29]
if str:gsub("%w+!", {["apple!"]="", ["juice!"]="", ["cake!"]=""}) == "" then --do something end 此解决方案使用表作为string.gsub的第二个参数。 由于模式都匹配%w+ ,表格将第二次验证,只有真正的三个模式被替换为空字符串。 如果在所有替换后,字符串变空,则匹配成功。 使用辅助表变量可以使其更清晰: local t = {["apple!"]="", ["juice!"]="", ["cake! ... -
Lua将字符串保存到文件(Lua save string to file)[2022-02-18]
您可以从Lua中的Programming中阅读本章 ,了解如何使用文件; 当你打开文件然后调用f:write(shroom)进行f:write(shroom)操作时,你需要使用"w"模式。 对于在循环中睡觉,您可能需要检查此SO问题中的建议。 You can read this chapter from Programming in Lua to see how you can work with files; you'll need to use "w" mode when you open file a ... -
这不是Lua本身的问题。 > print("bisción" == "bisción") true 可能源代码编辑器使用的字符编码与数据源之间存在差异。 Lua在字节级进行比较操作。 例如,使用UTF-8编码的Lua源文件和从UTF-16编码的文件加载的数据就足够了,并且比较失败。 This is not a problem of Lua itself. > print("bisción" == "bisción") true Perhaps there is a discrepancy between ...
-
在C你有类似的东西 static int foo (lua_State *L) { int n = lua_gettop(L); //n is the number of arguments, use if needed lua_pushstring(L, str); //str is the const char* that points to your string return 1; //we are returning one value, the string } 在Lu ...
-
如何将捕获的内容与lua中的另一个字符串进行比较(How do I compare the contents of a capture with another string in lua)[2022-10-28]
您无需执行整个%1操作即可将捕获传递给您的函数。 string.gsub的一种可能的操作模式是将一个函数作为参数,并在每次找到匹配时向其传递表示捕获的字符串/字符串数组: 捕获值的最后一次使用可能是最强大的。 我们可以使用函数作为第三个参数调用string.gsub,而不是替换字符串。 当以这种方式调用时,string.gsub每次找到匹配时都会调用给定的函数; 此函数的参数是捕获,而函数返回的值用作替换字符串。 考虑到这一点,您可以从现有代码中删除一些字符,以便传递函数而不是调用它: function s ... -
如果需要从LuaJava加载/编译字符串,可以使用函数LuaState.LloadString(String source) 。 如果您不想多次从源加载“模块”,则必须为其分配名称并在表中保存一些标志。 您甚至可以提供“卸载”,以便您可以再次从源加载模块。 它可以在Lua中重新实现如下: do local loadedModules = {} -- local so it won't leak to _G function loadModule(name, source) if loaded ...