现在的位置: 首页 > 综合 > 正文

Boost的UDP接收server示例

2013年10月04日 ⁄ 综合 ⁄ 共 2654字 ⁄ 字号 评论关闭

废话不多说直接上代码:

GetConfig函数见本栏其他文章。功能:读取文本配置。

#include <sys/wait.h>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "boost/bind.hpp"





class receiver_
{
public:
  receiver_(boost::asio::io_service& io_service,
      const boost::asio::ip::address& listen_address,
      const boost::asio::ip::address& multicast_address,string src_port)
    : socket_(io_service),
      timer_(io_service)
  {
    // Create the socket so that multiple may be bound to the same address.
	  stringstream buffer ; buffer<<src_port ; int PORT = 0 ;
	  buffer>>PORT;
    boost::asio::ip::udp::endpoint listen_endpoint(
        listen_address, PORT);
    socket_.open(listen_endpoint.protocol());
    socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    socket_.bind(listen_endpoint);

    // Join the multicast group.
    socket_.set_option(
        boost::asio::ip::multicast::join_group(multicast_address));


    timer_.expires_from_now(boost::posix_time::seconds(1));
    timer_.async_wait(
        boost::bind(&receiver_::handle_timeout, this,
          boost::asio::placeholders::error));


  }

  void handle_receive_from(const boost::system::error_code& error,
      size_t bytes_recvd)
  {
    if (!error)
    {
    	printf("Data has coming \n");
    	int rtn ;
    	int pid=fork() ;
    	if ( pid == 0 ) {
    		/* 子进程程序 */
        	this->timer_.cancel();
    		ReadTrackMSGBuf(data_,bytes_recvd);
        	exit(1);
    	}
    	else {
    		/* 父进程程序*/
			printf("Father waiting \n");
//			signal(SIGCHLD,SIG_IGN);
			waitpid(pid,&rtn,0);
//			wait(pid);
			printf("Child Back\n");
    	}
    }
    else{
    	Fprint_String(error.message().c_str(),"StdTrackError","a++");
    }
  }
  void handle_timeout(const boost::system::error_code& error)
  {
    if (!error)
    {
        memset(data_,0,max_length);

    	socket_.async_receive_from(
            boost::asio::buffer(data_, max_length), sender_endpoint_,
            boost::bind(&receiver_::handle_receive_from, this,
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));

      timer_.cancel();
      timer_.expires_from_now(boost::posix_time::milliseconds(100));
      timer_.async_wait(
          boost::bind(&receiver_::handle_timeout, this,
            boost::asio::placeholders::error));

    }
    else{
    	Fprint_String(error.message().c_str(),"StdTrackError","a++");
    }
  }

private:
  boost::asio::deadline_timer timer_;
  boost::asio::ip::udp::socket socket_;
  boost::asio::ip::udp::endpoint sender_endpoint_;
  enum { max_length = 102400 };
  char data_[max_length];
};

int main_UDP_Receive()
{
  try
  {
    boost::asio::io_service io_service;

    char *Addr = GetConfigVal("TrackConfig","SourceAddr");
    char *Port = GetConfigVal("TrackConfig","SourcePort");
    if(Addr==NULL || Port==NULL)
    {
    	printf("TrackConfig Lost\n");
    	Fprint_String("TrackConfig Lost\n","StdTrackError","a+");
    	exit(-1);
    }
    string StrAddr(Addr); string StrPort(Port); free(Addr); free(Port);

	receiver_ r(io_service,
		boost::asio::ip::address::from_string("0.0.0.0"),
		boost::asio::ip::address::from_string(StrAddr.c_str()),StrPort.c_str());
	io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

抱歉!评论已关闭.