当前位置:编程学习 > C#/ASP.NET >>

业务问一下,心跳设置


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public class StateObject
        {
            // Client  socket.
            public Socket workSocket = null;
            // Size of receive buffer.
            public const int BufferSize = 1024;
            // Receive buffer.
            public byte[] buffer = new byte[BufferSize];
            // Received data string.
            public StringBuilder sb = new StringBuilder();
        }

        public Form1()
        {
            InitializeComponent();
        }
        Socket Service = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        public static ManualResetEvent allDone = new ManualResetEvent(false);

        private void button1_Click(object sender, EventArgs e)
        {
           Thread start = new Thread(new ThreadStart(StartList));
             start.Start();
          //  StartList();
        }
        private void StartList()
        {
            Service.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000));
            Service.Listen(10);

            while (true)
            {
                allDone.Reset();
                MessageBox.Show("开始接收");
                Service.BeginAccept(new AsyncCallback(AcceptAsyncCallback), Service);
                allDone.WaitOne();
            }
        }
        private void AcceptAsyncCallback(IAsyncResult ir)
        {

            allDone.Set();


            Socket listener = (Socket)ir.AsyncState;//Service
            Socket handler = listener.EndAccept(ir);
            StateObject state = new StateObject();
            state.workSocket = handler;

            handler.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveAsyn), state);

           

        }
        const int KEEPALIVESIZE = 12;
        static byte[] KeepAlive(int onOff, int keepAliveTime, int keepAliveInterval)
        {
            byte[] buffer = new byte[KEEPALIVESIZE];
            BitConverter.GetBytes(onOff).CopyTo(buffer, 0);
            BitConverter.GetBytes(keepAliveTime).CopyTo(buffer, 4);
            BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer, 8);
            return buffer;
        }

        private void XinTiao(Socket handler)
        {
            byte[] inValue = new byte[] { 1, 0, 0, 0, 0x20, 0x4e, 0, 0, 0xd0, 0x07, 0, 0 };// 
            handler.IOControl(IOControlCode.KeepAliveValues,inValue,null);
        }
        private void ReceiveAsyn(IAsyncResult ir)
        {
            try
            {
                String content = String.Empty;

                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                StateObject state = (StateObject)ir.AsyncState;
                Socket handler = state.workSocket;

                // Read data from the client socket. 
                int bytesRead = handler.EndReceive(ir);
                XinTiao(handler);  //心跳设置
                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(
                        state.buffer, 0, bytesRead));

                    // Check for end-of-file tag. If it is not there, read 
                    // more data.
                    content = state.sb.ToString();
                    if (content.IndexOf("<EOF>") > -1)
                    {
                        // All the data has been read from the 
                        // client. Display it on the console.
                        Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                            content.Length, content);
                        // Echo the data back to the client.
                        // Send(handler, content);
                    }
                    else
                    {
                        // Not all data received. Get more.
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveAsyn), state);
                    }

                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    


    }
}


到网上看的,我怎么就抓不到异常啊。 --------------------编程问答-------------------- catch --------------------编程问答-------------------- 有无发生异常?没有的情况下,你怎么抓? --------------------编程问答-------------------- 关掉客户端。 --------------------编程问答-------------------- 求个简单明子,好用的心跳处理程序 --------------------编程问答-------------------- 求个简单明了,高效,好用的心跳处理程序 --------------------编程问答--------------------
引用 5 楼 hhc123 的回复:
求个简单明了,高效,好用的心跳处理程序

同求.... --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- 在异步线程里当然抓不到异常,你写成同步方式再调试代码 --------------------编程问答--------------------

const int KEEPALIVESIZE = 12;
static byte[] KeepAlive(int onOff, int keepAliveTime, int keepAliveInterval)        {            byte[] buffer = new byte[KEEPALIVESIZE];            BitConverter.GetBytes(onOff).CopyTo(buffer, 0);           
 BitConverter.GetBytes(keepAliveTime).CopyTo(buffer, 4);            BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer, 8);           
 return buffer;        }
//设置的地方:
Socket clientSocket = Socket.EndAccept(ar); //在 EndAccept 以后clientSocket.IOControl(IOControlCode.KeepAliveValues, KeepAlive(0, 30000, 5000), null);

http://wenwen.soso.com/z/q120706478.htm
网上找的,我怎么就跳不动了。客户端关了也没见服务端异常 --------------------编程问答-------------------- 原来没人了啊 --------------------编程问答-------------------- ??????
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,