36 if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
38 std::cout <<
"ERROR: WSAStartup error code " << WSAGetLastError() << std::endl;
42 if ((
sock_=socket(AF_INET, SOCK_DGRAM, 0))==-1){
43 std::cout <<
"ERROR: Could not create socket!" << std::endl;
54 siMe_.sin_family = AF_INET;
55 siMe_.sin_port = htons((
short)port);
56 if(0 == localInterface.compare(
"255.255.255.255"))
57 siMe_.sin_addr.s_addr = htonl(INADDR_ANY);
59 siMe_.sin_addr.s_addr = inet_addr(localInterface.c_str());
63 std::cout <<
"ERROR: Could not bind socket, error code " << WSAGetLastError() << std::endl;
70 if ((
sock_=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
71 std::cout <<
"ERROR: Could not create socket!" << std::endl;
76 setsockopt(
sock_, SOL_SOCKET, SO_REUSEADDR, &one,
sizeof(one));
80 siMe_.sin_family = AF_INET;
81 siMe_.sin_port = htons((
short)port);
82 if(0 == localInterface.compare(
"255.255.255.255"))
83 siMe_.sin_addr.s_addr = htonl(INADDR_ANY);
85 siMe_.sin_addr.s_addr = inet_addr(localInterface.c_str());
89 std::cout <<
"ERROR: Could not bind socket!" << std::endl;
101 cout <<
"ERROR: UDP socket has not been initialized!" << endl;
106 FD_ZERO(&udpWaitSet);
107 FD_SET(
sock_, &udpWaitSet);
110 struct timeval timeout={0,timeoutMicroSec};
113 int status = select(
sock_+1, &udpWaitSet, NULL, NULL, &timeout);
124 if(FD_ISSET(
sock_, &udpWaitSet)){
128 int recvLength = recvfrom(
sock_, data, dataSize, 0, (
struct sockaddr *)&
siOther_, (socklen_t *)&
slen_);
131 int recvLength = recvfrom(
sock_, (
char*)data, dataSize, 0, (
struct sockaddr *)&
siOther_, &
slen_);
int receive(void *data, unsigned int dataSize, int timeoutMicroSec)
bool init(int port, std::string localInterface)
struct sockaddr_in siOther_