How to create a WCF that listens to TCP requests o

2019-08-30 02:32发布

问题:

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.