本文主要是介绍Nebula2探秘09-IpcPeer(UDP),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Nebula2探秘09-IpcPeer(UDP)
happykevins文
Nebula2有内置了两种IPC(进程间通讯)的解决方案:IpcServer(UDP)和IpcPeer(TCP);下面一段代码展示如何使用Nebula2的IpcPeer。
/* ************************************************************************** */
/* Nebula2 - Tutorial 09 */
/* IpcPeer - Using UDP */
/* author: happykevins */
/* ************************************************************************** */
/// ----------------------------------------------------------------------------
/// +必要头文件
// nebula2 includes
#include " kernel/nkernelserver.h "
#include " kernel/nipcpeer.h "
/// -必要头文件
/// ----------------------------------------------------------------------------
/// ----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
/// -链接库
/// ----------------------------------------------------------------------------
/// ----------------------------------------------------------------------------
/// +Application
int main( int argc, const char ** argv)
{
/// 创建KernelServer
nKernelServer * ks = n_new(nKernelServer);
/// 启动网络模块
WSADATA wsaData;
int err = WSAStartup( MAKEWORD( 2 , 2 ), & wsaData );
if ( err != 0 ) {
n_printf( " 无法启动网络模块! " );
return 0 ;
}
/// 初始化两个Peer用于发送和接收数据
nIpcAddress addr1( " self " , 7111 );
nIpcAddress addr2( " self " , 7222 );
nIpcPeer peer1( & addr1, nIpcPeer::CanSend);
nIpcPeer peer2( & addr2, nIpcPeer::CanReceive);
/// 测试发送5条信息
for ( int i = 1 ; i <= 5 ; i ++ )
{
nIpcBuffer send( 256 );
nIpcBuffer recv( 256 );
nIpcAddress from;
/// 发送信息
send.SetString( " Hello Peer2! I'm Peer1! " );
peer1.SendTo(addr2, send);
/// Sleep一段时间确保收到消息
n_sleep( 3.0 );
/// 接收消息
if ( peer2.ReceiveFrom(recv, from) )
{
if ( recv.IsValidString() )
{
n_printf( " ]Receive a Message from %s:%d " , from.GetHostName(), from.GetPortNum());
n_printf( " ]Message Content: %s " , recv.GetString());
}
}
}
/// 关闭网络模块
WSACleanup();
n_printf( " 按任意键退出... " );
/// 销毁KernelServer
n_delete(ks);
getchar();
return 0 ;
}
/// -Application
/// ----------------------------------------------------------------------------
/* Nebula2 - Tutorial 09 */
/* IpcPeer - Using UDP */
/* author: happykevins */
/* ************************************************************************** */
/// ----------------------------------------------------------------------------
/// +必要头文件
// nebula2 includes
#include " kernel/nkernelserver.h "
#include " kernel/nipcpeer.h "
/// -必要头文件
/// ----------------------------------------------------------------------------
/// ----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
/// -链接库
/// ----------------------------------------------------------------------------
/// ----------------------------------------------------------------------------
/// +Application
int main( int argc, const char ** argv)
{
/// 创建KernelServer
nKernelServer * ks = n_new(nKernelServer);
/// 启动网络模块
WSADATA wsaData;
int err = WSAStartup( MAKEWORD( 2 , 2 ), & wsaData );
if ( err != 0 ) {
n_printf( " 无法启动网络模块! " );
return 0 ;
}
/// 初始化两个Peer用于发送和接收数据
nIpcAddress addr1( " self " , 7111 );
nIpcAddress addr2( " self " , 7222 );
nIpcPeer peer1( & addr1, nIpcPeer::CanSend);
nIpcPeer peer2( & addr2, nIpcPeer::CanReceive);
/// 测试发送5条信息
for ( int i = 1 ; i <= 5 ; i ++ )
{
nIpcBuffer send( 256 );
nIpcBuffer recv( 256 );
nIpcAddress from;
/// 发送信息
send.SetString( " Hello Peer2! I'm Peer1! " );
peer1.SendTo(addr2, send);
/// Sleep一段时间确保收到消息
n_sleep( 3.0 );
/// 接收消息
if ( peer2.ReceiveFrom(recv, from) )
{
if ( recv.IsValidString() )
{
n_printf( " ]Receive a Message from %s:%d " , from.GetHostName(), from.GetPortNum());
n_printf( " ]Message Content: %s " , recv.GetString());
}
}
}
/// 关闭网络模块
WSACleanup();
n_printf( " 按任意键退出... " );
/// 销毁KernelServer
n_delete(ks);
getchar();
return 0 ;
}
/// -Application
/// ----------------------------------------------------------------------------
这篇关于Nebula2探秘09-IpcPeer(UDP)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!