2011-11-24 138 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