有个控制硬件的类如下所示:
    public class RelayMgr
    {
        public static uint ProtocolHead = 0x55; //协议头
        public static Socket socketClient = null;
        Thread threadClient = null;
        /// <summary>
        /// 更新事件
        /// </summary>
        /// <param name="msg"></param>
        public delegate void HandleInterfaceUpdataDelegate(byte[] msg);
        private HandleInterfaceUpdataDelegate interfaceUpdataHandle;
        public enum RelayType : uint
        {
            /// <summary>
            /// 开
            /// </summary>
            ON = 0x12,
            /// <summary>
            /// 关
            /// </summary>
            OFF = 0x11,
            /// <summary>
            /// 位控制
            /// </summary>
            BYTECTRL = 0x13,
            /// <summary>
            /// 查询
            /// </summary>
            INQUIRE = 0x10
        }
        //命令列表
        private static List<byte[]> CMDLIST = new List<byte[]>();
        //
        byte[] tempCMD = new byte[8];        //写一个发送信息到服务端的方法
        public static void clientSendMsg(byte[] txtCMsg)
        {
            //获取文本框txtCMsg输入的内容
            //string strClientSendMsg = txtCMsg;
            //将输入的内容字符串转换为机器可以识别的byte数组
            //byte[] arrClientSendMsg = System.Text.Encoding.UTF8.GetBytes(strClientSendMsg);
            //调用客户端套接字发送byte数组
            try
            {
                socketClient.Send(txtCMsg);
            }
            catch (Exception err)
            {
                throw;
                // Response.Write(err.Message);
                // MessageBox.Show(err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void RelayMrg()
        {
           // Connect("192.168.1.110", "6000");
        }
        ~RelayMgr()
        {
            //终止线程
          //  threadClient.Abort();
            //关闭socket
           socketClient.Close();
           // Dispose();
        }         public static void RelaySwitch(ushort Address,ushort Line,ushort Option)
        {
                              }
        public void OpenAll()
        {            
             SendCMD(Convert.ToInt16("1"), 0xFFFF, RelayType.BYTECTRL);
            //SendToBus(Convert.ToInt16("1"), 0xFFFF, RelayType.INQUIRE);
            //SendToBus();
        }
        public void CloseAll()
        {
            SendCMD(Convert.ToInt16("1"), 0x0000, RelayType.BYTECTRL);       
           // SendToBus(Convert.ToInt16("1"), 0x0000, RelayType.INQUIRE);
        }
        ////网络连接 消息接收
        //定义一个接受服务端发来信息的方法
        public void RecMsg()
        {
            while (true) //持续监听服务端发来的消息
            {
                //客户端 定义一个1M的byte数组空间
                byte[] arrRecMsg = new byte[8];
                //定义byte数组的长度
                int length = socketClient.Receive(arrRecMsg);
                if (length == 8)
                {
                    //SetDevState(arrRecMsg);
                    // this.Invoke(interfaceUpdataHandle, arrRecMsg);
                }
                //arrRecMsg 接收到的物理数据                //将byte数组转换为人可以看懂的字符串
                //string strRecMsg = System.Text.Encoding.UTF8.GetString(arrRecMsg, 0, length);            }
        }
        /// <summary>
        /// 变更设备状态
        /// </summary>
        /// <param name="statemsg"></param>
        private void SetDevState(byte[] statemsg)
        {
            //int i = 1;
            //for (i = 1; i <= 16; i++)
            //{
            //    if (statemsg[1] == Convert.ToInt16(txtAddress))
            //    {
            //        int devstate = Relay.GetState(statemsg, i);
            //        if (devstate == 0)
            //        {
            //            Controls["btn_Item" + i].BackColor = Color.Red;
            //        }
            //        else
            //        {
            //            Controls["btn_Item" + i].BackColor = Color.Green;
            //        }
            //    }
            //}
        }
        public bool Connect(string IP, string Port)
        {
            try
            {
                //定义一个套字节监听  包含3个参数(IP4寻址协议,流式连接,TCP协议)
                socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipaddress = IPAddress.Parse(IP);
                //将获取的ip地址和端口号绑定到网络节点endpoint上
                IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(Port));
                //注意: 这里是客服端套接字连接到Connect网络节点 不是Bind
                socketClient.Connect(endpoint);
                //socket.Bind(endPoint);//将socket绑定到一个端点上
               // socketClient.Listen(10);//设置连接队列的长度                //new一个新线程 调用下面的接受服务端发来信息的方法RecMsg
                threadClient = new Thread(RecMsg);
                //将窗体线程设置为与后台同步
                threadClient.IsBackground = true;
                //启动线程
                threadClient.Start();
                interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(SetDevState);
                //SendCMD(Convert.ToInt16("1"), 0xFFFF, RelayType.BYTECTRL);
                return true;
            }
            catch (Exception err)
            {
                throw;
                //return false;
               // Response.Write(err.Message);
               // MessageBox.Show(err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //创建命令
        public static byte[] CreateCMD(short _Address, int _Line, RelayType _type)
        {
            byte[] command = new byte[8];
            command[0] = (byte)ProtocolHead;
            command[1] = (byte)(_Address % 256);
            command[2] = (byte)_type;
            command[3] = 0;
            command[4] = 0;
            command[5] = (byte)(_Line >> 8);
            command[6] = (byte)_Line;
            int sum = 0;
            for (int i = 0; i <= 6; i++)
            {
                sum = sum + command[i];
            }
            command[7] = (byte)(sum % 256);
            return command;
        }
        private void SendToBus(short _Address, int _Line, RelayType _type)
        {            tempCMD = CreateCMD(_Address, _Line, _type);
            if (_type == RelayType.INQUIRE)
            {
                CMDLIST.Add(tempCMD);
            }            if (CMDLIST.Count > 0)
            {
                //SendCMDToBUS(CMDLIST[0]);
                clientSendMsg(CMDLIST[0]);
                CMDLIST.Remove(CMDLIST[0]);
            }
        }
                private static void SendCMD(short _Address, int _Line, RelayType _type)
        {
            byte[] tempCMD = new byte[8];
            tempCMD = CreateCMD(_Address, _Line, _type);
            //            clientSendMsg(m_SendCMD);
            clientSendMsg(tempCMD);        }    }
然后,我在网页的代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace StockManage
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RelayMgr.RelayMgr rm=new RelayMgr.RelayMgr();
                if (rm.Connect("192.168.1.110", "6000")) 
                  Response.Write("已联接");               
               // if (RelayMgr.RelayMgr.r("192.168.1.110", "6000") > 1)            }
        }
        protected void Page_unLoad(object sender, EventArgs e)
        {
           //Response.Write("继开连接");
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            RelayMgr.RelayMgr rm = new RelayMgr.RelayMgr();
            rm.OpenAll();        }        protected void Button2_Click(object sender, EventArgs e)
        {
            RelayMgr.RelayMgr rm = new RelayMgr.RelayMgr();
            rm.CloseAll();
        }
    }
}当我第一次按连接,时正常,可以第二次连接里就出错提示:

解决方案 »

  1.   

     
     socketClient.Close();
    这里释放了,且asp.net 不是自己会回收资源吗?
      

  2.   

    http://www.cnblogs.com/jamesping/articles/2071932.html
      

  3.   

    服务http://localhost/WCF/Service1/不支持内容类型 text/xml;
    charset=utf-8.客户端和服务绑定可能不匹配。
      

  4.   

    服务启动了,但是会提示:服务http://localhost/WCF/Service1/不支持内容类型 text/xml;
    charset=utf-8.客户端和服务绑定可能不匹配。 怎么解决呢?
      

  5.   


    网页好像实现不了Socket,他一加载完页面,这个页面的类就销毁了。你还是改成服务的型式吧,