套接字异常

时间:2011-11-24 12:03:06

标签: c# sockets .net-micro-framework

我正在开发一个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

我也尝试在发送后关闭套接字,但都不起作用。有什么想法吗?

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

2 个答案:

答案 0 :(得分: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 :(得分:-1)

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