2011-11-24 2 views
1

나는 웹 클라이언트에서 일하고있다.소켓 예외

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 메시지를 보낸 후 소켓 오류가 있습니다 : 나는 클라이언트를 테스트하기 위해이 코드를 사용하고

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(); 
      } 
     } 
    } 
} 

: 여기에 코드입니다. 여기에 오류가 있습니다 : enter image description here

나는 송신 후에도 소켓을 닫으려고했지만 어느 것도 작동하지 않습니다. 어떤 생각?

편집 : 메시지를 보낸 후 코드를 업데이트하고 소켓을 닫았습니다. 에뮬레이터에서는 정상적으로 작동하지만 내 장치에서는 작동하지 않습니다.

답변

0

그렇게 해결 :

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

그리고이 추가 메시지를 보낸 후 : 업데이트

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

사용자의 맞춤법이 잘못되었습니다. static string _event = "CrearteEvent"; 그러면 메시지가 작동하지 않게되고 생성자에서 다른 연결을 열기 전에 연결을 닫는 close()를 추가해야합니다.

+0

질문 : 고정 코드를 볼 수 있습니다 여기에

connection.Close(); 

. 나는 그것이 처음 15 번 잘 작동한다고 말했고 소켓을 닫으려고 시도했다. – Manu

+0

귀하의 게시물에서 처음 15 번은 유효한 응답이 없었습니다. 10035는 리소스를 사용할 수 없음을 의미하므로 리소스가 Close()를 통해 해제되고 시간 제한을 늘릴 수도 있음을 의미합니다. 소켓이 비 블로킹 (non-blocking)이면 소켓 작업이 여전히 수행되는 동안 나머지 코드가 계속됩니다. 이로 인해이 오류가 발생할 수 있습니다. – loconero

+0

마찬가지로 생성자를 가져 오십시오 EfficientPutRequest Servicio = new EfficientPutRequest (endPoint); out while while loop – loconero

관련 문제