我需要知道是否可以使用TFTP创建WCF服务以从设备获取数据。我知道我可以使用C#创建一个应用程序,但我正在努力使它成为一个基于Web的应用程序。此外,WCF需要托管在IIS上。我想使用WCF服务启动连接,然后从我的设备中提取图像。当我运行我的代码时,它似乎没有SendTo命令的问题,但它总是给我一个" System.Net.Sockets.SocketException(0x80004005):连接尝试失败,因为连接方没有正确响应一段时间后,或建立的连接失败,因为连接的主机无法在System.Net.Sockets.Socket响应。 ReceiveFrom (...)。这是因为我使用的是netTcpBinding吗?我可以使用基本的HTTP绑定或其他方式执行此操作吗?或者也许它不可能使用WCF创建这项服务,思想??
服务代码段:
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>
答案 0 :(得分:0)
我能够确定问题所在。 TFTP使用UDP,显然您不能使用IIS来托管服务。您可以使用Windows控制台或Windows窗体应用程序托管服务。我创建了一个简单的控制台应用程序,然后更新了我的web.config(现在是一个App.config),如下所示:
的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>