/****************************************************************************/
/* 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
///----------------------------------------------------------------------------