udpreceiver.cpp
Go to the documentation of this file.
1 /*
2  * Done by Antti Kolu, antti.kolu@tut.fi, anttikolu@gmail.com
3 */
4 
6 #include <iostream>
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include <unistd.h>
11 
12 using std::cout;
13 using std::endl;
14 using std::string;
15 
17 {
18  slen_=sizeof(siOther_);
19  initOK = false;
20 }
21 
23 {
24 #ifdef UNIX
25  close(sock_);
26 #endif
27 #ifdef WIN32
28  closesocket(sock_);
29  WSACleanup();
30 #endif
31 }
32 
33 bool UDPReceiver::init(int port, std::string localInterface) {
34 
35 #ifdef WIN32
36  if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
37  {
38  std::cout << "ERROR: WSAStartup error code " << WSAGetLastError() << std::endl;
39  return false;
40  }
41  // create UDP socket that will be used to receive data
42  if ((sock_=socket(AF_INET, SOCK_DGRAM, 0))==-1){
43  std::cout << "ERROR: Could not create socket!" << std::endl;
44  return false;
45  }
46 
47  //char broadcast = 1;
48  //setsockopt(sock_, SOL_SOCKET, SO_BROADCAST,
49  // &broadcast, sizeof broadcast);
50 
51  // Initialize socket address. Receive data from PORT and from user
52  // specified local address(or any address is 255.255.255.255).
53  memset((char *) &siMe_, 0, sizeof(siMe_));
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);
58  else
59  siMe_.sin_addr.s_addr = inet_addr(localInterface.c_str());
60 
61  // Bind socket into socket address
62  if (bind(sock_, (struct sockaddr *)&siMe_, sizeof(siMe_))==-1){
63  std::cout << "ERROR: Could not bind socket, error code " << WSAGetLastError() << std::endl;
64  return false;
65  }
66 #endif
67 
68 #ifdef UNIX
69  // create UDP socket that will be used to receive data
70  if ((sock_=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
71  std::cout << "ERROR: Could not create socket!" << std::endl;
72  return false;
73  }
74 
75  int one = 1;
76  setsockopt(sock_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
77 
78  // Initialize socket address. Receive data from PORT and from any address.
79  memset((char *) &siMe_, 0, sizeof(siMe_));
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);
84  else
85  siMe_.sin_addr.s_addr = inet_addr(localInterface.c_str());
86 
87  // Bind socket into socket address
88  if (bind(sock_, (struct sockaddr *)&siMe_, sizeof(siMe_))==-1){
89  std::cout << "ERROR: Could not bind socket!" << std::endl;
90  return false;
91  }
92 #endif
93 
94  initOK = true;
95  return true;
96 }
97 
98 int UDPReceiver::receive(void *data, unsigned int dataSize, int timeoutMicroSec)
99 {
100  if(!initOK){
101  cout << "ERROR: UDP socket has not been initialized!" << endl;
102  return false;
103  }
104 
105  fd_set udpWaitSet;
106  FD_ZERO(&udpWaitSet);
107  FD_SET(sock_, &udpWaitSet);
108 
109  // initialize timeout value that select will wait for UDP packet. 500ms
110  struct timeval timeout={0,timeoutMicroSec};
111 
112  // wait if there is any data to receive. If not, then continue to next loop.
113  int status = select(sock_+1, &udpWaitSet, NULL, NULL, &timeout);
114  if(status<0){
115  //cout << "Select returned unexpected value: " << status << " (Ctrl+c called?)" << endl;
116  return -2;
117  }else if(status==0){
118  //Timeout reached
119  //cout << "Timeout on UDP receive." << endl;
120  return -1;
121  }
122 
123  // if there is something to receive, check if it is from the right socket.
124  if(FD_ISSET(sock_, &udpWaitSet)){
125 
126  // receive the message into buf.
127 #ifdef UNIX
128  int recvLength = recvfrom(sock_, data, dataSize, 0, (struct sockaddr *)&siOther_, (socklen_t *)&slen_);
129 #endif
130 #ifdef WIN32
131  int recvLength = recvfrom(sock_, (char*)data, dataSize, 0, (struct sockaddr *)&siOther_, &slen_);
132 #endif
133  //cout << "received data from" << siOther_.sin_addr.s_addr << endl;
134  return recvLength;
135  }
136  return -2;
137 }
int receive(void *data, unsigned int dataSize, int timeoutMicroSec)
Definition: udpreceiver.cpp:98
bool init(int port, std::string localInterface)
Definition: udpreceiver.cpp:33
struct sockaddr_in siOther_
Definition: udpreceiver.h:41
struct sockaddr_in siMe_
Definition: udpreceiver.h:39


ifm_o3mxxx
Author(s):
autogenerated on Mon Jun 10 2019 13:34:12