0

I need to know if I can create a WCF service using TFTP to get data from a device. I know I can create an application using C# to do this but I am trying to make it a web based application. Also the WCF needs to be hosted on IIS. I want to use a WCF service to start a connection and then pull an image from my device. When I run my code it does not seem to have a problem with the SendTo command but it always gives me a "System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.ReceiveFrom(...). Is this because I am using a netTcpBinding? Can I do this using a basic HTTP binding or something else? Or maybe it just isn't possible to create this service using WCF, thoughts??

Service Code Snippet:

public byte[] ipTftpGet(String xferFileName)
{...
   byte[] rcvBuffer = new byte[5200];
   ....

try
{    
    ipEndPointFtp = new IPEndPoint(ipAddress, 69);
    tftpS = new Socket(ipEndPointFtp.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
    remoteEP = ipEndPointFtp;
    // Request and Receive first Data Packet From TFTP Server
    tftpS.SendTo(sndBuffer, nameLen, System.Net.Sockets.SocketFlags.None, remoteEP);
    tftpS.ReceiveTimeout = 1000;

    try
    {                
        len = tftpS.ReceiveFrom(rcvBuffer, ref remoteEP);//tftpS.ReceiveFrom(rcvBuffer, ref remoteEP);
        rcvBuffer[len] = 0x00;
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        xferValid = false;
        errMsgStr = ex.ToString();
    }
}

Web.config:

<services>
  <service behaviorConfiguration="mexBehavior"
           name="ComService.ComService">
    <endpoint address="ComService" binding="netTcpBinding"
        contract="ComService.IComService" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8090" />
        <add baseAddress="http://localhost:8080" />
      </baseAddresses>
    </host>
  </service>
</services>
Viper
  • 3
  • 3

1 Answers1

0

I was able to determine the problem. TFTP uses UDP and apparently you cannot use IIS to host the service. You can host the service using a Windows Console or Windows Forms application. I created a simple console application and then updated my web.config (now an App.config) as follows:

Program.cs:

namespace TFTPConsoleHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(TFTPService.TFTPService)))
            {
                host.Open();
                Console.WriteLine("Host started @ " + DateTime.Now.ToString());
                Console.ReadLine();
            }

        }
    }
}

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="unsecured">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="TFTPService.TFTPService">
        <endpoint address="soap.udp://localhost:8060/" binding="udpBinding" contract="TFTPService.ITFTPService"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8060/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>
Viper
  • 3
  • 3