SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Private Member Functions | List of all members
UDPSocket Class Reference

#include <UDPSocket.h>

+ Inheritance diagram for UDPSocket:
+ Collaboration diagram for UDPSocket:

Public Member Functions

 UDPSocket ()
 
 UDPSocket (unsigned short localPort)
 
 UDPSocket (const string &localAddress, unsigned short localPort)
 
void DisconnectFromHost ()
 
void SendDataGram (const void *buffer, int bufferLen, const string &foreignAddress, unsigned short foreignPort)
 
int RecvDataGram (void *buffer, int bufferLen, string &sourceAddress, unsigned short &sourcePort)
 
void SetMulticastTTL (unsigned char multicastTTL)
 
void JoinGroup (const string &multicastGroup)
 
void LeaveGroup (const string &multicastGroup)
 
- Public Member Functions inherited from CSocket
virtual ~CSocket ()
 
string GetLocalAddress ()
 
unsigned short GetLocalPort ()
 
void BindLocalPort (unsigned short localPort)
 
void BindLocalAddressAndPort (const string &localAddress, unsigned short localPort=0)
 
unsigned long int GetReadBufferSize ()
 
void SetReadBufferSize (unsigned int nSize)
 
void SetNonBlocking (bool bBlocking)
 
void ConnectToHost (const string &foreignAddress, unsigned short foreignPort)
 
void Send (const void *buffer, int bufferLen)
 
int Recv (void *buffer, int bufferLen)
 
string GetPeerAddress ()
 
unsigned short GetPeerPort ()
 
CSocketoperator<< (const string &sStr)
 
CSocketoperator>> (string &sStr)
 
virtual int OnDataRead (unsigned long timeToWait=ULONG_MAX)
 
void SetBindToDevice (const string &sInterface)
 

Private Member Functions

void SetBroadcast ()
 

Additional Inherited Members

- Public Types inherited from CSocket
enum  SocketType { TcpSocket = SOCK_STREAM, UdpSocket = SOCK_DGRAM, UnknownSocketType =-1 }
 
enum  NetworkLayerProtocol { IPv4Protocol = AF_INET, IPv6Protocol = AF_INET6, UnknownNetworkLayerProtocol = -1 }
 
enum  ReadResult { DATA_ARRIVED = 0, DATA_TIMED_OUT = ETIMEDOUT, DATA_EXCEPTION = 255 }
 
- Protected Member Functions inherited from CSocket
 CSocket (SocketType type, NetworkLayerProtocol protocol)
 
 CSocket (int sockDesc)
 
- Static Protected Member Functions inherited from CSocket
static void FillAddr (const string &localAddress, unsigned short localPort, sockaddr_in &localAddr)
 
- Protected Attributes inherited from CSocket
int m_sockDesc
 

Detailed Description

UDP Socket class.

Definition at line 251 of file UDPSocket.h.

Constructor & Destructor Documentation

◆ UDPSocket() [1/3]

UDPSocket::UDPSocket ( )

Construct a UDP socket

Exceptions
SocketExceptionthrown if unable to create UDP socket

Definition at line 299 of file UDPSocket.cpp.

References SetBroadcast().

300 {
301  SetBroadcast();
302 }
CSocket(SocketType type, NetworkLayerProtocol protocol)
Definition: UDPSocket.cpp:53
void SetBroadcast()
Definition: UDPSocket.cpp:401
+ Here is the call graph for this function:

◆ UDPSocket() [2/3]

UDPSocket::UDPSocket ( unsigned short  localPort)

Construct a UDP socket with the given local port

Parameters
localPortlocal port
Exceptions
SocketExceptionthrown if unable to create UDP socket

Definition at line 304 of file UDPSocket.cpp.

References CSocket::BindLocalPort(), and SetBroadcast().

304  :
306 {
307  BindLocalPort(localPort);
308  SetBroadcast();
309 }
CSocket(SocketType type, NetworkLayerProtocol protocol)
Definition: UDPSocket.cpp:53
void BindLocalPort(unsigned short localPort)
Definition: UDPSocket.cpp:99
void SetBroadcast()
Definition: UDPSocket.cpp:401
+ Here is the call graph for this function:

◆ UDPSocket() [3/3]

UDPSocket::UDPSocket ( const string &  localAddress,
unsigned short  localPort 
)

Construct a UDP socket with the given local port and address

Parameters
localAddresslocal address
localPortlocal port
Exceptions
SocketExceptionthrown if unable to create UDP socket

Definition at line 311 of file UDPSocket.cpp.

References CSocket::BindLocalAddressAndPort(), and SetBroadcast().

311  :
313 {
314  BindLocalAddressAndPort(localAddress, localPort);
315  SetBroadcast();
316 }
CSocket(SocketType type, NetworkLayerProtocol protocol)
Definition: UDPSocket.cpp:53
void BindLocalAddressAndPort(const string &localAddress, unsigned short localPort=0)
Definition: UDPSocket.cpp:113
void SetBroadcast()
Definition: UDPSocket.cpp:401
+ Here is the call graph for this function:

Member Function Documentation

◆ DisconnectFromHost()

void UDPSocket::DisconnectFromHost ( )

Unset foreign address and port

Returns
true if disassociation is successful
Exceptions
SocketExceptionthrown if unable to disconnect UDP socket Unset foreign address and port
Returns
true if disassociation is successful
Exceptions
SocketExceptionthrown if unable to disconnect UDP socket

Definition at line 318 of file UDPSocket.cpp.

