Logo
~Sockets~
~Examples~
~Contact~


UdpSocket.h

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #ifndef _SOCKETS_UdpSocket_H
00031 #define _SOCKETS_UdpSocket_H
00032 
00033 #include "sockets-config.h"
00034 #include "Socket.h"
00035 
00036 #ifdef SOCKETS_NAMESPACE
00037 namespace SOCKETS_NAMESPACE {
00038 #endif
00039 
00042 class UdpSocket : public Socket
00043 {
00044 public:
00049         UdpSocket(ISocketHandler& h,int ibufsz = 16384,bool ipv6 = false);
00050         ~UdpSocket();
00051 
00057         virtual void OnRawData(const char *buf,size_t len,struct sockaddr *sa,socklen_t sa_len);
00058 
00063         int Bind(port_t& port,int range = 1);
00069         int Bind(const std::string& intf,port_t& port,int range = 1);
00075         int Bind(ipaddr_t a,port_t& port,int range = 1);
00076 #ifdef ENABLE_IPV6
00077 #ifdef IPPROTO_IPV6
00078 
00083         int Bind(in6_addr a,port_t& port,int range = 1);
00084 #endif
00085 #endif
00086 
00090         int Bind(SocketAddress& ad,int range = 1);
00091 
00096         bool Open(ipaddr_t l,port_t port);
00101         bool Open(const std::string& host,port_t port);
00102 #ifdef ENABLE_IPV6
00103 #ifdef IPPROTO_IPV6
00104 
00108         bool Open(struct in6_addr& a,port_t port);
00109 #endif
00110 #endif
00111 
00114         bool Open(SocketAddress& ad);
00115 
00117         void SendToBuf(const std::string& ,port_t,const char *data,int len,int flags = 0);
00119         void SendToBuf(ipaddr_t,port_t,const char *data,int len,int flags = 0);
00120 #ifdef ENABLE_IPV6
00121 #ifdef IPPROTO_IPV6
00122 
00123         void SendToBuf(in6_addr,port_t,const char *data,int len,int flags = 0);
00124 #endif
00125 #endif
00126 
00127         void SendToBuf(SocketAddress& ad,const char *data,int len,int flags = 0);
00128 
00130         void SendTo(const std::string&,port_t,const std::string&,int flags = 0);
00132         void SendTo(ipaddr_t,port_t,const std::string&,int flags = 0);
00133 #ifdef ENABLE_IPV6
00134 #ifdef IPPROTO_IPV6
00135 
00136         void SendTo(in6_addr,port_t,const std::string&,int flags = 0);
00137 #endif
00138 #endif
00139 
00140         void SendTo(SocketAddress& ad,const std::string&,int flags = 0);
00141 
00143         void SendBuf(const char *data,size_t,int flags = 0);
00145         void Send(const std::string& ,int flags = 0);
00146 
00148         void SetBroadcast(bool b = true);
00151         bool IsBroadcast();
00152 
00154         void SetMulticastTTL(int ttl = 1);
00155         int GetMulticastTTL();
00156         void SetMulticastLoop(bool = true);
00157         bool IsMulticastLoop();
00158         void AddMulticastMembership(const std::string& group,const std::string& intf = "0.0.0.0",int if_index = 0);
00159         void DropMulticastMembership(const std::string& group,const std::string& intf = "0.0.0.0",int if_index = 0);
00160 #ifdef ENABLE_IPV6
00161 #ifdef IPPROTO_IPV6
00162 
00163         void SetMulticastHops(int = -1);
00165         int GetMulticastHops();
00166 #endif
00167 #endif
00168 
00169         bool IsBound();
00171         port_t GetPort();
00172 
00173         void OnOptions(int,int,int,SOCKET) {}
00174 
00175         int GetLastSizeWritten();
00176 
00177 protected:
00178         UdpSocket(const UdpSocket& s) : Socket(s) {}
00179         void OnRead();
00180 
00181 private:
00182         UdpSocket& operator=(const UdpSocket& ) { return *this; }
00184         void CreateConnection();
00185         char *m_ibuf; 
00186         int m_ibufsz; 
00187         bool m_bind_ok; 
00188         port_t m_port; 
00189         int m_last_size_written;
00190 };
00191 
00192 
00193 #ifdef SOCKETS_NAMESPACE
00194 }
00195 #endif
00196 
00197 #endif // _SOCKETS_UdpSocket_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4