Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2012, 04:41 AM
Registered User
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default How to build a C sharp application with HttpListener to receive SMS messages

Hi all,
I need a C# application to receive SMS messages, and I found a source code at the official site of Ozeki NG SMS Gateway with some information on HTTPListener, as well. Could you give me your opinion on:
1. The source code itself: whether it would work.
2. Ozeki NG SMS Gateway: whether it really is so reliable and robust as I keep reading it everywhere.
3. HTTPListener: if it would be a good choice as a webserver.
Thank you for your answers, and here’s what I found at http://www.ozekisms.com/sql-sms-gate...x.php?owpn=696
Once you have an SMS Gateway configured, you can build your C# application. This application's built in webserver is HttpListener. HttpListener is a very efficient webserver available on Windows XP SP2 and Windows 2003 or later windows version such as Windows Vista. The HttpListener class is part of the System.Net package and it comes with .NET Framework 2.0.

If you take a look at the source code you will see that as a first step of getting this solution running, you need to register the "http://127.0.0.1:8080/" address as your URL prefix. Please note, that this URL is configured into the SMS Gateway HTTP Client user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;

namespace httplistener_console
{
class Program
{
static void Main(string[] args)
{
try
{
HttpListener listener = new HttpListener();

//The listeningURL will hold the listening URL
string listeningURL = "http://+:8080/";
listener.Prefixes.Add(listeningURL);
listener.Start();
Console.WriteLine("Ozeki HTTP server - Listening started at {0}", listeningURL);

while (listener.IsListening)
{
HttpListenerContext context = listener.GetContext();

try
{
Thread t = new Thread(() =>
{
//Querying the sender address
string sender = context.Request.QueryString["sender"];
//"message" variable holds the response message
string message = "Thank you for your message";

string response = "{SMS:TEXT}{}{+1234657}{" + sender + "}{" + message + "}";
byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(response);
context.Response.ContentType = "text/html; charset=utf-8";
context.Response.OutputStream.Write(Buffer, 0, Buffer.Length);
context.Response.Close();

//writing the sent message into the console
Console.WriteLine("Message sent to {0}. Data: {1}", sender, message);
Console.ReadLine();

});
t.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}

The request subrouting is called asynchronously when an incoming SMS arrives. This sub is very simple, it calls the process request function to serve the requests.

The process request subroutine has two main parts: the first part reads the message from the HTTP requests and displays a MessageBox. The second part generates a response by writing to the Outputstream. The output will be encoded in UTF8 to support international characters. This is achieved by using UTF 8 encoding in the System.Text.Encoding.UTF8.GetBytes statement.

Any answers would be appreciated.

J.





Similar Threads
Thread Thread Starter Forum Replies Last Post
SOLUTION: HowTo Send SMS Messages dparsons ASP.NET 1.0 and 1.1 Basics 9 August 29th, 2008 12:16 PM
Only Receive sms anyJulio Visual Studio 2005 0 June 18th, 2007 10:05 PM
SMS APPLICATION ruddarpartap ASP.NET 1.x and 2.0 Application Design 7 February 8th, 2007 08:13 AM
using a sql db to send sms messages/emails Adam H-W SQL Server 2000 0 April 13th, 2006 05:46 AM
sms application???? need help dobmak VB How-To 0 December 28th, 2004 10:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.