介绍
- VRPN即虚拟现实外围网络,主要用于实现应用程序与一组物理设备之间的网络透明接口。通过VRPN你可以连接到设备所在的服务器,然后获取该设备的数。
- VRPN是跨平台的,可以在许多OS上运行,包括windows,Linux以及Mac。
- 比如:当局域网下的主机A允许了VRPN的Server程序并且按下了“A”键,那么我们可以在该局域网下通过主机B通过VRPN的Cilent端获取主机A按下“A”键的操作。
源代码
- Client:
/*检测键盘按键*/
#include "vrpn_Button.h"
#include <iostream>
using namespace std;
void VRPN_CALLBACK handle_button(void *user, const vrpn_BUTTONCB button)
{
cout << "Button:" << button.button << endl;
}
int main(int argc, char *argv[])
{
/*定义模拟输出的驱动,构造函数参数为设备名加网络地址,本地可以@localhost*/
vrpn_Button_Remote *vrpnButton = new vrpn_Button_Remote("Keyboard0@192.168.1.206");
/*将设备输入的button数据与函数handle_button绑定*/
vrpnButton->register_change_handler(0, handle_button);
/*循环检测设备消息*/
while (1)
{
vrpnButton->mainloop();
}
return 0;
}
注意
在搭建过程中,我们需要先运行server端来接受外设的消息,然后server将消息传递给client端也就是我们上面的那段代码,然后client才能做出响应。