send.cpp
Go to the documentation of this file.
00001 
00023 #include "udp_bc_broker/send.h"
00024 
00025 using namespace std;
00026 
00027 namespace udp_bc_broker{
00028 
00029     UdpSender::UdpSender(int port)
00030     {
00031         setvbuf(stdout, NULL, _IONBF, 0);
00032         fflush(stdout);
00033 
00034         sock = -1;
00035         if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
00036             cout<<"socket is error!"<<endl;
00037             exit(-1);
00038         }
00039 
00040         const int opt = 1;
00041         int nb = 0;
00042         nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
00043         if(nb == -1) {
00044             cout<<"set socket error!"<<endl;
00045             exit(-1);
00046         }
00047 
00048         bzero(&addrto, sizeof(struct sockaddr_in));
00049         addrto.sin_family = AF_INET;
00050         addrto.sin_addr.s_addr = htonl(INADDR_BROADCAST);
00051         addrto.sin_port = htons(port);
00052         nlen = sizeof(addrto);
00053     };
00054 
00055     void UdpSender::send(const char* msg, int len)
00056     {
00057         int ret = sendto(sock, msg, len, 0, (sockaddr*)&addrto, nlen);
00058         if(ret < 0) {
00059             cout<<"send error, ret = "<<ret<<endl;
00060         }
00061         else {
00062             /*cout<<"send msg len: "<<len<<", data: ";
00063 
00064             for(int i = 0; i < ret; i++) {
00065                 std::cout<<(int)msg[i];
00066             }
00067             std::cout<<endl;*/
00068         }
00069     }
00070 };


udp_bc_broker
Author(s):
autogenerated on Thu Jun 6 2019 18:52:50