A portable socket interface for reading and writing to an open socket. More...
#include <Net.h>
Public Member Functions | |
Sock (SOCKET new_sock=INVALID_SOCKET) | |
Encapsulate a new socket, receiving the base socket type of the system (int or SOCKET). More... | |
void | Close () |
Manually close the socket. More... | |
void | SetRemote (RStr remote_addr, RStr remote_port) |
For record-keeping, set the remote address and port. | |
RStr | GetRemoteAddr () const |
Get the remote address this socket is connected to. | |
RStr | GetRemotePort () const |
Get the remote port this socket is connected to. | |
bool | DataReady (bool block_until_ready=false) const |
Returns true if data is ready for reading. More... | |
bool | CanSend (bool block_until_ready=false) const |
Returns true if the socket is ready to send data. More... | |
FileRW | ToFileRW () |
On unix-based systems, return a FileRW coresponding to this socket. More... | |
SOCKET | Raw () const |
Return the raw encapsulated socket. | |
template<class T > | |
size_t | Recv (Data1D< T > buf, bool do_block=true) |
Receive data from the socket into buf, up to the size of buf. More... | |
size_t | Recv (RStr &str, bool crop_newline=true, bool do_block=true) |
Receive characters into str until a newline or the end of data. More... | |
template<class T > | |
size_t | Send (Data1D< T > buf, bool do_block=true) |
Sends the contents of buf through the socket. More... | |
size_t | Send (RStr str, bool do_block=true) |
Sends the contents of str through the socket. More... | |
A portable socket interface for reading and writing to an open socket.
This class shares its data in a reference-counted manner upon assignment or copy construction, and automatically closes the socket as the last instance goes out of scope.
|
inline |
Encapsulate a new socket, receiving the base socket type of the system (int or SOCKET).
Use Connect or Listener::Accept to initialize this.
|
inline |
Returns true if the socket is ready to send data.
If block_until_ready is true this will wait until data can be sent or an error occurs, such as the socket closing or a signal being received.
|
inline |
Manually close the socket.
Note this is handled automatically when all copies of the Sock go out of scope.)
|
inline |
Returns true if data is ready for reading.
If block_until_ready is true this will wait until data is available or an error occurs, such as the socket closing or a signal being received. (Note that rare kernel level events can cause data to no longer be available after it was reported as available.)
|
inline |
Receive data from the socket into buf, up to the size of buf.
buf | The buffer into which data is received. |
do_block | If true, wait until some data is available. |
|
inline |
Receive characters into str until a newline or the end of data.
str | The string into which a line is received. |
crop_newline | If true, removes "\r\n" and "\n" from the end. |
do_block | If true, keeps reading until newline or the socket closes. If false, reads until newline or no data is available. |
|
inline |
Sends the contents of buf through the socket.
buf | The buffer containing the data to send. |
do_block | If true, sends all of buf. If false, sends as much of buf as can be sent. |
|
inline |
Sends the contents of str through the socket.
str | The string containing the data to send. |
do_block | If true, sends all of str. If false, sends as much of str as can be sent. |
|
inline |
On unix-based systems, return a FileRW coresponding to this socket.
Throws ErrorMsgNet if it cannot convert the socket.