我自已网络开发基础比较差。翻了点资料就上阵了,还有些地方没看明白。以下是我从msdn找的帮助文档中的关键代码,还是不太明白,大家帮忙看看,是不是用这个。注释是我加的。=================================================================== //接收方法,静态成员 private static void Receive(Socket client) { try { // Create the state object. StateObject state = new StateObject(); state.workSocket = client; // Begin receiving the data from the remote device. client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); //new AsyncCallback(ReceiveCallback)调用委托,方法ReceiveCallBack; } catch (Exception e) { //.. } } //以下是ReceiveCallback方法,“IAsyncResult ar”是什么意思? private static void ReceiveCallback( IAsyncResult ar ) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject) ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); 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)); // Get the rest of the data. client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); } else { // All the data has arrived; put it in response. if (state.sb.Length > 1) { response = state.sb.ToString(); } // Signal that all bytes have been received. receiveDone.Set(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } 最重要的问题是,这个委托总是能够接收到不定时发过来的数据吗? 只要一有数据发过来,ReceiveCallback方法就能触发吗?
TO: cchinasp(风和影的恋人) 基本上是这样了,不过要注意的是当异步委托ReceiveCallback被触发后,需要再次调用BeginReceive(),还有可以指定接受字节数,利用它可以精确控制完整接受一个有长度描述的自定义报文。
else { // All the data has arrived; put it in response. if (state.sb.Length > 1) { response = state.sb.ToString(); } // Signal that all bytes have been received.
.........
}
但是我不明白这样的死循环会不会有什么问题,我简单试了一下,发现有很耗资源,不知是不是我写错了。
turnmissile(会翻跟头的导弹),可以给我一小段代码参考参考吗?
加Timer计时3minute,后断开。
你可以把监听线程放在page_Load事件中开始,这样只要你登陆就开始监听
并接受对放发来的信息问题2:
好象有个什么ConnectionTimeOut属性(不记得了,你查下)
如果不行的话用Timer timer=new Timer();
timer.Interval=1000*60*3 //3 minutes
timer.Tick+=new EventHandler(timer_Tick);
void timer_Tick(object obj,EventArgs ea)
{...} wish u good luck
Greatsft
具体的使用方法可以参见Socket类的帮助文档。
//接收方法,静态成员
private static void Receive(Socket client) {
try {
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client; // Begin receiving the data from the remote device. client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state); //new AsyncCallback(ReceiveCallback)调用委托,方法ReceiveCallBack;
} catch (Exception e) {
//..
}
}
//以下是ReceiveCallback方法,“IAsyncResult ar”是什么意思? private static void ReceiveCallback( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket; // Read data from the remote device.
int bytesRead = client.EndReceive(ar); 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)); // Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
最重要的问题是,这个委托总是能够接收到不定时发过来的数据吗?
只要一有数据发过来,ReceiveCallback方法就能触发吗?
基本上是这样了,不过要注意的是当异步委托ReceiveCallback被触发后,需要再次调用BeginReceive(),还有可以指定接受字节数,利用它可以精确控制完整接受一个有长度描述的自定义报文。
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
// Signal that all bytes have been received.
//在这里加上要调用的委托!!!!!!
receiveDone.Set();
}