Examples Documentation Contact Downloads

Networking

Modern applications spend their days on the internet. And no application has sufficiently evolved until it can send mail. Grace is here to help you with that.

Using tcpsocket

With tcpsocket you can create a file object that is connected to a remote server. Its major distinction from a regular file object is the connect method, which gets the tcp connection magic going:

tcpsocket s;
if (! s.connect ("bofh.example.net", 666)) return false;

string excuse = s.gets ();
s.close ();
fout.writeln (excuse);
return true;

Where a disk file is either set for reading or for writing, a tcpsocket can go both ways. You can also connect to unix domain sockets using uconnect:

tcpsocket s;
if (! s.uconnect ("/dev/excuse")) return false;
// etc. etc.

Listening Sockets

A tcplistener is an object that listens for incoming connections on a tcp port or unix device socket. You can bind tcpsocket objects to connections it accepts.

void excuseServer (void)
{
    tcplistener lsock;
    tcpsocket sock;
    
    value excuses = $("Solar radiation") ->
                    $("Collision detection") ->
                    $("EMP");

    lsock.listento (666);
    while (true)
    {
        sock = lsock.accept ();
        int exc = rand() % excuses.count();
        sock.writeln (excuses[exc]);
        sock.close ();
    }
}

Interacting with HTTP Servers

HTTP is the new TCP in many environments. The httpsocket class is your gateway to web data:

#include "myapp.h"
#include <grace/http.h>

int myApp::main (void)
{
    httpsocket hs;
    string slash = hs.get ("http://slashdot.org");
    fout.writeln ("%i bytes, st %i" %format (slash.strlen(), hs.status));
    return 0;
}

Objects of this class normally try to use the HTTP 1.1 keepalive mechanism to keep the connection open for more requests — when a new request for the same domain is received, httpsocket will reuse it, as long as the object is still around. You can influence this through the keepalive method.

The authentication call can set up HTTP basic authentication:

httpsocket hs;
hs.authentication ("backend","s3ckr1t");
string secret = hs.get ("http://db.local:8112/getSecret");

You can also interact with the incoming and outgoing HTTP headers. For incoming headers, pass a reference to a value object as a second argument. For outgoing headers, use the setheader method:

httpsocket hs;
value rethdr;

hs.setheader ("User-Agent", "EarthWorm");
string dat = hs.get ("http://db.local:1225/cachelist", rethdr);
fout.writeln ("Data: %s" %format (rethdr["Content-type"]));

Posting Form Data

If you need to post data, there are several ways. The most direct way is to assume normal form encoding:

httpsocket hs;
string res;

res = hs.post ("http://db.local/submit", $("q","beans"));

You can also post arbitrary data with a content-type:

value *calljson (const value &args)
{
    returnclass (value) res retain;
    httpsocket hs;
    string in;
    string out = args.tojson ();
    
    in = hs.post ("http://db.local/call", "application/json", out);
    res.fromjson (in);
    return &res;
}

Sending SMTP Mail

Sending email is quite simple with the smtpsocket class:

#include <grace/smtp.h>

void sendAlert (const string &alert)
{
    smtpsocket smtp;
    
    // Defaults to localhost:25
    smtp.setsmtphost ("mx-internal.local", 25);
    smtp.setsender ("alerts@storpel.local", "Alert Service");
    
    // First argument is a value object that contains an
    // array of all recipients, in this case we will use
    // only one.
    smtp.sendmessage ($("alerts@example.net"), "Alert", alert);
}

 

Previous Chapter Table of Contents Next Chapter