首页 \ 问答 \ 从C接收的nodejs缓冲区分为两部分(nodejs' buffer received from C is divided into two parts)

从C接收的nodejs缓冲区分为两部分(nodejs' buffer received from C is divided into two parts)

我正在使用tcp传输数据。 服务器代码由C编写,客户端代码由nodejs编写。 当我发送一个缓冲区时,有时客户端将收到此缓冲区的两个部分,console.log函数将触发两次,但有时它运行良好。 以下是nodejs代码和C代码。 nodejs代码:

var client = new net.Socket();
client.on('data', function(data) {
  console.log('data:', data)
});

C代码:

send(socket_file, buffer, strlen(buffer),0)

I am using tcp to transfer data. The server code is written by C, and client code is written by nodejs. When I send one buffer, sometimes the client will receive two parts of this buffer, the console.log function will trigger twice, but sometimes it works well. following are nodejs code and C code. nodejs code:

var client = new net.Socket();
client.on('data', function(data) {
  console.log('data:', data)
});

C code:

send(socket_file, buffer, strlen(buffer),0)

原文:https://stackoverflow.com/questions/37941352
更新时间:2022-01-12 22:01

最满意答案

这是TCP的典型特征,它毕竟是面向流的(与面向分组的UDP相反)协议。

无法保证对网络的一次写入等于另一端的一次读取,可以一起传送多次写入,并且可以分割单次写入。

您必须添加应用程序级消息协议。


This is typical for TCP, which is a stream-oriented (as opposed to packet-oriented like UDP) protocol after all.

There's no guarantee that one write to the network equals one read at the other end, multiple writes may be delivered together and single writes may be split.

You must add an applicaton-level message protocol.

相关问答

更多

相关文章

更多

最新问答

更多
  • Google会更新重定向网址吗?(Does Google update redirect URLs? [closed])
  • 如何将js-variable从.js文件传递给Electron中的.html页面?(How to pass js-variable from .js file to .html page in Electron?)
  • matlab中的数据处理gui - 从文件和文本框中读取并传递给函数(data handling in matlab gui - reading from file and textbox and pass to function)
  • SVG矩形的笔划宽度在顶部/左侧被切除。(SVG rectangle's stroke widths are cut off on top/left. How do I fix this?)
  • BOOST_CHECK中的错误?(Bug in BOOST_CHECK?)
  • 使用ActiveDocument时记录替换量(Recording the amount of replacements when using ActiveDocument)
  • 将php数组转换为javascript数组(convert php array to a javascript array)
  • 在斯威夫特干邑国际象棋引擎与iOS项目集成(Stockfish Chess Engine Integration with iOS project in Swift)
  • C# - 增加一个双倍值(1.212E + 25)(C# - Incrementing a double value (1.212E+25))
  • 取消asynctask的最佳方法(Best way to cancel asynctask)
  • 文件转换问题的最佳方法(Best approach to File Conversion problems)
  • 从分组数据中选择第一行和最后一行(Select first and last row from grouped data)
  • 哪些元素可以禁用?(What elements can ng-disabled be applied to?)
  • ASP.NET GridView在回发时为空(ASP.NET GridView empty on postback)
  • lua环境和模块(lua environments and modules)
  • 如何将小切片复制到更大切片的末尾(How to copy a small slice to the end of a bigger slice)
  • IIS7 URL重写模块替换(IIS7 URL Rewriting Module Replace)
  • IE和Google Chrome中的多表单提交(Multi-forms submission in IE and Google chrome)
  • Symfony2:如何在控制器中检索UserChecker?(Symfony2: How to retrieve the UserChecker in a controller?)
  • 循环通过数组和删除项目,而不破坏循环(Looping through array and removing items, without breaking for loop)
  • 如何动态更改YouTube播放器的选项(How to dynamically change YouTube player's options)
  • Laravel使用以点开头的文件处理存储磁盘(Laravel working through storage disk with files starting with dot)
  • 在挂架中使用重定向时出错(Error using redirect in pylons)
  • 替换netCDF文件中的变量(Replacing a variable inside a netCDF file)
  • 让swable中的非可变数组(let Non-mutable array in swift)
  • 春季安全缺省登录页面代码位于何处?(Where is spring security default login page code located?)
  • Unix - 并排打印公共线(Unix - Print Common Lines side by side)
  • 验证TimeSpan(Validating TimeSpan)
  • 如果还有一个具有返回值的重载版本,如何正确调用非返回值Swift函数?(How do I properly call a non-return-value Swift function when it also has an overloading version that has returned value?)
  • Perl:比较两个文件并打印匹配和不匹配的数据(Perl: Comparing two files and printing data that match and don't match)