1 #include "UDPServer.hh"
2 #include<Base/Common/W32Compat.hh>
8 # include <sys/socket.h>
9 # include <sys/types.h>
10 # include <arpa/inet.h>
19 #include <Base/Debug/Error.hh>
20 #include <Base/Debug/TimeMeasure.hh>
24 #include <Base/Common/BIASpragma.hh>
29 UDPServer::UDPServer() {
30 socket_descriptor_ = 0;
35 TCPinsteadOfUDP_ =
false;
41 UDPServer::~UDPServer() {
42 delete[] send_buffer_;
47 int UDPServer::Send(
const char *message,
const size_t message_size)
49 BIASCDOUT(D_UDP_SEND,
"Send: "<<message_size<<endl);
51 if (socket_descriptor_ <= 0) {
52 BIASERR(
"UDPServer:send(): not connected. Use Connect()");
60 BIASCDOUT(D_UDP_SEND,
"Message counter: "<<message_counter_<<endl);
67 register int message_frames_total;
69 BIASCDOUT(D_UDP_SEND,
"Message frames to send image: "<<message_frames_total<<endl);
72 message_frames_total--;
74 MessageSize_ = (
unsigned int)message_size +
88 int actual_data_length = data_length;
93 for (
int i = 0; i < message_frames_total; i++) {
96 if ( i == (message_frames_total - 1) ) {
97 actual_data_length = (int)message_size - (i * data_length);
101 memcpy(data_start, &((
char*) message)[i * data_length], actual_data_length);
106 if (TCPinsteadOfUDP_) {
107 bytes_sent = send( socket_descriptor_,
115 bytes_sent = sendto( socket_descriptor_,
119 (
struct sockaddr*) &UDPSockAddr_,
120 sizeof(UDPSockAddr_));
121 BIASCDOUT(D_UDP_SEND,
"Sendto in "<<i<<
" th iteration."<<endl);
123 if (bytes_sent < 0) {
124 syscallerror(
"Failed to send via UDP Socket. Stop sending packages!");
126 int error = WSAGetLastError();
131 biasusleep(Throttle_ * 1000);
143 int UDPServer::SendToUDP (
const char* message,
const size_t message_size,
const char* ipAddress,
int port) {
145 if (TCPinsteadOfUDP_) {
146 BIASERR(
"UDPServer: SendToUDP is only usable with UDP sockets");
150 BIASCDOUT(D_UDP_SEND,
"SendToUDP: "<< message_size << endl);
151 if (socket_descriptor_ <= 0) {
152 BIASERR(
"UDPServer: SendToUDP(): invalid socket descriptor, call Connect before sending");
160 BIASCDOUT(D_UDP_SEND,
"Message counter: "<<message_counter_<<endl);
163 message_counter_ = 0;
167 register int message_frames_total;
169 BIASCDOUT(D_UDP_SEND,
"Message frames to send image: "<<message_frames_total<<endl);
172 message_frames_total--;
174 MessageSize_ = (
unsigned int)message_size +
188 int actual_data_length = data_length;
191 for (
int i = 0; i < message_frames_total; i++) {
194 if ( i == (message_frames_total - 1) ) {
195 actual_data_length = (int)message_size - (i * data_length);
199 memcpy(data_start, &((
char*) message)[i * data_length], actual_data_length);
206 struct sockaddr_in UDPHostAddr;
210 if (strlen(ipAddress) < 16) {
212 addr = inet_addr(ipAddress);
213 if((remoteHost = gethostbyaddr((
char *) &addr, 4, AF_INET)) == NULL) {
214 syscallerror(
"Could not retrieve host information for ip address: " << ipAddress);
218 syscallerror(
"Error: supplied host IPv4 address string is too large.");
222 char* ip = inet_ntoa(*((
struct in_addr *)remoteHost->h_addr));
224 memset(&UDPHostAddr, 0,
sizeof(UDPHostAddr));
225 UDPHostAddr.sin_family = AF_INET;
226 UDPHostAddr.sin_addr.s_addr = inet_addr(ip);
227 UDPHostAddr.sin_port = htons(port);
230 bytes_sent = sendto(socket_descriptor_, send_buffer_,
232 (
struct sockaddr*) &UDPHostAddr,
sizeof(UDPHostAddr));
233 BIASCDOUT(D_UDP_SEND,
"Sendto in "<<i<<
" th iteration."<<endl);
235 if (bytes_sent < 0) {
236 syscallerror(
"Failed to send via UDP Socket. Stop sending packages!");
238 int error = WSAGetLastError();
243 biasusleep(Throttle_ * 1000);
249 int UDPServer::Connect(
const char* ipaddress_or_name,
int port,
bool UseTCP )
251 TCPinsteadOfUDP_ = UseTCP;
255 WORD wVersionRequested;
259 wVersionRequested = MAKEWORD(2, 2);
260 err = WSAStartup(wVersionRequested, &wsaData);
264 syscallerror(
"WSAStartup failed with error: "<< err);
270 if (!isalpha(ipaddress_or_name[0]))
272 if (strlen(ipaddress_or_name) < 16)
274 addr = inet_addr(ipaddress_or_name);
275 if((remoteHost = gethostbyaddr((
char *) &addr, 4, AF_INET))==NULL){
276 syscallerror(
"Could not retrieve IP address of client name: "<<ipaddress_or_name);
282 syscallerror(
"error: Supplied host IPv4 address string is too large.");
287 if ( (remoteHost = gethostbyname(ipaddress_or_name) ) == NULL) {
288 syscallerror(
"Could not retrieve IP address of client name: "<<ipaddress_or_name);
293 char* ipaddress = inet_ntoa(*((
struct in_addr *)remoteHost->h_addr));
296 memset( &UDPSockAddr_, 0,
sizeof(UDPSockAddr_));
297 UDPSockAddr_.sin_family = AF_INET;
298 UDPSockAddr_.sin_addr.s_addr = inet_addr(ipaddress);
299 UDPSockAddr_.sin_port = htons(port);
301 if (TCPinsteadOfUDP_) {
303 socket_descriptor_ = socket(AF_INET, SOCK_STREAM,0);
305 res=connect( socket_descriptor_, (sockaddr*)&UDPSockAddr_,
306 sizeof(UDPSockAddr_));
307 if (res <0) perror(
"Failed to connect");
313 socket_descriptor_ = socket(AF_INET, SOCK_DGRAM,0);
314 if (socket_descriptor_ < 0) {
315 cerr <<
"UDPServer: An error has occured while creating a UDP" <<
316 " socket. Could not send any data.\n";
324 int UDPServer::Connect(
int port,
bool UseTCP )
326 TCPinsteadOfUDP_ = UseTCP;
328 WORD wVersionRequested;
332 wVersionRequested = MAKEWORD(2, 2);
333 err = WSAStartup(wVersionRequested, &wsaData);
337 syscallerror(
"WSAStartup failed with error: "<< err);
343 memset( &UDPSockAddr_, 0,
sizeof(UDPSockAddr_));
344 UDPSockAddr_.sin_family = AF_INET;
345 UDPSockAddr_.sin_addr.s_addr = INADDR_ANY;
346 UDPSockAddr_.sin_port = htons(port);
348 if (TCPinsteadOfUDP_) {
350 socket_descriptor_ = socket(AF_INET, SOCK_STREAM,0);
352 res=connect( socket_descriptor_, (sockaddr*)&UDPSockAddr_,
353 sizeof(UDPSockAddr_));
354 if (res <0) perror(
"Failed to connect");
360 socket_descriptor_ = socket(AF_INET, SOCK_DGRAM,0);
361 if (socket_descriptor_ < 0) {
362 cerr <<
"UDPServer: An error has occured while creating a UDP" <<
363 " socket. Could not send any data.\n";
369 int UDPServer::Disconnect()
372 if(socket_descriptor_ >=0){
375 close(socket_descriptor_);
377 closesocket(socket_descriptor_);
const int maxMessageNumber
class TimeMeasure contains functions for timing real time and cpu time.
const int UDP_PACKAGE_SIZE