References CSocket::m_sockDesc.

319 {
320  sockaddr_in nullAddr;
321  memset(&nullAddr, 0, sizeof(nullAddr));
322  nullAddr.sin_family = AF_UNSPEC;
323  // Try to disconnect
324  if (::connect(m_sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0)
325  {
326  if (errno != EAFNOSUPPORT)
327  {
328  throw CSocketException("Disconnect failed (connect())", true);
329  }
330  }
331 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ JoinGroup()

void UDPSocket::JoinGroup ( const string &  multicastGroup)

Join the specified multicast group

Parameters
multicastGroupmulticast group address to join
Exceptions
SocketExceptionthrown if unable to join group

Definition at line 371 of file UDPSocket.cpp.

References CSocket::m_sockDesc.

372 {
373  struct ip_mreq multicastRequest;
374 
375  multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
376  multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
377  if (setsockopt(m_sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
378  (void *) &multicastRequest,
379  sizeof(multicastRequest)) < 0)
380  {
381  throw CSocketException("Multicast group join failed (setsockopt())", true);
382  }
383 
384 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ LeaveGroup()

void UDPSocket::LeaveGroup ( const string &  multicastGroup)

Leave the specified multicast group

Parameters
multicastGroupmulticast group address to leave
Exceptions
SocketExceptionthrown if unable to leave group

Definition at line 386 of file UDPSocket.cpp.

References CSocket::m_sockDesc.

387 {
388  struct ip_mreq multicastRequest;
389 
390  multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
391  multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
392  if (setsockopt(m_sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
393  (void *) &multicastRequest,
394  sizeof(multicastRequest)) < 0)
395  {
396  throw CSocketException("Multicast group leave failed (setsockopt())", true);
397  }
398 
399 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ RecvDataGram()

int UDPSocket::RecvDataGram ( void *  buffer,
int  bufferLen,
string &  sourceAddress,
unsigned short &  sourcePort 
)

Read read up to bufferLen bytes data from this socket. The given buffer is where the data will be placed

Parameters
bufferbuffer to receive data
bufferLenmaximum number of bytes to receive
sourceAddressaddress of datagram source
sourcePortport of data source
Returns
number of bytes received and -1 for error
Exceptions
SocketExceptionthrown if unable to receive datagram

Definition at line 346 of file UDPSocket.cpp.

References CSocket::m_sockDesc.

347 {
348  sockaddr_in clntAddr;
349  socklen_t addrLen = sizeof(clntAddr);
350  int nBytes;
351  if ((nBytes = recvfrom(m_sockDesc, (void *) buffer, bufferLen, 0, (sockaddr *) &clntAddr,
352  (socklen_t *) &addrLen)) < 0)
353  {
354  throw CSocketException("Receive failed (recvfrom())", true);
355  }
356  sourceAddress = inet_ntoa(clntAddr.sin_addr);
357  sourcePort = ntohs(clntAddr.sin_port);
358  char* sData = static_cast<char *>(buffer);
359  sData[nBytes] = '\0';
360  return nBytes;
361 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ SendDataGram()

void UDPSocket::SendDataGram ( const void *  buffer,
int  bufferLen,
const string &  foreignAddress,
unsigned short  foreignPort 
)

Send the given buffer as a UDP datagram to the specified address/port

Parameters
bufferbuffer to be written
bufferLennumber of bytes to write
foreignAddressaddress (IP address or name) to send to
foreignPortport number to send to
Returns
true if send is successful
Exceptions
SocketExceptionthrown if unable to send datagram

Definition at line 333 of file UDPSocket.cpp.

References CSocket::FillAddr(), and CSocket::m_sockDesc.

335 {
336  sockaddr_in destAddr;
337  FillAddr(foreignAddress, foreignPort, destAddr);
338  // Write out the whole buffer as a single message.
339  if (sendto(m_sockDesc, (void *) buffer, bufferLen, 0,(sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen)
340  {
341  throw CSocketException("Send failed (sendto())", true);
342  }
343 
344 }
static void FillAddr(const string &localAddress, unsigned short localPort, sockaddr_in &localAddr)
Definition: UDPSocket.cpp:124
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the call graph for this function:

◆ SetBroadcast()

void UDPSocket::SetBroadcast ( )
private

Definition at line 401 of file UDPSocket.cpp.

References CSocket::m_sockDesc.

Referenced by UDPSocket().

402 {
403  // If this fails, we'll hear about it when we try to send. This will allow
404  // system that cannot broadcast to continue if they don't plan to broadcast
405  int broadcastPermission = 1;
406  setsockopt(m_sockDesc, SOL_SOCKET, SO_BROADCAST,
407  (void *) &broadcastPermission, sizeof(broadcastPermission));
408 
409 }
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the caller graph for this function:

◆ SetMulticastTTL()

void UDPSocket::SetMulticastTTL ( unsigned char  multicastTTL)

Set the multicast TTL

Parameters
multicastTTLmulticast TTL
Exceptions
SocketExceptionthrown if unable to set TTL

Definition at line 363 of file UDPSocket.cpp.

References CSocket::m_sockDesc.

364 {
365  if (setsockopt(m_sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, (void *) &multicastTTL, sizeof(multicastTTL)) < 0)
366  {
367  throw CSocketException("Multicast TTL set failed (setsockopt())", true);
368  }
369 }
int m_sockDesc
Definition: UDPSocket.h:235

The documentation for this class was generated from the following files: