[英]How to read whenever there is an incoming message in Tcp socket programming in C#
[英]How to build TCP IP listner to read incoming messages using chilkat socket class c#
我正在使用chilkat套接字类。 问题是我想保持套接字打开状态,可以说我执行了表单并第一次打开了特定IP上的端口以侦听消息。现在我只能第一次成功接收消息,此后消息我想让我的应用程序继续监听并在收到新消息时接收消息。
我们有几个客户端,它们将在同一端口和ip上连接并发送一些文本消息。
但是我无法做到这一点。 我需要构建一个侦听器,该侦听器将继续侦听,并且一旦收到需要处理的任何消息。 任何使用chilkat类或具有此类应用程序经验的人都请向我建议如何实现此功能,因为我无法在CHILKAT网站上找到此类应用程序的好例子,或者可能是我没有经验如何精确编码这种功能。
编辑1:杰米
是的,我们已经开发了REST WCF服务,并且它们工作正常,但是问题在于REST WCF服务的响应出现了很大的响应标头,我们不希望这样做,因为在我们的企业应用程序中,Windows Phone 7手机还将进行通信和发送短信,并且仅出于移动设备的原因,我们试图减少需要传递回的数据,并且通过使用套接字,我们可以避免额外的响应头,并且由于成本原因,SMS不是我们的选择。 如果您对Web服务有任何建议以尽量减少数据,请共享它。
您考虑过Web服务吗? 几乎所有可以发送Http请求的语言都可以使用它们。 如果您可以控制客户端应用程序,那么Web Service绝对是正确的途径。
http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html
编辑:
您是否考虑过简单的http上传字节,并带有http响应代码。 即Http正常,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代理的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.