tcpsocket Class Reference

A tcp connection class. More...

#include <tcpsocket.h>

Inheritance diagram for tcpsocket:
Inheritance graph
[legend]
Collaboration diagram for tcpsocket:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 tcpsocket (void)
 ~tcpsocket (void)
bool connect (const string &host, int hport)
bool connect (const char *host, int port)
bool connect (ipaddress addr, int hport)
bool uconnect (const string &)
bool bindtoaddr (ipaddress)
void getcredentials (void)
void sendcredentials (void)
filegetfd (void)
void sendfd (file &fl)
void sendfile (const string &path, unsigned int sz)
tcpsocketoperator= (tcpsocket &orig)
tcpsocketoperator= (tcpsocket *orig)

Public Attributes

pid_t peer_pid
uid_t peer_uid
gid_t peer_gid
ipaddress peer_addr
string peer_name
int peer_port
time_t ti_established
ipaddress localbindaddr

Protected Member Functions

void derive (tcpsocket *s)
void derive (tcpsocket &s)

Detailed Description

A tcp connection class.

Implements blocking and non-blocking interaction with another host over an inbound or outbound tcp connection.

Usage example:

#include <grace/application.h>
#include <grace/tcpsocket.h>

/// RADB lookup application.
/// Connects to whois.radb.net and queries the first commandline argument.
class radblookupApp : public application
{
public:
         radblookupApp (void) : application ("example.app.radblookup")
         {
         }
        ~radblookupApp (void)
         {
         }

    int  main (void);
};

APPOBJECT(radblookupApp);

int radblookupApp::main (void)
{
    tcpsocket outsock;
    string line;
    
    // Handle connection-related exceptions.
    try
    {
        // Try to connect to the RADB whois host.
        if (outsock.connect ("whois.radb.net", 63))
        {
            // Send the whois query.
            outsock.printf ("%s\r\n", argv["*"][0].cval());
            
            // Get all reply data.
            while (! outsock.eof())
            {
                line = outsock.gets();
                fout.printf ("%s\n", line.str());
            }
            
            // Close the connection.
            outsock.close();
        }
        else // Connect failed.
        {
            ferr.printf ("%% Connection error\n");
            return 1;
        }
    }
    catch (...)
    {
        ferr.printf ("%% Connection exception\n");
        return 1;
    }
    return 0;
}
Type Constraints

Member Function Documentation

bool tcpsocket::bindtoaddr ( ipaddress  address  ) 

Set address to connect from when using connect( .

. ) First use this before connecting

Parameters:
address Address to bind

References localbindaddr.

bool tcpsocket::connect ( ipaddress  addr,
int  hport 
)

Connect to an IPv4 host by address.

Returns true if connection succeeded.

Parameters:
host Hostname or dotted quad to connect to.
hport TCP port to use.
Returns:
Status, true if connection succeeded.
Exceptions:
socketCreateException Error creating a BSD socket.
EX_SSL_INIT Error initializing sslclientcodec.

References iocodec::addinput(), file::buffer, file::close(), file::codec, connect(), string::crop(), iocodec::doneoutput(), file::err, file::errcode, iocodec::error(), file::feof, iocodec::fetchinput(), file::filno, ringbuffer::flush(), localbindaddr, iocodec::peekoutput(), peer_addr, peer_port, string::printf(), file::read(), iocodec::reset(), iocodec::setup(), string::str(), string::strlen(), and ti_established.

Here is the call graph for this function:

bool tcpsocket::connect ( const string host,
int  hport 
)

Connect to an IPv4 host by name.

Returns true if connection succeeded.

Parameters:
host Hostname or dotted quad to connect to.
hport TCP port to use.
Returns:
Status, true if connection succeeded.
Exceptions:
socketCreateException Error creating a BSD socket.
EX_SSL_INIT Error initializing sslclientcodec.

References netdb::resolve().

Referenced by connect(), httpsocket::connectToHost(), smtpsocket::dosmtp(), and uconnect().

Here is the call graph for this function:

void tcpsocket::derive ( tcpsocket s  )  [protected]

Backend implementation of all derivation methods.

Flushes the internal buffer. Takes over the other object's filno and eof status. Copies the original's buffer contents.

Parameters:
s Reference to the original object.

References file::buffer, file::err, file::errcode, file::feof, file::filno, ringbuffer::flush(), peer_addr, peer_gid, peer_name, peer_pid, peer_port, peer_uid, and ti_established.

Here is the call graph for this function:

void tcpsocket::derive ( tcpsocket s  )  [protected]

Backend implementation of all derivation methods.

Flushes the internal buffer. Takes over the other object's filno and eof status. Copies the original's buffer contents. Then gets rid of the original.

Parameters:
s Pointer to the original object.

References file::buffer, file::err, file::errcode, file::feof, file::filno, ringbuffer::flush(), peer_addr, peer_gid, peer_name, peer_pid, peer_port, peer_uid, and ti_established.

Referenced by operator=().

Here is the call graph for this function:

file * tcpsocket::getfd ( void   ) 

Receive a file descriptor.

This uses special features of Unix Domain sockets that may not be available on all platforms. The program on the other side of the socket can use file::sendfd() to initiate this transfer.

Returns:
Pointer to new file object bound to the descriptor.

References file::filno, and file::openread().

Here is the call graph for this function:

tcpsocket & tcpsocket::operator= ( tcpsocket orig  ) 

Derive from other tcpsocket.

Deletes original.

Parameters:
orig Pointer to the original object.
Returns:
Reference to self.

Reimplemented in sslsocket.

References derive().

Here is the call graph for this function:

tcpsocket & tcpsocket::operator= ( tcpsocket orig  ) 

Derive from other tcpsocket.

Parameters:
orig The original socket.
Returns:
Reference to self.

References derive().

Here is the call graph for this function:

void tcpsocket::sendfd ( file fl  ) 

Hand over a file descriptor.

Uses special features in the implementation of Unix Domain sockets that allows file descriptors to be passed from one process to another. This feature is only available for the Linux kernel family.

Parameters:
fl The file to share with the other process.

References file::filno.

void tcpsocket::sendfile ( const string path,
unsigned int  sz 
)

Use sendfile to send a disk file.

Some kernels have a sendfile system call that can be used to instruct the kernel to send the contents of a disk file directly to a socket, bypassing expensive context-switches.

Parameters:
path Absolute path of the file.
sz Number of bytes to send.

References file::close(), file::eof(), file::filno, file::openread(), file::puts(), file::read(), and string::strlen().

Referenced by httpdfileshare::run(), and httpd::sendfile().

Here is the call graph for this function:

bool tcpsocket::uconnect ( const string path  ) 

Connect to a Unix Domain socket.

Returns:
Status, true if connection succeeded.
Exceptions:
socketCreateException Error creating a BSD socket.
EX_SSL_INIT Error initializing sslclientcodec.

References file::buffer, file::close(), file::codec, connect(), string::crop(), file::err, file::errcode, iocodec::error(), file::feof, file::filno, ringbuffer::flush(), peer_addr, peer_name, peer_port, string::printf(), iocodec::setup(), string::str(), ti_established, and filesystem::transr().

Referenced by httpsocket::get(), and httpsocket::post().

Here is the call graph for this function:


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

Generated on Sun Feb 28 10:36:52 2010 for GRACE by  doxygen 1.6.1