首页 \ 问答 \ NetworkStream缺少数据(NetworkStream missing data)

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
更新时间:2023-05-23 07:05

最满意答案

您无需执行整个%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 call stringDecide for each match, substituting the string captured into the string parameter to stringDecide on each call, but what's in fact happening is that stringDecide is being called once with the literal parameter "%1" before string.gsub is even called, and it is returning "ERROR" as expected, basically expanding your string.gsub call in place to string.gsub(a, '{(.-)}', "ERROR").

相关问答

更多
  • 你可以使用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 } ...
  • if str:gsub("%w+!", {["apple!"]="", ["juice!"]="", ["cake!"]=""}) == "" then --do something end 此解决方案使用表作为string.gsub的第二个参数。 由于模式都匹配%w+ ,表格将第二次验证,只有真正的三个模式被替换为空字符串。 如果在所有替换后,字符串变空,则匹配成功。 使用辅助表变量可以使其更清晰: local t = {["apple!"]="", ["juice!"]="", ["cake! ...
  • 您可以从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 ...
  • 您无需执行整个%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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)