Question

I am using chilkat socket class. The problem is I want to keep my socket open, lets say I executed my form and the very first time which opened the port on a specific IP to listen the messages.I am able to receive the messages first time only successfully, now after this message I want to keep my application to keep listening and receive when ever a new message comes.

We have several clients who will connect and send some text messages on the same port and ip.

But I am unable to achieve this. I need to build a Listener, which will keep on listening and as soon as I will get any message I need to process it. Any body who has used chilkat class or having experience in this kind of application kindly suggest me how can I achieve this functionality as I could't find good example for this kind of application on CHILKAT website or may be I am inexperienced don't know how to exactly code this type of functionality.

Edit 1: Jermy,

yes we have developed REST WCF services and they are working perfect, but the problem is in the response of REST WCF Service big response headers are appearing, which we don't want because in our enterprise application Windows Phone 7 mobiles will also communicate and send text messages and only for the sake of mobiles we are trying to reduce the data we need to pass back and by using sockets we can avoid extra response headers and SMS is not an option for us because of cost. If you have any suggestions towards Webservices to minimize the data kindly share it.

Was it helpful?

Solution

Have you considered a Web Service? They can be consumed by pretty much any language that can send Http requests. If you have control of the client applications then a Web Service is definitely the correct route.

http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html

Edit:

Have you considered simple http upload of bytes, with a http response code. Ie Http Ok, Http Failure. You can customize the status codes to anything that suits your project.

Edit 2:

Perhaps an RPC styled method with ONLY http status codes as the response could be suitable. Checks this question for hints. json call with C#

Basically you are just sending some string to a url, then receiving the status code back. That is quite minimal.

Edit 3:

Here is something I pulled out of some old code with Reflector. This is just for the general gist of the procedure. Obviously there should be a using statement on the first request.

public void SMS(Uri address, string data)
{

   // Perhaps string data is JSON, or perhaps its something delimited who knows.
   // Json seems to be the pretty lean.
    try
    {
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
        request.Method = "POST";
        // If we don't setup proxy information then IE has to resolve its current settings
        // and adds 500+ms to the request time.
        request.Proxy = new WebProxy();
        request.Proxy.IsBypassed(address);
        request.ContentType = "application/json;charset=utf-8";
        // If your only sending two bits of data why not add custom headers?
        // If you only send headers, no need for the StreamWriter.
        // request.Headers.Add("SMS-Sender","234234223");
        // request.Headers.Add("SMS-Body","Hey mom I'm keen for dinner tonight :D");
        request.Headers.Add("X-Requested-With", "XMLHttpRequest");
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.WriteLine(data);
        writer.Close();
        using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                // Either read the stream or get the status code and description.
                // Perhaps you won't even bother reading the response stream or the code 
                // and assume success if no HTTP error status causes an exception.
            }
        }
    }
    catch (WebException exception)
    {
        if (exception.Status == WebExceptionStatus.ProtocolError)
        {
            // Something,perhaps a HTTP error is used for a failed SMS?
        }
    }
}

Remember to respond only with Http status codes and descriptions. And ensure that the request's proxy is setup to bypass the requesting Url to save time resolving the IE proxy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top