Advanced Services - SMS Connectivity

CPP on Linux Sample application using HTTP to send an EMS message

HSL Mobile Messaging

The following sample application written in CPP on Linux sends an EMS message to a single mobile telephone. The application uses HTTP to communicate with HSL's systems. To send to more than one mobile telephone at a time, separate each mobile number in the destaddr field by a comma (e.g. "447968000111,447720000111,3378100100").

#include <cstring>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

#include <openssl/evp.h>

int main(int argc, char* argv[]) {
    const char* host = "194.247.82.149";
    const int port = 80;
    const char* action = "submitsm";
    const char* client_id = "<your client_id>";
    const char* destaddr = "<some fully-qualified MSISDN>";
    const char* shortmessage = "7C0C7A00424547494E3A494D454C4F44590D0A564552" \
        "53494F4E3A312E320D0A464F524D41543A434C415353312E300D0A4D454C4F44593A" \
        "64332363336433236333643372326433236333643365332366336533236633673361" \
        "33236733613323673361337232613323673361330D0A454E443A494D454C4F44590D0A";
    const char* secret = "<your secret>";

    // Digest variables
    EVP_MD_CTX md_context;
    const EVP_MD* md = EVP_md5();
    unsigned int md_length;
    unsigned char md_value[EVP_MAX_MD_SIZE];

    // Initialise the digest
    EVP_DigestInit(&md_context, md);

    // Feed data into the digest
    EVP_DigestUpdate(&md_context, secret, strlen(secret));
    EVP_DigestUpdate(&md_context, shortmessage, strlen(shortmessage));
    EVP_DigestUpdate(&md_context, destaddr, strlen(destaddr));

    // Get the digest and it's length
    EVP_DigestFinal(&md_context, md_value, &md_length);

    // Convert the digest's raw bytes into a string
    char lut[] = { '0', '1', '2', '3', '4', '5', '6', '7',
                   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    char* hash = new char[md_length * 2 + 1];
    for(int i = 0; i < md_length; i++) {
        hash[2 * i] = lut[(md_value[i] & 0xF0) >> 4];
        hash[2 * i + 1] = lut[md_value[i] & 0x0F];
    }
    hash[md_length * 2] = '\0';

    // Create the URL
    const char* pattern = "GET /%s/?client_id=%s&destaddr=%s&esm=64" \
        "&shortmessage=%s&key=%s HTTP/1.0\r\n\r\n";
    int buffer_size = strlen(action) + strlen(client_id) + strlen(destaddr)
                      + strlen(shortmessage) + strlen(hash) + 64;
    char* buffer = new char[buffer_size];
    sprintf(buffer,
            pattern,
            action,
            client_id,
            destaddr,
            shortmessage,
            hash);

    // Clean up
    delete[] hash;

    // Create the socket
    int the_socket = socket(AF_INET, SOCK_STREAM, 0);

    // Set up the address
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = htons(port);
    inet_pton(AF_INET, host, &address.sin_addr);

     // Connect
    connect(the_socket, (struct sockaddr *)&address, sizeof(address));

    // Variables used for IO
    int bytes_to_io = strlen(buffer);
    int total_bytes_iod = 0;
    int bytes_iod = 0;

    // Send the request
    while(total_bytes_iod < bytes_to_io
          && (bytes_iod = send(the_socket,
                               buffer + total_bytes_iod,
                               bytes_to_io - total_bytes_iod,
                               0)) != -1)
    {
        total_bytes_iod += bytes_iod;
    }

    // Read the response
    buffer_size = 4096;
    buffer = new char[buffer_size];
    bytes_to_io = buffer_size;
    total_bytes_iod = 0;
    while(total_bytes_iod < buffer_size
          && (bytes_iod = recv(the_socket,
                               buffer + total_bytes_iod,
                               buffer_size - total_bytes_iod,
                               0)))
    {
        total_bytes_iod += bytes_iod;
    }

    buffer[total_bytes_iod] = '\0';

    // Inform the user of the outcome
    fprintf(stdout, "HTTP response:\n%s\n", buffer);

    delete[] buffer;

    return EXIT_SUCCESS;
}

ems_http.cpp


Copyright © Hay Systems Ltd 2004

Owner: support@haysystems.com 20 September 2004

Sample code link | Developers section link