UdpConnection.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <unistd.h>
00010 #include <string.h>
00011 #include <netdb.h>
00012 #include <fcntl.h>
00013 #include <sys/time.h>
00014 #include <arpa/inet.h>
00015 #include <sys/socket.h>
00016 #include <errno.h>
00017 #include <sys/ioctl.h>
00018 
00019 #include "Error.h"
00020 
00021 #include "UdpConnection.h"
00022 
00023 // Public methods.
00024 
00025 UdpConnection::UdpConnection(const String &host, const String &service) : Connection()
00026 {
00027   struct sockaddr_in peer;
00028   int sock;
00029   int res;
00030 
00031   DEBUG("UDP connection to host '%s' with service '%s'.", CSTRING(host), CSTRING(service));
00032 
00033   if (FAILED(res = setAddress(CSTRING(host), CSTRING(service), "udp", &peer))) {
00034     failure(ERROR_BACKTRACE(res));
00035     return;
00036   }
00037 
00038   if ((sock = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00039     failure(ERROR(MSG_SOCKET_CANNOT_CREATE, strerror(errno)));
00040     return;
00041   }
00042 
00043   if (FAILED(res = setup(sock, &peer))) {
00044     failure(ERROR_BACKTRACE(res));
00045     return;
00046   }
00047 }
00048 
00049 UdpConnection::~UdpConnection()
00050 {
00051 }
00052 
00053 int UdpConnection::setBroadcast(const bool yes)
00054 {
00055   const int on = yes ? 1 : 0;
00056 
00057   if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&on,  sizeof(on)))
00058     return ERROR(MSG_SOCKET_CANNOT_SET_OPTION, strerror(errno));
00059 
00060   return OK;
00061 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1