A tcp connection class. More...
#include <tcpsocket.h>


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) |
| file * | getfd (void) |
| void | sendfd (file &fl) |
| void | sendfile (const string &path, unsigned int sz) |
| tcpsocket & | operator= (tcpsocket &orig) |
| tcpsocket & | operator= (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) |
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;
}
| bool tcpsocket::bindtoaddr | ( | ipaddress | address | ) |
Set address to connect from when using connect( .
. ) First use this before connecting
| 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.
| host | Hostname or dotted quad to connect to. | |
| hport | TCP port to use. |
| 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.

| bool tcpsocket::connect | ( | const string & | host, | |
| int | hport | |||
| ) |
Connect to an IPv4 host by name.
Returns true if connection succeeded.
| host | Hostname or dotted quad to connect to. | |
| hport | TCP port to use. |
| socketCreateException | Error creating a BSD socket. | |
| EX_SSL_INIT | Error initializing sslclientcodec. |
References netdb::resolve().
Referenced by connect(), httpsocket::connectToHost(), smtpsocket::dosmtp(), and uconnect().

| 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.
| 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.

| 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.
| 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=().

| 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.
References file::filno, and file::openread().

| 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.
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.
| 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().

| bool tcpsocket::uconnect | ( | const string & | path | ) |
Connect to a Unix Domain socket.
| 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().

1.6.1