【问题标题】:How to build TCP IP listner to read incoming messages using chilkat socket class c#如何使用 chilkat 套接字类 c# 构建 TCP IP 侦听器以读取传入消息
【发布时间】:2014-02-03 22:11:09
【问题描述】:

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

我们有几个客户端会在同一个端口和 ip 上连接并发送一些短信。

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

编辑 1:杰米,

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

【问题讨论】:

  • 使用Chilkat 代替System.NetSystem.Net.Sockets 中的类的任何特殊用途?
  • L.B,因为我们目前在我们应用程序的其他部分中使用 chilkat 来处理加密的东西,并且由于它的简单性,我们选择了 chilkat。但看起来 chilkat 插座在我们的场景中可能不是完美的选择,或者我可能无法正确使用它。
  • Restful 服务没有让你失望的肥皂头,所以我猜套接字是一个选项。检查我的编辑。
  • @Shax may be I am not able to utilize it properly 这是最可能的情况。但是您有更多机会获得有关 Windows 套接字的帮助。
  • @JeremyChild,你说检查我的编辑。您的编辑在哪里或您编辑的内容找不到您的编辑 Jeremy。

标签: c# .net sockets chilkat


【解决方案1】:

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

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

编辑:

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

编辑 2:

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

基本上,您只是向 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 代理的时间。

【讨论】:

  • Jermy,请检查原始帖子的编辑 1,我为您附加。
  • 不,我无法诚实地尝试,我不知道这一点。你能否指导我一个很好的教程,我可以使用 HTTP 上传并尽可能少地获得响应。
  • Jeremy 感谢您的回答,我明白了,现在我要修复它。
猜你喜欢
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 2020-11-22
  • 1970-01-01
  • 2012-04-09
相关资源
最近更新 更多