29 UdpRecver::UdpRecver(
int port)
31 setvbuf(stdout, NULL, _IONBF, 0);
35 bzero(&addrto,
sizeof(
struct sockaddr_in));
36 addrto.sin_family = AF_INET;
37 addrto.sin_addr.s_addr = htonl(INADDR_ANY);
38 addrto.sin_port = htons(port);
41 bzero(&from,
sizeof(
struct sockaddr_in));
42 from.sin_family = AF_INET;
43 from.sin_addr.s_addr = htonl(INADDR_ANY);
44 from.sin_port = htons(port);
46 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
47 cout<<
"socket error!"<<endl;
53 nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (
char *)&opt,
sizeof(opt));
55 cout<<
"set socket error!"<<endl;
59 if(bind(sock,(
struct sockaddr *)&(addrto),
sizeof(
struct sockaddr_in)) == -1) {
60 cout<<
"bind error!"<<endl;
64 len =
sizeof(sockaddr_in);
65 buf =
new char[10000];
68 UdpRecver::~UdpRecver()
70 process_thread_->interrupt();
71 process_thread_->join();
72 delete process_thread_;
77 void UdpRecver::process_msg()
80 int ret = recvfrom(sock, buf, 10000, 0, (
struct sockaddr*)&from, (socklen_t*)&len);
82 cout<<
"read error...."<<sock<<endl;
85 std::vector<uint8_t> msg_data;
86 for(
int i = 0; i < ret; i++) {
87 msg_data.push_back(buf[i]);
95 void UdpRecver::receive(boost::function<
void(
const vector<uint8_t>&)> callBack)
98 process_thread_ =
new boost::thread(&UdpRecver::process_msg,
this);
void callback(const vector< uint8_t > &msg)