业务问一下,心跳设置

hhc123 2010-06-01 09:55:22

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



}
}


到网上看的,我怎么就抓不到异常啊。
...全文
164 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhc123 2010-06-03
  • 打赏
  • 举报
回复
原来没人了啊
hhc123 2010-06-03
  • 打赏
  • 举报
回复
??????
hhc123 2010-06-02
  • 打赏
  • 举报
回复

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
网上找的,我怎么就跳不动了。客户端关了也没见服务端异常
deknight 2010-06-01
  • 打赏
  • 举报
回复
mark
铛铛 2010-06-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hhc123 的回复:]
求个简单明了,高效,好用的心跳处理程序
[/Quote]
同求....
hhc123 2010-06-01
  • 打赏
  • 举报
回复
求个简单明了,高效,好用的心跳处理程序
hhc123 2010-06-01
  • 打赏
  • 举报
回复
求个简单明子,好用的心跳处理程序
hhc123 2010-06-01
  • 打赏
  • 举报
回复
关掉客户端。
捷哥1999 2010-06-01
  • 打赏
  • 举报
回复
有无发生异常?没有的情况下,你怎么抓?
hhc123 2010-06-01
  • 打赏
  • 举报
回复
catch
LutzMark 2010-06-01
  • 打赏
  • 举报
回复
在异步线程里当然抓不到异常,你写成同步方式再调试代码
w79841446 2010-06-01
  • 打赏
  • 举报
回复
mark

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