udp_server.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014 Norwegian University of Science and Technology
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Norwegian University of Science and
18  * Technology, nor the names of its contributors may be used to
19  * endorse or promote products derived from this software without
20  * specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /*
37  * Author: Lars Tingelstad
38  */
39 
40 #ifndef KUKA_RSI_HARDWARE_INTERFACE_UDP_SERVER_
41 #define KUKA_RSI_HARDWARE_INTERFACE_UDP_SERVER_
42 
43 #include <iostream>
44 #include <string>
45 #include <stdexcept>
46 
47 // Select includes
48 #include <sys/time.h>
49 
50 #include <string.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <netdb.h>
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59 
60 #define BUFSIZE 1024
61 
62 class UDPServer
63 {
64 public:
65  UDPServer(std::string host, unsigned short port) : local_host_(host), local_port_(port), timeout_(false)
66  {
67  sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
68  if (sockfd_ < 0)
69  throw std::runtime_error("Error opening socket: " + std::string(strerror(errno)));
70  optval = 1;
71  setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));
72  memset(&serveraddr_, 0, sizeof(serveraddr_));
73  serveraddr_.sin_family = AF_INET;
74  serveraddr_.sin_addr.s_addr = inet_addr(local_host_.c_str());
75  serveraddr_.sin_port = htons(local_port_);
76  if (bind(sockfd_, (struct sockaddr *) &serveraddr_, sizeof(serveraddr_)) < 0)
77  throw std::runtime_error("Error binding socket: " + std::string(strerror(errno)));
78  clientlen_ = sizeof(clientaddr_);
79  }
80 
82  {
83  close(sockfd_);
84  }
85 
86  bool set_timeout(int millisecs)
87  {
88  if (millisecs != 0)
89  {
90  tv_.tv_sec = millisecs / 1000;
91  tv_.tv_usec = (millisecs % 1000) * 1000;
92  timeout_ = true;
93  return timeout_;
94  }
95  else
96  {
97  return timeout_;
98  }
99  }
100 
101  ssize_t send(std::string& buffer)
102  {
103  ssize_t bytes = 0;
104  bytes = sendto(sockfd_, buffer.c_str(), buffer.size(), 0, (struct sockaddr *) &clientaddr_, clientlen_);
105  if (bytes < 0)
106  {
107  std::cout << "ERROR in sendto" << std::endl;
108  }
109 
110  return bytes;
111  }
112 
113  ssize_t recv(std::string& buffer)
114  {
115  ssize_t bytes = 0;
116 
117  if (timeout_)
118  {
119  fd_set read_fds;
120  FD_ZERO(&read_fds);
121  FD_SET(sockfd_, &read_fds);
122 
123  struct timeval tv;
124  tv.tv_sec = tv_.tv_sec;
125  tv.tv_usec = tv_.tv_usec;
126 
127  if (select(sockfd_+1, &read_fds, NULL, NULL, &tv) < 0)
128  {
129  return 0;
130  }
131 
132  if (FD_ISSET(sockfd_, &read_fds))
133  {
134  memset(buffer_, 0, BUFSIZE);
135  bytes = recvfrom(sockfd_, buffer_, BUFSIZE, 0, (struct sockaddr *) &clientaddr_, &clientlen_);
136  if (bytes < 0)
137  {
138  std::cout << "ERROR in recvfrom" << std::endl;
139  }
140  }
141  else
142  {
143  return 0;
144  }
145 
146  }
147  else
148  {
149  memset(buffer_, 0, BUFSIZE);
150  bytes = recvfrom(sockfd_, buffer_, BUFSIZE, 0, (struct sockaddr *) &clientaddr_, &clientlen_);
151  if (bytes < 0)
152  {
153  std::cout << "ERROR in recvfrom" << std::endl;
154  }
155  }
156 
157  buffer = std::string(buffer_);
158 
159  return bytes;
160  }
161 
162 private:
163  std::string local_host_;
164  unsigned short local_port_;
165  bool timeout_;
166  struct timeval tv_;
167 
168  int sockfd_;
169  socklen_t clientlen_;
170  struct sockaddr_in serveraddr_;
171  struct sockaddr_in clientaddr_;
173  int optval;
174 
175 };
176 
177 #endif
178 
UDPServer(std::string host, unsigned short port)
Definition: udp_server.h:65
ssize_t send(std::string &buffer)
Definition: udp_server.h:101
struct sockaddr_in serveraddr_
Definition: udp_server.h:170
#define BUFSIZE
Definition: udp_server.h:60
int optval
Definition: udp_server.h:173
socklen_t clientlen_
Definition: udp_server.h:169
std::string local_host_
Definition: udp_server.h:163
struct timeval tv_
Definition: udp_server.h:166
ssize_t recv(std::string &buffer)
Definition: udp_server.h:113
~UDPServer()
Definition: udp_server.h:81
bool set_timeout(int millisecs)
Definition: udp_server.h:86
struct sockaddr_in clientaddr_
Definition: udp_server.h:171
char buffer_[BUFSIZE]
Definition: udp_server.h:172
int sockfd_
Definition: udp_server.h:168
unsigned short local_port_
Definition: udp_server.h:164
bool timeout_
Definition: udp_server.h:165


kuka_rsi_hw_interface
Author(s): Lars Tingelstad
autogenerated on Tue Oct 15 2019 03:33:54