gpt4 book ai didi

c++ - 通过从 C++ 到 nodejs 进程的套接字连接发送 250ms UDP 价格滴答,为什么?

转载 作者:行者123 更新时间:2023-11-28 05:07:23 26 4
gpt4 key购买 nike

尝试将 UDP 价格报价从 C++“talker”进程发送到 nodejs“listener”进程,我遇到了崩溃。

Tick talking and listening 适用于 C++ 到 C++ 进程。当我添加 Nodejs 片段监听器时,它爆炸了。

有什么见解吗?谢谢,基思 :^)

报价数据 看起来不错:

Price unicasted: 49.58
Price unicasted: 50.00
Price unicasted: 50.24
Price unicasted: 50.09
Price unicasted: 49.81

C++ 说话者

#define SERVERPORT "4950"

int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo;
int rv;
size_t numbytes;
char* address = "192.168.1.100";

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;

if ((rv = getaddrinfo(address, SERVERPORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);

char myPrice[7];
float f;
while(1) {
f = randPrice();
ftoa(f, myPrice, 2); // Convert price to string
myPrice[5] = '\n'; myPrice[6] = '\0';

std::cout << "Price unicasted: " << myPrice;
if ((numbytes = sendto(sockfd, myPrice, strlen(myPrice), 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) {
perror("talker: sendto");
exit(1);
}
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
freeaddrinfo(servinfo);
close(sockfd);

return 0;
}

Node.js 监听器

var PORT = 4950;
var HOST = '192.168.1.100';

var dgram = require('dgram');
var server = dgram.createSocket('udp4');

server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});

while(1) {
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
}

server.bind(PORT, HOST);

错误 Eek!

pickledEgg> node udp.listener.js 
#
# Fatal error in , line 0
# API fatal error handler returned after process out of memory
#
==== C stack trace ===============================
#
# Fatal error in , line 0
# API fatal error handler returned after process out of memory
#
==== C stack trace ===============================
<--- Last few GCs --->

[14801:0x102806800] 13404 ms: Mark-sweep 1385.7 (1425.8) -> 1385.7 (1441.3) MB, 1099.6 / 0.0 ms allocation failure scavenge might not succeed
[14801:0x102806800] 14533 ms: Mark-sweep 1400.7 (1441.3) -> 1400.7 (1441.8) MB, 1099.3 / 0.0 ms allocation failure scavenge might not succeed
[14801:0x102806800] 15852 ms: Mark-sweep 1401.5 (1441.8) -> 1401.5 (1456.3) MB, 1317.5 / 0.0 ms allocation failure scavenge might not succeed

<--- JS stacktrace --->
Cannot get stack trace in GC.
FATAL ERROR: MarkCompactCollector: semi-space copy, fallback in old gen Allocation failed - JavaScript heap out of memory
1: node::Abort() [/usr/local/bin/node]
2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]
3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
4: v8::internal::MarkCompactCollector::EvacuateNewSpaceVisitor::AllocateTargetObject(v8::internal::HeapObject*, v8::internal::HeapObject**) [/usr/local/bin/node]
5: v8::internal::MarkCompactCollector::EvacuateNewSpaceVisitor::Visit(v8::internal::HeapObject*) [/usr/local/bin/node]
6: v8::internal::MarkCompactCollector::Evacuator::EvacuatePage(v8::internal::Page*) [/usr/local/bin/node]
7: v8::internal::PageParallelJob<v8::internal::EvacuationJobTraits>::Task::RunInternal() [/usr/local/bin/node]
8: v8::internal::MarkCompactCollector::EvacuatePagesInParallel() [/usr/local/bin/node]
9: v8::internal::MarkCompactCollector::EvacuateNewSpaceAndCandidates() [/usr/local/bin/node]
10: v8::internal::MarkCompactCollector::CollectGarbage() [/usr/local/bin/node]
11: 0 node 0x0000000100bf1593 v8::base::debug::StackTrace::StackTrace() + 19
1 node 0x0000000100bee739 V8_Fatal + 233
2 node 0x0000000100143e48 v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) + 744
3 node 0x00000001005e03bb v8::internal::MarkCompactCollector::EvacuateNewSpaceVisitor::AllocateTargetObject(v8::internal::HeapObject*, v8::internal::HeapObject**) + 1019
4 node 0x00000001005df6be v8::internal::MarkCompactCollector::EvacuateNewSpaceVisitor::Visit(v8::internal::HeapObject*) + 318
5 node 0x00000001005eb8eb v8::internal::MarkCompactCollector::Evacuator::EvacuatePage(v8::internal::Page*) + 235
6 node 0x00000001005eb786 v8::internal::PageParallelJob<v8::internal::EvacuationJobTraits>::Task::RunInternal() + 198
7 node 0x0000000100b067d9 v8::platform::WorkerThread::Run() + 25
8 node 0x0000000100bf3187 v8::base::ThreadEntry(void*) + 87
9 libsystem_pthread.dylib 0x00007fff9250bc13 _pthread_body + 131
10 libsystem_pthread.dylib 0x00007fff9250bb90 _pthread_body + 0
11 libsystem_pthread.dylib 0x00007fff92509375 thread_start + 13
Illegal instruction: 4

编辑 1:后续错误

node udp.listener.js 
events.js:182
throw er; // Unhandled 'error' event
^

Error: bind EADDRINUSE 192.168.1.100:4950
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at _handle.lookup (dgram.js:242:18)
at _combinedTickCallback (internal/process/next_tick.js:105:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
at Function.Module.runMain (module.js:607:11)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3

最佳答案

您似乎在无限循环 while(1) 中插入了 message 事件处理程序,所以您得到了内存不足错误。很可能您只需要设置一次处理程序。

关于c++ - 通过从 C++ 到 nodejs 进程的套接字连接发送 250ms UDP 价格滴答,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com