c#写的socket异步程序 运行一小时左右出错,高手们帮帮小弟 c#写的socket异步程序 每三秒接收一次请求并发送一次 运行一个小时左右 程序提示“索引超出了数组界限。” 有没有相同经验的高手帮我解决一下,感激涕零 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void ReceiveCallback(IAsyncResult ar) { Socket temp = ar.AsyncState as Socket ; try { int bytesRead = temp.EndReceive(ar); if (bytesRead > 0) { //string Receive = Encoding.ASCII.GetString(buffer, 0, bytesRead); string Receive = tf.ReceiveXML(buffer, bytesRead); if (onReceiveCompleted != null) onReceiveCompleted(new IPEndPoint(m_RemoteIP, m_RemotePort), Receive); } temp.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), temp); } catch (SocketException se) { doClose(se); } catch (System.IO.IOException io) { doClose(io); } catch (ObjectDisposedException ode) { doClose(ode); } catch (ArgumentException ae) { doClose(ae); } catch (Exception e) { //throw e; System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); sockconnect(); } } private void ReceiveCallback(IAsyncResult ar) { Socket temp = ar.AsyncState as Socket ; try { int bytesRead = temp.EndReceive(ar); if (bytesRead > 0) { //string Receive = Encoding.ASCII.GetString(buffer, 0, bytesRead); string Receive = tf.ReceiveXML(buffer, bytesRead); if (onReceiveCompleted != null) onReceiveCompleted(new IPEndPoint(m_RemoteIP, m_RemotePort), Receive); } temp.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), temp); } catch (SocketException se) { doClose(se); } catch (System.IO.IOException io) { doClose(io); } catch (ObjectDisposedException ode) { doClose(ode); } catch (ArgumentException ae) { doClose(ae); } catch (Exception e) { //throw e; System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); sockconnect(); } } 那你就带程序运行着看出错时断在哪里这种问题一般时你从缓存取数据超出了数组的界限你的数组比如为1024,按照你的取值计算方法,比如有个循环,i*(x)取X位置的值这是就可能越界了 用正则查找字符串的数字 新手请教 提取邮箱用户名和域名 问题 父控件怎么得到子控件的KeyUp消息? 求怎样才能画出环绕一个字符最小的圆 ========客户端的textbox值如何被另一个框架页面的后台获取?======= 能不能不用Form直接用Notifyicon? 有用C#开发腾讯通RTX插件高手帮忙看一下 呼叫中心的开发设计(欢迎讨论) OleDbCommandBuilder是个什么东西,是干什么用的?怎么使用? 除了用关系外,怎样能在DataGrid中实现主从表的嵌套 未能找到与解决方案关联的源代码管理提供程序 多线程程序运行中不能计算数据
{
Socket temp = ar.AsyncState as Socket ;
try
{
int bytesRead = temp.EndReceive(ar);
if (bytesRead > 0)
{
//string Receive = Encoding.ASCII.GetString(buffer, 0, bytesRead);
string Receive = tf.ReceiveXML(buffer, bytesRead);
if (onReceiveCompleted != null)
onReceiveCompleted(new IPEndPoint(m_RemoteIP, m_RemotePort), Receive);
}
temp.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), temp);
}
catch (SocketException se)
{
doClose(se);
}
catch (System.IO.IOException io)
{
doClose(io);
}
catch (ObjectDisposedException ode)
{
doClose(ode);
}
catch (ArgumentException ae)
{
doClose(ae);
}
catch (Exception e)
{
//throw e;
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
sockconnect();
}
}
private void ReceiveCallback(IAsyncResult ar)
{
Socket temp = ar.AsyncState as Socket ;
try
{
int bytesRead = temp.EndReceive(ar);
if (bytesRead > 0)
{
//string Receive = Encoding.ASCII.GetString(buffer, 0, bytesRead);
string Receive = tf.ReceiveXML(buffer, bytesRead);
if (onReceiveCompleted != null)
onReceiveCompleted(new IPEndPoint(m_RemoteIP, m_RemotePort), Receive);
}
temp.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), temp);
}
catch (SocketException se)
{
doClose(se);
}
catch (System.IO.IOException io)
{
doClose(io);
}
catch (ObjectDisposedException ode)
{
doClose(ode);
}
catch (ArgumentException ae)
{
doClose(ae);
}
catch (Exception e)
{
//throw e;
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
sockconnect();
}
}
看出错时断在哪里
这种问题一般时你从缓存取数据超出了数组的界限
你的数组比如为1024,按照你的取值计算方法,比如有个循环,i*(x)取X位置的值
这是就可能越界了