How to create a WCF that listens to TCP requests over web

夙愿已清 提交于 2020-01-06 18:31:12

问题


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.

My question is how to achieve it using WCF ? Or if WCF is needed or not for this problem.

Please help me. Thanx in advance.


回答1:


Did you mean TCP embedded transported/embedded using http? or just TCP but using the internet?

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:

<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)

I strongly recommend You to read a A truely simple example to get started with TCP and WCF




回答2:


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

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

Or use a full framework like WebApi or Nancy.

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

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.



来源:https://stackoverflow.com/questions/32181993/how-to-create-a-wcf-that-listens-to-tcp-requests-over-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!