Async  1.5.0
Public Member Functions | Public Attributes | List of all members
Async::UdpSocket Class Reference

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

#include <AsyncUdpSocket.h>

Inheritance diagram for Async::UdpSocket:

Public Member Functions

 UdpSocket (uint16_t local_port=0, const IpAddress &bind_ip=IpAddress())
 Constructor. More...
 
 ~UdpSocket (void)
 Destructor. More...
 
bool initOk (void) const
 Check if the initialization was ok. More...
 
bool write (const IpAddress &remote_ip, int remote_port, const void *buf, int count)
 Write data to the remote host. More...
 
int fd (void) const
 Get the file descriptor for the UDP socket. More...
 

Public Attributes

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

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);
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
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()

Async::UdpSocket::~UdpSocket ( void  )

Destructor.

Member Function Documentation

◆ fd()

int Async::UdpSocket::fd ( void  ) const
inline

Get the file descriptor for the UDP socket.

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

Definition at line 167 of file AsyncUdpSocket.h.

◆ initOk()

bool Async::UdpSocket::initOk ( void  ) const
inline

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.

Definition at line 149 of file AsyncUdpSocket.h.

◆ write()

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

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

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 176 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 183 of file AsyncUdpSocket.h.


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