JUCE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Public Member Functions | List of all members
DatagramSocket Class Reference

A wrapper for a datagram (UDP) socket. More...

Public Member Functions

 DatagramSocket (bool enableBroadcasting=false)
 Creates a datagram socket.
 
 ~DatagramSocket ()
 Destructor.
 
bool bindToPort (int localPortNumber)
 Binds the socket to the specified local port.
 
int getBoundPort () const noexcept
 Returns the local port number to which this socket is currently bound.
 
int getRawSocketHandle () const noexcept
 Returns the OS's socket handle that's currently open.
 
int waitUntilReady (bool readyForReading, int timeoutMsecs) const
 Waits until the socket is ready for reading or writing.
 
int read (void *destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived)
 Reads bytes from the socket.
 
int read (void *destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived, String &senderIPAddress, int &senderPortNumber)
 Reads bytes from the socket and return the IP address of the sender.
 
int write (const String &remoteHostname, int remotePortNumber, const void *sourceBuffer, int numBytesToWrite)
 Writes bytes to the socket from a buffer.
 

Detailed Description

A wrapper for a datagram (UDP) socket.

This allows low-level use of sockets; for an easier-to-use messaging layer on top of sockets, you could also try the InterprocessConnection class.

See Also
StreamingSocket, InterprocessConnection, InterprocessConnectionServer

Constructor & Destructor Documentation

DatagramSocket::DatagramSocket ( bool  enableBroadcasting = false)

Creates a datagram socket.

You first need to bind this socket to a port with bindToPort if you intend to read from this socket.

If enableBroadcasting is true, the socket will be allowed to send broadcast messages (may require extra privileges on linux)

DatagramSocket::~DatagramSocket ( )

Destructor.

Member Function Documentation

bool DatagramSocket::bindToPort ( int  localPortNumber)

Binds the socket to the specified local port.

The localPortNumber is the port on which to bind this socket. If this value is 0, the port number is assigned by the operating system.

Returns
true on success; false may indicate that another socket is already bound on the same port
int DatagramSocket::getBoundPort ( ) const
noexcept

Returns the local port number to which this socket is currently bound.

This is useful if you need to know to which port the OS has actually bound your socket when bindToPort was called with zero.

Returns -1 if the socket didn't bind to any port yet or an error occured.

int DatagramSocket::getRawSocketHandle ( ) const
noexcept

Returns the OS's socket handle that's currently open.

int DatagramSocket::waitUntilReady ( bool  readyForReading,
int  timeoutMsecs 
) const

Waits until the socket is ready for reading or writing.

If readyForReading is true, it will wait until the socket is ready for reading; if false, it will wait until it's ready for writing.

If the timeout is < 0, it will wait forever, or else will give up after the specified time.

If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1.

int DatagramSocket::read ( void *  destBuffer,
int  maxBytesToRead,
bool  blockUntilSpecifiedAmountHasArrived 
)

Reads bytes from the socket.

If blockUntilSpecifiedAmountHasArrived is true, the method will block until maxBytesToRead bytes have been read, (or until an error occurs). If this flag is false, the method will return as much data as is currently available without blocking.

Returns
the number of bytes read, or -1 if there was an error.
See Also
waitUntilReady
int DatagramSocket::read ( void *  destBuffer,
int  maxBytesToRead,
bool  blockUntilSpecifiedAmountHasArrived,
String senderIPAddress,
int &  senderPortNumber 
)

Reads bytes from the socket and return the IP address of the sender.

If blockUntilSpecifiedAmountHasArrived is true, the method will block until maxBytesToRead bytes have been read, (or until an error occurs). If this flag is false, the method will return as much data as is currently available without blocking.

Returns
the number of bytes read, or -1 if there was an error. On a successful result, the senderIPAddress value will be set to the IP of the sender.
See Also
waitUntilReady
int DatagramSocket::write ( const String remoteHostname,
int  remotePortNumber,
const void *  sourceBuffer,
int  numBytesToWrite 
)

Writes bytes to the socket from a buffer.

Note that this method will block unless you have checked the socket is ready for writing before calling it (see the waitUntilReady() method).

Returns
the number of bytes written, or -1 if there was an error.

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