29 UdpSender::UdpSender(
int port)
31 setvbuf(stdout, NULL, _IONBF, 0);
35 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
36 cout<<
"socket is error!"<<endl;
42 nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (
char *)&opt,
sizeof(opt));
44 cout<<
"set socket error!"<<endl;
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);
55 void UdpSender::send(
const char* msg,
int len)
57 int ret = sendto(sock, msg, len, 0, (sockaddr*)&addrto, nlen);
59 cout<<
"send error, ret = "<<ret<<endl;