2011-11-24 125 views
1

我正在处理Web客户端。下面是代码:套接字异常

using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using Microsoft.SPOT; 
using System.Threading; 
using System; 

namespace EPI.Net 
{ 
    public class EfficientPutRequest 
    { 
     #region MysSettings   
     static string _server; 
     static int httpPort; 
     static string _name; 
     static string _namespace = "http://tempuri.org/"; 
     static string _event = "CreateEvent"; 
     #endregion 

     public static Socket connection; 


     public EfficientPutRequest(string uri) 
     { 
      Uri siteUri = new Uri(uri); 
      _server = siteUri.Host; 
      _name = siteUri.AbsolutePath; 
      httpPort = siteUri.Port; 
      connection = Connect(_server, 5000); 
     } 

     static Socket Connect(string host, int timeout) 
     { 
      IPHostEntry hostEntry = Dns.GetHostEntry(host); 

      IPAddress hostAddress = hostEntry.AddressList[0]; 
      IPEndPoint remoteEndPoint = new IPEndPoint(hostAddress, httpPort); 

      var connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      connection.Connect(remoteEndPoint); 
      connection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); 
      connection.SendTimeout = timeout; 
      return connection; 
     } 

     public void SendRequest(System.DateTime timestamp, string args) 
     { 
      Socket s = connection; 
      const string CRLF = "\r\n"; 

      string content = 
       "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
       "<s:Body>" + 
       "<" + _event + " xmlns=\"" + _namespace + "\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + 
       "<timestamp>" + timestamp.ToString("yyyy-MM-ddTHH:mm:ss") + "</timestamp>" + 
       "<Args>" + args + "</Args>" + 
       "</" + _event + ">" + 
       "</s:Body>" + 
       "</s:Envelope>"; 
      byte[] contentBuffer = Encoding.UTF8.GetBytes(content); 

      var requestLine = 
       "POST " + _name + " HTTP/1.1" + CRLF; 
      byte[] requestLineBuffer = Encoding.UTF8.GetBytes(requestLine); 

      var headers = 
       "Content-Type: text/xml; charset=utf-8" + CRLF + 
       "SOAPAction: \"" + _namespace + _event + "\"" + CRLF + 
       "Host: " + _server + CRLF + 
       "Content-Length: " + contentBuffer.Length + CRLF + 
       "Expect: 100-continue" + CRLF + 
       "Accept-Encoding: gzip, deflate" + CRLF + CRLF; 
      byte[] headersBuffer = Encoding.UTF8.GetBytes(headers); 
      try 
      { 
       s.Send(requestLineBuffer); 
       Thread.Sleep(100); 
       s.Send(headersBuffer); 
       Thread.Sleep(100); 
       s.Send(contentBuffer); 
       Thread.Sleep(100); 
       s.Close(); 
       connection.Close(); 
      } 
     } 
    } 
} 

我使用这个代码,以测试客户端:

using System; 
using System.Threading; 
using Microsoft.SPOT; 
using Microsoft.SPOT.Hardware; 
using GHIElectronics.NETMF.FEZ; 
using EPI.Net; 

namespace FEZ_Cobra_Console_Application1 
{ 
    public class Program 
    { 
     public static string endPoint = "http://192.*********/webservice.asmx"; 
     public static int i = 1; 
     public static void Main() 
     { 
      while (true) 
      { 
       EfficientPutRequest Servicio = new EfficientPutRequest(endPoint); 
       Servicio.SendRequest(System.DateTime.Now, 17, 17, 0, ""); 
       Debug.Print(i.ToString() + " sent"); 
       i++; 
       Thread.Sleep(1000); 
      } 
     } 

    } 
} 

它发送15条消息后,给出了套接字错误。这里的错误: enter image description here

我也试过在发送后关闭套接字,既不工作。任何想法?

编辑:我更新了代码,发送消息后关闭套接字。在模拟器中正常工作,但不在我的设备中。

回答

0

解决这样:

connection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 }); 

并将其加入发送消息后:

connection.Close(); 

在这里你可以看到固定码:

using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using Microsoft.SPOT; 
using System.Threading; 
using System; 

namespace EPI.Net 
{ 
    public class EfficientPutRequest 
    { 
     #region MysSettings   
     static string _server; 
     static int httpPort; 
     static string _name; 
     static string _namespace = "http://tempuri.org/"; 
     static string _event = "CreateEvent"; 
     #endregion 

     public static Socket connection; 


     public EfficientPutRequest(string uri) 
     { 
      Uri siteUri = new Uri(uri); 
      _server = siteUri.Host; 
      _name = siteUri.AbsolutePath; 
      httpPort = siteUri.Port; 
      connection = Connect(_server, 5000); 
     } 

     static Socket Connect(string host, int timeout) 
     { 
      IPHostEntry hostEntry = Dns.GetHostEntry(host); 

      IPAddress hostAddress = hostEntry.AddressList[0]; 
      IPEndPoint remoteEndPoint = new IPEndPoint(hostAddress, httpPort); 

      var connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      connection.Connect(remoteEndPoint); 
      connection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 }); 
      connection.SendTimeout = timeout; 
      return connection; 
     } 

     public void SendRequest(System.DateTime timestamp, string args) 
     { 
      const string CRLF = "\r\n"; 

      string content = 
       "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
       "<s:Body>" + 
       "<" + _event + " xmlns=\"" + _namespace + "\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + 
       "<timestamp>" + timestamp.ToString("yyyy-MM-ddTHH:mm:ss") + "</timestamp>" + 
       "<Args>" + args + "</Args>" + 
       "</" + _event + ">" + 
       "</s:Body>" + 
       "</s:Envelope>"; 
      byte[] contentBuffer = Encoding.UTF8.GetBytes(content); 

      var requestLine = 
       "POST " + _name + " HTTP/1.1" + CRLF; 
      byte[] requestLineBuffer = Encoding.UTF8.GetBytes(requestLine); 

      var headers = 
       "Content-Type: text/xml; charset=utf-8" + CRLF + 
       "SOAPAction: \"" + _namespace + _event + "\"" + CRLF + 
       "Host: " + _server + CRLF + 
       "Content-Length: " + contentBuffer.Length + CRLF + 
       "Expect: 100-continue" + CRLF + 
       "Accept-Encoding: gzip, deflate" + CRLF + CRLF; 
      byte[] headersBuffer = Encoding.UTF8.GetBytes(headers); 
      try 
      { 
       connection.Send(requestLineBuffer); 
       Thread.Sleep(100); 
       connection.Send(headersBuffer); 
       Thread.Sleep(100); 
       connection.Send(contentBuffer); 
       Thread.Sleep(100); 
       connection.Close(); 
      } 
     } 
    } 
} 
-1

你拼错你的eventname:static string _event =“CrearteEvent”; 这会导致消息无法正常工作,在打开构造函数中的另一个连接之前,还应该添加一个关闭连接的close()。

+0

问题更新。我已经说过,它第一次运行15次,而且我也尝试过关闭套接字。 – Manu

+0

我从你的帖子中得知,前15次没有给出有效的回复。 10035表示资源不可用,因此请确保资源通过Close()释放,也可能会增加超时。如果套接字是非阻塞的,那么当套接字操作仍在执行时,其余代码仍在继续。这可能会导致此错误。 – loconero

+0

Aslo尝试引入构造函数EfficientPutRequest Servicio = new EfficientPutRequest(endPoint);在你的while循环中出现 – loconero