标签:blog http io os ar 2014 sp log on
普通的UDP组件发送和接收
接收端
//server.cpp #include <ace/SOCK_Dgram.h> #include <ace/INET_Addr.h> #include <ace/Time_Value.h> #include <string> #include <iostream> using namespace std; #include "boost/lexical_cast.hpp" using namespace boost; void receive() { ACE_INET_Addr port_to_listen(3000); //绑定的端口(本地地址) ACE_SOCK_Dgram peer(port_to_listen); //本地地址藏匿于此,发送的时候除了知道目的地址,也要携带本地地址的(因为UDP没有连接这一说) ACE_INET_Addr remoteAddr; //所连接的远程地址 char buf[100]={0}; int receive_length = peer.recv(buf,100,remoteAddr); //接收消息,获取远程地址信息 if( receive_length != -1) { string received(buf,receive_length); cout<<"server received:["<<received<<"]"<<endl; } } int main(int argc, char *argv[]) { receive(); return 0; }
发送端
#include <ace/SOCK_Dgram.h> #include <ace/INET_Addr.h> #include <ace/Time_Value.h> #include <ace/OS.h> #include <string> #include <iostream> using namespace std; #include "boost/lexical_cast.hpp" using namespace boost; void send(void) { ACE_INET_Addr remoteAddr(3000,"127.0.0.1"); //所连接的远程地址 ACE_INET_Addr local_addr; ACE_SOCK_Dgram peer(local_addr); //本地地址藏匿于此,发送的时候除了知道目的地址,也要携带本地地址的(因为UDP没有连接这一说) string send_str; double i =3.1415926535897932; send_str = lexical_cast<string>(i++).c_str(); int send_length = peer.send(send_str.c_str(),send_str.size(),remoteAddr); //发送消息到目的地址 if (send_length != -1) { cout<<"send "<<send_str<<endl; } } int main(int argc, char *argv[]) { send(); return 0; }
执行接收端之后再执行发送端,执行结果
ACE Proactor UDP (接收什么就打印什么)1.0
标签:blog http io os ar 2014 sp log on
原文地址:http://blog.csdn.net/calmreason/article/details/39344315