简体   繁体   English

如何创建WCF来侦听Web上的TCP请求

[英]How to create a WCF that listens to TCP requests over web

I have a gps device that will send packets to TCP connection hosted over web that will have a static IP address & dedicated port for it. 我有一个gps设备,它将数据包发送到通过Web托管的TCP连接,该设备将具有静态IP地址和专用端口。

My question is how to achieve it using WCF ? 我的问题是如何使用WCF实现它? Or if WCF is needed or not for this problem. 或者是否需要WCF来解决此问题。

Please help me. 请帮我。 Thanx in advance. 提前感谢。

Did you mean TCP embedded transported/embedded using http? 您是说使用http传输/嵌入TCP嵌入式吗? or just TCP but using the internet? 还是只是TCP但使用互联网?

The last is supported for sure. 当然支持最后一个。


To set a TCP connectionover WCF is easy, just use NetTcpBinding, you can set this in C# code or better in .config file like this: 要通过WCF设置TCP连接很容易,只需使用NetTcpBinding,您可以在C#代码中设置,也可以在.config文件中更好地设置,如下所示:

<system.serviceModel>
  <bindings>
    <netTcpBinding name="portSharingBinding" 
                   portSharingEnabled="true" />
  </bindings>
  <services>
    <service name="MyService">
        <endpoint address="net.tcp://localhost/MyService"
                  binding="netTcpBinding"
                  contract="IMyService"
                  bindingConfiguration="portSharingBinding" />
    </service>
  </services>
</system.serviceModel>

There's no differece between setup a TCP channel in a private network or in a public one (internet) 在专用网络或公用网络(互联网)中设置TCP通道之间没有区别。

I strongly recommend You to read a A truely simple example to get started with TCP and WCF 我强烈建议您阅读一个真正简单的示例,以开始使用TCP和WCF

Depending the type of data being sent you could use (in ascending order of abstraction): 取决于要发送的数据类型,您可以使用(以升序排列):

Examples of use are given in the documentation. 文档中给出了使用示例。

TCP Listener : https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.110).aspx TCP侦听器https : //msdn.microsoft.com/zh-cn/library/system.net.sockets.tcplistener(v= vs.110) .aspx

HttpListner : https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx A nice example here: https://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx HttpListnerhttps : //msdn.microsoft.com/zh-cn/library/system.net.httplistener ( v= vs.110).aspx此处是一个很好的示例: https : //www.codehosting.net/blog/BlogEngine /post/Simple-C-Web-Server.aspx

Or use a full framework like WebApi or Nancy . 或使用WebApiNancy之类的完整框架。

http://www.asp.net/web-api http://www.asp.net/web-api

http://nancyfx.org/ http://nancyfx.org/

Without knowing how you are wanting to send the data it is hard to recommend but don't reinvent the wheel if you don't have to. 不知道您要如何发送数据,很难推荐,但是如果不需要的话,不要重新发明轮子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM