1

我正在使用 chilkat 套接字类。问题是我想保持我的套接字打开,假设我执行了我的表单并且第一次打开了特定 IP 上的端口来监听消息。我只能成功地第一次接收消息,现在之后消息 我想让我的应用程序在有新消息出现时继续收听和接收。

我们有几个客户端将在同一端口和 IP 上连接并发送一些文本消息。

但我无法做到这一点。我需要建立一个监听器,它会继续监听,一旦我收到任何需要处理的消息。任何使用过 chilkat 课程或有此类应用程序经验的人请建议我如何实现此功能,因为我在 CHILKAT 网站上找不到此类应用程序的好例子,或者我可能没有经验不知道如何准确编码这种类型的功能。

编辑1:杰米,

是的,我们已经开发了 REST WCF 服务并且它们运行良好,但问题在于 REST WCF 服务的响应中出现了大响应标头,这是我们不希望的,因为在我们的企业应用程序中,Windows Phone 7 手机也将进行通信和发送文本消息,仅出于手机的考虑,我们试图减少需要传回的数据,通过使用套接字,我们可以避免额外的响应标头,并且由于成本原因,SMS 不是我们的选择。如果您对 Web 服务有任何建议以尽量减少数据,请分享。

4

1 回答 1

0

您是否考虑过 Web 服务?几乎任何可以发送 Http 请求的语言都可以使用它们。如果您可以控制客户端应用程序,那么 Web 服务绝对是正确的途径。

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

编辑:

您是否考虑过使用 http 响应代码进行简单的 http 字节上传。即 Http Ok,Http 失败。您可以将状态代码自定义为适合您项目的任何内容。

编辑2:

也许只有 http 状态代码作为响应的 RPC 风格的方法可能是合适的。检查此问题以获取提示。使用 C# 调用 json

基本上,您只是将一些字符串发送到 url,然后接收状态码。那是很小的。

编辑3:

这是我使用 Reflector 从一些旧代码中提取的内容。这只是该程序的一般要点。显然,第一个请求应该有一个 using 语句。

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?
        }
    }
}

请记住仅使用 Http 状态代码和描述进行响应。并确保请求的代理设置为绕过请求 URL,以节省解析 IE 代理的时间。

于 2011-11-07T20:37:37.790 回答