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 (int localPortNumber, bool enableBroadcasting=false)
 Creates an (uninitialised) datagram socket.
 
 ~DatagramSocket ()
 Destructor.
 
bool bindToPort (int localPortNumber)
 Binds the socket to the specified local port.
 
bool connect (const String &remoteHostname, int remotePortNumber, int timeOutMillisecs=3000)
 Tries to connect the socket to hostname:port.
 
bool isConnected () const noexcept
 True if the socket is currently connected.
 
void close ()
 Closes the connection.
 
const StringgetHostName () const noexcept
 Returns the name of the currently connected host.
 
int getPort () const noexcept
 Returns the port number that's currently open.
 
bool isLocal () const noexcept
 True if the socket is connected to this machine rather than over the network.
 
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 write (const void *sourceBuffer, int numBytesToWrite)
 Writes bytes to the socket from a buffer.
 
DatagramSocketwaitForNextConnection () const
 This waits for incoming data to be sent, and returns a socket that can be used to read it.
 

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 ( int  localPortNumber,
bool  enableBroadcasting = false 
)

Creates an (uninitialised) datagram socket.

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.

To use the socket for sending, call the connect() method. This will not immediately make a connection, but will save the destination you've provided. After this, you can call read() or write().

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

To wait for other sockets to connect to this one, call waitForNextConnection().

DatagramSocket::~DatagramSocket ( )

Destructor.

Member Function Documentation

bool DatagramSocket::bindToPort ( int  localPortNumber)

Binds the socket to the specified local port.

Returns
true on success; false may indicate that another socket is already bound on the same port
bool DatagramSocket::connect ( const String remoteHostname,
int  remotePortNumber,
int  timeOutMillisecs = 3000 
)

Tries to connect the socket to hostname:port.

If timeOutMillisecs is 0, then this method will block until the operating system rejects the connection (which could take a long time).

Returns
true if it succeeds.
See Also
isConnected
bool DatagramSocket::isConnected ( ) const
noexcept

True if the socket is currently connected.

void DatagramSocket::close ( )

Closes the connection.

const String& DatagramSocket::getHostName ( ) const
noexcept

Returns the name of the currently connected host.

int DatagramSocket::getPort ( ) const
noexcept

Returns the port number that's currently open.

bool DatagramSocket::isLocal ( ) const
noexcept

True if the socket is connected to this machine rather than over the network.

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::write ( 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.
DatagramSocket* DatagramSocket::waitForNextConnection ( ) const

This waits for incoming data to be sent, and returns a socket that can be used to read it.

The object that gets returned is owned by the caller, and can't be used for sending, but can be used to read the data.


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