求助:socket发送偶而抛出异常问题!!!

linphantom 2012-07-18 11:27:15
//申明
private Hashtable tempsocketList = new Hashtable();
private Hashtable socketList = new Hashtable();
private TcpListener listenmo;

//启动socket
listenmo = new TcpListener(8000);
listenmo.Start();

//分配socket的线程
private void ReceiveDeal()
{
try
{
lock(tempsocketList)
{
byte[] buf = new byte[10];
byte[] tempbuf;
string command,msg;
int count = 0;
IDictionaryEnumerator myEnumerator = tempsocketList.GetEnumerator();
while (myEnumerator.MoveNext())
{
Socket socket = myEnumerator.Value as Socket;
try
{
if (socket.Connected)
{
while (socket.Available > 0)
{
//自定了个简单的传输协议
socket.Receive(buf, 0, 10, SocketFlags.None);
command = Encoding.ASCII.GetString(buf, 0, 10).Trim('\0');
count = Convert.ToInt32(command.Substring(5,5));
tempbuf = new byte[count];
socket.Receive(tempbuf, 0, count, SocketFlags.None);
msg = Encoding.ASCII.GetString(tempbuf, 0, count).Trim('\0');

if (msg == "Test")
{

lock(socketList)
{
if (socketList.ContainsKey(command.Substring(0,5)))
socketList.Remove(command.Substring(0,5));
socketList.Add(command.Substring(0,5) , socket);
} tempsocketList.Remove(myEnumerator.Key);

return;
}
}
}
else
{
socket.Close();
tempsocketList.Remove(myEnumerator.Key);
return;
}
}
catch (Exception ex)
{
}
}
}
}
catch (Exception ex)
{
}

}

//发送的函数
public void SendMsg(string ip , string msg)
{
try
{
msg = Convert.ToString(msg.Length).PadLeft(5,'0') + msg;

byte[] buf;
buf = new byte[msg.Length];
Buffer.BlockCopy(Encoding.ASCII.GetBytes(msg), 0, buf, 0, msg.Length);

lock(socketList)
{
(socketList[ip] as Socket).Send(buf);
}
}
catch (Exception ex)
{

}
}


//以下是发送给客户机的函数 每一分多钟会运行一次

//但是每运行1000次左右 总会抛出一两次的异常

//Collection was modified; enumeration operation may not execute.
//at System.Collections.HashtableEnumerator.MoveNext()
//at lj.lj.SendAllInit()

public void SendAllInit()
{
try
{
string ip = "" , msg = "";
int i = 2000;

IDictionaryEnumerator myEnumerator = socketList.GetEnumerator();
while (myEnumerator.MoveNext())
{
ip = myEnumerator.Key as String;

msg = "Init";
//此处省略一堆 msg += string
msg += Convert.ToString(i).PadLeft(4,' ');
SendMsg(ip,msg);
}
}
catch (Exception ex)
{
}
}

各位大侠们 小弟实在不清楚问题出在哪里 帮帮忙忙啊!!!
...全文
128 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nvshenxp 2012-07-18
  • 打赏
  • 举报
回复
日志内容不是很明确了么,因为你之前操作过socketList,有socketList.ReMove()之类的操作,导致列表已经更改,所以影响到SendAllInit()这个函数了。
bdmh 2012-07-18
  • 打赏
  • 举报
回复
基本上断定是因为没有加锁的原因,集合中途被修改
linphantom 2012-07-18
  • 打赏
  • 举报
回复
代码里有加log

日志报错内容如下:
Collection was modified; enumeration operation may not execute.
at System.Collections.HashtableEnumerator.MoveNext()
at lj.lj.SendAllInit()

可是还是不清楚问题出在哪里啊!!!!!!!!!!!!!!!
cyljay 2012-07-18
  • 打赏
  • 举报
回复
抛出了什么异常,并且在哪个函数类抛出的异常?鉴于你说是运行1000次左右会抛出,让你去debug代码不现实,所以这时就凸显出了log的好处了。你可以在相应代码段加上log,然后再判断是在哪里出了问题,出了什么问题。光看代码是永远看不出来的。
Ricercar 2012-07-18
  • 打赏
  • 举报
回复
抛出异常很正常,你要做的是捕获异常,然后分析
先要知道是什么异常
linphantom 2012-07-18
  • 打赏
  • 举报
回复
4 5 楼这么一说 我感觉好像也是 lock 的问题

我不应该在 SendMsg() 这个函数里 lock(socketList)

应该在 SendAllInit()里 这样锁
lock(socketList)
{
IDictionaryEnumerator myEnumerator = socketList.GetEnumerator();
while (myEnumerator.MoveNext())
{
...
}
}


先这样优化吧 要测试几天才知道ok了没.... 先谢了啊

110,533

社区成员

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

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

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