send.cpp
Go to the documentation of this file.
1 
23 #include "udp_bc_broker/send.h"
24 
25 using namespace std;
26 
27 namespace udp_bc_broker{
28 
29  UdpSender::UdpSender(int port)
30  {
31  setvbuf(stdout, NULL, _IONBF, 0);
32  fflush(stdout);
33 
34  sock = -1;
35  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
36  cout<<"socket is error!"<<endl;
37  exit(-1);
38  }
39 
40  const int opt = 1;
41  int nb = 0;
42  nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
43  if(nb == -1) {
44  cout<<"set socket error!"<<endl;
45  exit(-1);
46  }
47 
48  bzero(&addrto, sizeof(struct sockaddr_in));
49  addrto.sin_family = AF_INET;
50  addrto.sin_addr.s_addr = htonl(INADDR_BROADCAST);
51  addrto.sin_port = htons(port);
52  nlen = sizeof(addrto);
53  };
54 
55  void UdpSender::send(const char* msg, int len)
56  {
57  int ret = sendto(sock, msg, len, 0, (sockaddr*)&addrto, nlen);
58  if(ret < 0) {
59  cout<<"send error, ret = "<<ret<<endl;
60  }
61  else {
62  /*cout<<"send msg len: "<<len<<", data: ";
63 
64  for(int i = 0; i < ret; i++) {
65  std::cout<<(int)msg[i];
66  }
67  std::cout<<endl;*/
68  }
69  }
70 };


udp_bc_broker
Author(s):
autogenerated on Mon Jun 10 2019 14:02:40