ASL
PacketSocket Class Reference

Detailed Description

A communication socket for UDP/IP protocol.

A listening side binds to a port and receives packets. The readFrom() functions also identifies the sender so you can reply.

PacketSocket socket;
socket.bind(port);
InetAddress sender;
auto data = socket.readFrom(sender);
socket.sendTo(sender, String("hi"));

The sender just sends packets to an endpoint:

PacketSocket socket;
socket.sendTo(InetAddress("localhost", port), data);

#include <Socket.h>

Inheritance diagram for PacketSocket:
Socket MulticastSocket

Public Member Functions

String void sendTo (const InetAddress &addr, const void *data, int n)
 Sends n bytes from data as a packet to the address addr.
 
void sendTo (const InetAddress &addr, const ByteArray &data)
 Sends a data packet to the address addr.
 
int readFrom (InetAddress &addr, void *data, int n)
 Reads an incoming packet into the buffer data of size n, writes the address of the sending peer and returns the actual packet size.
 
ByteArray readFrom (InetAddress &addr, int n=1000)
 Reads an incoming packet of at most n bytes and returns it, and gets the address of the sending peer.
 
- Public Member Functions inherited from Socket
bool disconnected ()
 Checks if the connection was lost.
 
bool connected ()
 Checks if the connection is open.
 
bool bind (const String &ip, int port)
 Binds this socket to the given IP address and port number.
 
bool bind (int port)
 Binds this socket with the given port number to all IPv4 interfaces.
 
void listen (int n=5)
 Makes this socket listen to incoming connections.
 
Socket accept ()
 For a listening socket, waits for an incoming connection and returns a socket to communicate with the remote peer.
 
InetAddress remoteAddress () const
 Returns the adress of the remote peer for a connected socket.
 
InetAddress localAddress () const
 Returns the adress of this connected socket if bound.
 
bool connect (const String &host, int port)
 Connects this socket to the given host name and port, trying all addresses assigned to that name until one succeeds.
 
bool connect (const String &host)
 Connects this socket to the given address given as a string "host:port" or "[ipv6]:port".
 
void close ()
 Closes this socket.
 
String readLine ()
 Reads a line of text from the socket (keep in mind this is not buffered, can be a bit inefficient, and there is a length limit of 8000 bytes)
 
int available ()
 Returns the number of bytes available for reading without blocking.
 
int read (void *data, int size)
 Reads size bytes from the socket into the bufffer pointed to by data.
 
int write (const void *data, int n)
 Writes n bytes from the bufffer pointed to by data to the socket.
 
int write (const ByteArray &data)
 Writes the byte array to the socket and returns the number of bytes actually sent.
 
ByteArray read (int n=-1)
 Reads n bytes and returns them as an array of bytes, or reads all available bytes if no argument is given.
 
void skip (int n)
 Skips (reads and discards) the next n bytes from the socket.
 
bool waitInput (double timeout=2)
 Waits until there is incoming data in the socket or it is disconnected for a maximum time, and returns true if some of that happened before timeout.
 
bool waitData (double timeout=2)
 Waits until there is incoming data in the socket or it is disconnected for a maximum time, and returns true only if there is data to read (false may mean no data or disconnection)
 
int error () const
 Returns true if there was some communication error in this socket.
 
String errorMsg () const
 Returns a string representation of the last socket error.
 
template<class T >
Socketoperator<< (const T &x)
 Writes variable x to the socket respecting endianness in binary form.
 
template<class T >
Socketoperator>> (T &x)
 Reads variable x from the socket respecting endianness in binary form.
 
String readString (int n)
 Reads n bytes from the socket a string.
 
Socketoperator>> (String &x)
 Reads a string from the socket that is preceded by its length as an int32 (the sender must send the byte length before) More...
 

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