How to Receive an SMS in C/C++

The simplest way to send SMS from C/C++ is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

how to receive an sms in ccpp
Figure 1 - How to Receive an SMS in C/C++

C/C++ code to receive sms from mobile

The C/C++ sms code sample below demonstrates how you can receive SMS using the http rest sms api of Ozeki SMS Gateway using the C++ Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

ReceiveSms.cpp
#include <iostream>
#include <string>
#include "Ozeki.Libs.Rest.h"

using namespace std;

int main()
{
    //Function to create unique identifier for each messages
    srand((unsigned)time(0));

    Configuration configuration;
    configuration.Username = "http_user";
    configuration.Password = "qwe123";
    configuration.ApiUrl = "http://127.0.0.1:9509/api";

    MessageApi api(configuration);

    auto result = api.DownloadIncoming();

    cout << result << endl;
    for (Message message : result.Messages) {
        cout << message << endl;
    }

    return 0;
}
	

How to use the C++ sms example:

This C++ sms example can be used in any C or C++ application. To use it, you must add the Ozeki.Libs.Rest dll as a reference to your project. After the project reference is added, you must put the using Ozeki.Libs.Rest; directive into the header section of your C/C++ source code. This will allow you to use the classes provided by the Ozeki.Libs.Rest library. You can use the MessageApi class to receive the SMS from the SMS gateway. The SMS gateway will forward the message to you either through a wireless connection or through the Internet.

Download Receive-SMS.cs

The source code explained in this article can be downloaded and used and modified free of charge.
Download: ReceiveSms.cpp.zip (2.97Mb)

What is in the ReceiveSms.cpp.zip file?

The ReceiveSms.cpp.zip file contains the Ozeki.Libs.Rest library, which gives you all the tools necessary to send and receive SMS messages. You will also find the ReceiveSms project in the zip, which contains the example code to show you how to receive an SMS. This example code is listed below.

Figure 2 - What is inside ReceiveSms.cpp.zip

How to receive SMS in C/C++ (Easy guidelines)

To receive SMS in C/C++:

  1. Setup Visual Studio Community
  2. Download the ReceiveSms.cpp.zip file
  3. Extract the .zip file from Downloads
  4. Open the ReceiveSms.sln file in Visual Studio
  5. Launch Ozeki SMS Gateway
  6. Click on HTTP user in the Users and applications tab
  7. Run ReceiveSms.cpp to receive SMS in C/C++
  8. Check the Inbox in Ozeki SMS Gateway

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send, and receive SMS from C/C++, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your C/C++ code in Visual studio. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your C/C++ code.

HTTP API url to use Receive sms from C/C++

To receive SMS using C/C++, your C/C++ will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the C/C++ sms application is running, this can be 127.0.0.1. If it is installed on a different computer, it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest
	

HTTP authentication to use receive sms from C/C++

To authenticate the C/C++ sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In C++ you can use the following code to do this encoding:

string usernamePassword = username + ":" + password;
string usernamePasswordEncoded = base64::encode(usernamePassword);
return "Basic " + usernamePasswordEncoded;
	

For example if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send

HTTP request to receive SMS with C/C++

To receive SMS, your C/C++ application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.

GET /api?action=receivemsg&folder=inbox HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 127.0.0.1:9509
	

HTTP request header to receive SMS from C/C++

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
	

HTTP response received by the C/C++ sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the message's submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.123 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Tue, 29 Jun 2021 10:59:51 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked

{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "",
	"data": {
	  "folder": "inbox",
	  "limit": "1000",
	  "data": [
	    {
	      "message_id": "5d78e707-13d3-49d3-9b7d-66ba0c24b21e",
	      "from_connection": "admin@localhost",
	      "from_address": "",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 3",
	      "create_date": "2021-06-29 11:07:33",
	      "valid_until": "2021-07-06 11:07:33",
	      "time_to_send": "2021-06-29 11:07:33",
	      "submit_report_requested": true,
	      "delivery_report_requested": false,
	      "view_report_requested": false,
	      "tags": []
	    },
	    {
	      "message_id": "695acc97-43b6-4cf1-a6ae-c19e3f908c8f",
	      "from_connection": "admin@localhost",
	      "from_address": "",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 2",
	      "create_date": "2021-06-29 11:07:23",
	      "valid_until": "2021-07-06 11:07:23",
	      "time_to_send": "2021-06-29 11:07:23",
	      "submit_report_requested": true,
	      "delivery_report_requested": false,
	      "view_report_requested": false,
	      "tags": []
	    },
	    {
	      "message_id": "333f852e-2f8f-464d-9663-831e857819ca",
	      "from_connection": "admin@localhost",
	      "from_address": "",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 1",
	      "create_date": "2021-06-29 11:07:10",
	      "valid_until": "2021-07-06 11:07:10",
	      "time_to_send": "2021-06-29 11:07:10",
	      "submit_report_requested": true,
	      "delivery_report_requested": false,
	      "view_report_requested": false,
	      "tags": []
	    }
	  ]
	}
}
	

Downloading and opening the C/C++ project (Video tutorial)

In the following video, we are going to show you how to download and open the project file. The video starts with finding the download link and will end with the project ready to use. This 1:32 long video is easy to understand and very detailed. You will have no problem understanding this tutorial. We are using the Visual Studio programming environment to open the project.

Video 1 - How to download and run the example solution (Video tutorial)

The example code below is part of the Receive-SMS.sln Visual Studio Solution. A visual studio solution can contain multiple projects and multiple files. In this solution there is only one project: Receive-SMS.csproj, and one file: Program.cs.

Figure 3 - ReceiveSms.sln

Using the project (video tutorial)

The following video explains how to use the project. It will start with opening the Ozeki SMS Gateway and will take you all the way to the list of messages in the command prompt. In the process, you will see how the code moves the messages from the inbox folder to the C/C++ project. The video is great because it features all the steps in a detailed way and it is easy to understand. You will need only 34 seconds to watch the video and you will use the dashboard of the Ozeki SMS Gateway which is very intuitive. You will have no problem following the tutorial.

Video 2 - Receiving SMS with the C++ code above (Video tutorial)

To sum it up

This guide showed you how to receive SMS in C / C++ with the HTTP SMS API user of the Ozeki SMS Gateway. With this solution, you can assure that the incoming messages will be redirected by the SMS API and received by your Scala program. Your contact list and data are in safe hands because the Ozeki SMS Gateway runs in a closed environment that you control.

Continue reading about topics like this on the Ozeki webpage. Check out How to Delete an SMS in C/C++ and use the given codes without limitations.

Start working by downloading the Ozeki SMS Gateway now!

More information