模拟流数据 [英] Simulating streaming data
问题描述
我正在尝试编写一个程序,该程序将从平面数据中读取数据并模拟流式传输,因此我可以测试无需连接和启动流式传输硬件即可读取流式传输数据的程序.
I'm trying to write a program that will read from a flat file of data and simulate streaming it so I can test a program that reads streaming data without having to connect and start up the streaming hardware.
实现此目标的更现实的方法是什么?我需要它以可变速度播放,具体取决于模拟的硬件.
What are the more realistic ways to accomplish this? I need it to stream at variable speeds depending on the hardware im simulating.
到目前为止,我的两个想法是一个程序写入一个命名管道,或者一个程序以我需要的速率写入一个虚拟串行端口.
My two ideas so far are a program that writes to a named pipe, or a program that writes to a virtual serial port at the rates I need.
是否有更好(更现实)的模拟流数据的方法?
Is there a better (more realistic) way of simulating streaming data?
推荐答案
设置后台进程,以所需的任何速率将文件写入套接字.在Perl中,
Set up a background process that writes the file to a socket at whatever rate you want. In Perl,
use Socket;
socketpair my $A, my $B, AF_UNIX, SOCK_STREAM, PF_UNSPEC;
if (fork() == 0) {
stream();
exit;
}
while (<$A>) {
print;
}
sub stream {
# output 1024 bytes/sec
select $B; $| = 1; # disable output buffering
open my $fh, '<', '/file/to/stream';
my $buffer;
while (my $n = read $fh, $buffer, 1024) {
sleep 1;
print $B $buffer;
}
close $fh;
}
这篇关于模拟流数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!