错误信息(这个是从网上摘过来的):
套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的。本地套接字超时是“00:01:00”
原来是windows安全问题导致的,如果服务器端和客户端在同一个域环境里,没有这个问题 。我先是在局域网的两台机器测试的,服务器端是有加入域的,客户端是没有加入域的,
开始客户端连都连不上通也不通,ping了一下,原来是杀毒软件的防火墙开着,后来关掉就OK了。公网服务器再试了一下,还是不行,原来是我没有设置好,把服务端的安全配置
代码注释掉了。不多说了,看下面的代码设置
<!--服务器端代码设置--> <bindings> <netTcpBinding> <binding name="BindingBehaviorConfiguration"> <security mode="None" > <transport clientCredentialType="None"></transport> </security> </binding> </netTcpBinding> </bindings> <!--bindingConfiguration="BindingBehaviorConfiguration"--> <services> <service name="WCFColdChainService.UserService"> <endpoint address="" binding="netTcpBinding" contract="IContracts.IUser" bindingConfiguration="BindingBehaviorConfiguration"> <identity> <dns value="172.17.103.44" /> </identity> </endpoint> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> <host> <baseAddresses> <add baseAddress="net.tcp://172.17.103.44:5600/WCFColdChainService/UserService/" /> </baseAddresses> </host> </service> </services>
//客户端我是用代码写的 private static string strServer = ApplicationConfiguration.GetUrl("tcp"); private static string strService = "/WCFColdChainService/UserService/"; public static IUser GetUser() { //主要是这段的设置 NetTcpBinding binding = new NetTcpBinding(); binding.ReceiveTimeout = new TimeSpan(0, 10, 0); //安全类型,客户端凭证设置成关闭 binding.Security.Mode = SecurityMode.None; binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None; //此处添加地址 EndpointAddress address = new EndpointAddress(strServer + strService); ChannelFactory<IUser> factory = new ChannelFactory<IUser>(binding , address); IUser channel = factory.CreateChannel(); return channel; } public static void CloseChannel(IUser icc) { ICommunicationObject channel = (ICommunicationObject)icc; channel.Close(); }