Async 1.8.0
Async::UdpSocket Class Reference

A class for working with UDP sockets. More...

#include <AsyncUdpSocket.h>

Inheritance diagram for Async::UdpSocket:
Async::EncryptedUdpSocket

Public Member Functions

 UdpSocket (uint16_t local_port=0, const IpAddress &bind_ip=IpAddress())
 Constructor.
 
virtual ~UdpSocket (void)
 Destructor.
 
virtual bool initOk (void) const
 Check if the initialization was ok.
 
Async::IpAddress localAddr (void) const
 Get the local IP address associated with this connection.
 
uint16_t localPort (void) const
 Get the local UDP port associated with this connection.
 
virtual bool write (const IpAddress &remote_ip, int remote_port, const void *buf, int count)
 Write data to the remote host.
 
virtual int fd (void) const
 Get the file descriptor for the UDP socket.
 

Public Attributes

sigc::signal< void, const IpAddress &, uint16_t, void *, int > dataReceived
 A signal that is emitted when data has been received.
 
sigc::signal< void, bool > sendBufferFull
 A signal that is emitted when the send buffer is full.
 

Protected Member Functions

virtual void onDataReceived (const IpAddress &ip, uint16_t port, void *buf, int count)
 

Detailed Description

A class for working with UDP sockets.

Author
Tobias Blomberg
Date
2003-04-26

This class is used to work with UDP sockets. An example usage is shown below.

#include <iostream>
#include <AsyncUdpSocket.h>
#include <AsyncIpAddress.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
sock = new UdpSocket(12345);
sock->dataReceived.connect(mem_fun(*this, &MyClass::onDataReceived));
IpAddress addr("127.0.0.1");
sock->write(addr, 12345, "Hello, UDP!\n", 13);
}
~MyClass(void)
{
delete sock;
}
private:
UdpSocket * sock;
void onDataReceived(const IpAddress& addr, uint16_t port,
void *buf, int count)
{
cout << "Data received from " << addr << ":" << port << ": "
<< static_cast<char *>(buf);
Application::app().quit();
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
The core class for writing asyncronous cpp applications.
Platform independent representation of an IP address.
Contains a class for using UDP sockets.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
A class for representing an IP address in an OS independent way.
A class for working with UDP sockets.
Namespace for the asynchronous programming classes.
Examples
AsyncUdpSocket_demo.cpp.

Definition at line 124 of file AsyncUdpSocket.h.

Constructor & Destructor Documentation

◆ UdpSocket()

Async::UdpSocket::UdpSocket ( uint16_t local_port = 0,
const IpAddress & bind_ip = IpAddress() )

Constructor.

Parameters
local_portThe local port to use. If not specified, a random local port will be used.
bind_ipBind to the interface with the given IP address. If left empty, bind to all interfaces.

◆ ~UdpSocket()

virtual Async::UdpSocket::~UdpSocket ( void )
virtual

Destructor.

Member Function Documentation

◆ fd()

virtual int Async::UdpSocket::fd ( void ) const
inlinevirtual

Get the file descriptor for the UDP socket.

Returns
Returns the file descriptor associated with the socket or -1 on error

Definition at line 179 of file AsyncUdpSocket.h.

◆ initOk()

virtual bool Async::UdpSocket::initOk ( void ) const
inlinevirtual

Check if the initialization was ok.

Returns
Returns true if everything went fine during initialization or false if something went wrong

This function should always be called after constructing the object to see if everything went fine.

Reimplemented in Async::EncryptedUdpSocket.

Definition at line 149 of file AsyncUdpSocket.h.

Referenced by Async::EncryptedUdpSocket::initOk().

◆ localAddr()

Async::IpAddress Async::UdpSocket::localAddr ( void ) const

Get the local IP address associated with this connection.

Returns
Returns an IP address

◆ localPort()

uint16_t Async::UdpSocket::localPort ( void ) const

Get the local UDP port associated with this connection.

Returns
Returns a port number

◆ onDataReceived()

virtual void Async::UdpSocket::onDataReceived ( const IpAddress & ip,
uint16_t port,
void * buf,
int count )
protectedvirtual

Reimplemented in Async::EncryptedUdpSocket.

◆ write()

virtual bool Async::UdpSocket::write ( const IpAddress & remote_ip,
int remote_port,
const void * buf,
int count )
virtual

Write data to the remote host.

Parameters
remote_ipThe IP-address of the remote host
remote_portThe remote port to use
bufA buffer containing the data to send
countThe number of bytes to write
Returns
Return true on success or false on failure

Reimplemented in Async::EncryptedUdpSocket.

Member Data Documentation

◆ dataReceived

sigc::signal<void, const IpAddress&, uint16_t, void*, int> Async::UdpSocket::dataReceived

A signal that is emitted when data has been received.

Parameters
ipThe IP-address the data was received from
portThe remote port number
bufThe buffer containing the read data
countThe number of bytes read

Definition at line 188 of file AsyncUdpSocket.h.

◆ sendBufferFull

sigc::signal<void, bool> Async::UdpSocket::sendBufferFull

A signal that is emitted when the send buffer is full.

Parameters
is_fullSet to true if the buffer is full or false if the buffer full condition has been cleared

Definition at line 195 of file AsyncUdpSocket.h.


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