#include <usocket.hpp>
Inheritance diagram for ubit::USocket:
Public Member Functions | |
USocket (const char *remote_host, int remote_port) | |
USocket (const UStr &remote_host, int remote_port) | |
virtual int | connect (const char *remote_host, int remote_port) |
virtual void | close () |
virtual void | onInput (UCall &) |
adds a callback that is fired when data is received on the socket. | |
bool | isConnected () const |
int | getRemotePort () const |
int | getDescriptor () const |
bool | sendBlock (const char *buffer, unsigned short size) |
bool | sendBlock (UOutbuf &) |
bool | receiveBlock (UInbuf &) |
simplified block oriented I/O. | |
bool | sendBytes (const char *buffer, unsigned int size) |
bool | receiveBytes (char *buffer, unsigned int size) |
byte oriented I/O. | |
Protected Attributes | |
int | remport |
int | sock |
sockaddr_in * | sin |
UInput * | input |
Friends | |
class | UServerSocket |
Example:
creates a new socket connected to host "myhost" on port 666
USocket* s = new USocket("myhost", 666);
adds a callback function so that foo(s) will be called each time
s receives data.
s->onInput(ucall(s, foo));
void foo(USocket* s) {
UInbuf ibuf;
receiveBlock() retreives the data sent by sendBlock()
if (s->receiveBlock(ibuf)) {
ibuf.data() contains the data that has been received. It is
allocated by receiveBlock() and freed when ibuf is destroyed.
char* received_data = ibuf.data();
ibuf.size() is the size of ibuf.data() (in bytes)
unssigned int received_count = ibuf.size();
scans the received data according to a predefined format
(a short int followed by a long int in this case)
short i; long l;
ibuf.readShort(i); ibuf.readLong(l); ...;
}
}
See also: UServerSocket.
bool ubit::USocket::receiveBlock | ( | UInbuf & | ) |
simplified block oriented I/O.
these functions make it possible to send/receive blocks of data of an arbitrary size. ONE call of receiveBlock() always gets ALL the data sent by ONE call of sendBlock().
Memory is managed automatically by classes UOutbuf and UInbuf. UInbuf.data() returns the data received by receiveBlock() and UInbuf.size() the number of bytes. UInbuf.data() is allocated and deallocated automatically in memory and should NOT be freed.
NOTES:
bool ubit::USocket::receiveBytes | ( | char * | buffer, | |
unsigned int | size | |||
) |
byte oriented I/O.