//申明
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)
{
}
}各位大侠们 小弟实在不清楚问题出在哪里 帮帮忙忙啊!!!
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)
{
}
}各位大侠们 小弟实在不清楚问题出在哪里 帮帮忙忙啊!!!
Collection was modified; enumeration operation may not execute.
at System.Collections.HashtableEnumerator.MoveNext()
at lj.lj.SendAllInit()可是还是不清楚问题出在哪里啊!!!!!!!!!!!!!!!
lock(socketList)
{
IDictionaryEnumerator myEnumerator = socketList.GetEnumerator();
while (myEnumerator.MoveNext())
{
...
}
}
先这样优化吧 要测试几天才知道ok了没.... 先谢了啊