ubit
Public Member Functions | Protected Attributes | Friends | List of all members
ubit::USocket Class Reference

Ubit Simple Sockets. More...

#include <usocket.hpp>

Inheritance diagram for ubit::USocket:
ubit::UMService

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)
 see receiveBlock().
 
bool sendBlock (UOutbuf &)
 see receiveBlock().
 
bool receiveBlock (UInbuf &)
 simplified block oriented I/O. More...
 
bool sendBytes (const char *buffer, unsigned int size)
 see receiveBytes().
 
bool receiveBytes (char *buffer, unsigned int size)
 byte oriented I/O. More...
 

Protected Attributes

int remport
 
int sock
 
struct sockaddr_in * sin
 
USourceinput
 

Friends

class UServerSocket
 

Detailed Description

Ubit Simple Sockets.

Example:


creates a new socket connected to host "myhost" on port 666
  USocket* s = new USocket("myhost", 666);
adds a callback function so that socketInputCB(s) will be called each time
s receives data.
  s->onInput(ucall(s, socketInputCB));
  void socketInputCB(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.

Member Function Documentation

§ receiveBlock()

bool ubit::USocket::receiveBlock ( UInbuf buf)

simplified block oriented I/O.

this function makes 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:

  • the same UInbuf can be used in multiple calls of receiveBlock() (this may minimize the number of reallocations)
  • sendBlock() adds a two byte integer before the actual data to indicate its length. receiveBlock() removes this integer from the received data and uses this value to determine when the transaction is completed.

§ receiveBytes()

bool ubit::USocket::receiveBytes ( char *  buffer,
unsigned int  size 
)

byte oriented I/O.

  • sendBytes() sends 'size' bytes from 'buffer' to the socket.
  • receiveBytes() receives 'size' bytes from the socket and puts them into 'buffer'. 'buffer' MUST have been previoulsy allocated and be large enough. receiveBytes() will block until 'size' bytes have been received.
  • both functions write/read 'size' bytes in a single call (low-level functions send() and recv() are called until all data has been sent/received)

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