先贴代码 public byte[] SendCommand(byte[] cmd, int receiveLength)
{
lock (thisLock)
{
try
{
int receiveCount = 0;
if (socket != null)
{
if (socket.Connected)
{
byte[] value = new byte[receiveLength];
int length = 0;
socket.Send(cmd);
while (length < receiveLength)
{
if (receiveCount++>10)
{
throw (new SocketException());
}
if (socket.Available > 0)
{
int count = socket.Receive(data);
if (count < 0)
count = 0;
errorCount = 0;
Array.ConstrainedCopy(data, 0, value, length, count);
length += count;
}
else
{
System.Threading.Thread.Sleep(1);
}
}
return value;
}
} return null;
} catch (SocketException ex)
{
System.Threading.Thread.Sleep(1000);
errorCount++;
if (errorCount > 3)
{
errorCount = 0;
throw new Exception("SocketError");
}
else
{
return SendCommand(cmd, receiveLength);
}
}
catch (Exception e)
{
System.Threading.Thread.Sleep(1000);
throw e;
}
}
}现在的情况是经常在catch中捕获ErrorCode=0的异常,这是怎么回事,有人知道吗
{
lock (thisLock)
{
try
{
int receiveCount = 0;
if (socket != null)
{
if (socket.Connected)
{
byte[] value = new byte[receiveLength];
int length = 0;
socket.Send(cmd);
while (length < receiveLength)
{
if (receiveCount++>10)
{
throw (new SocketException());
}
if (socket.Available > 0)
{
int count = socket.Receive(data);
if (count < 0)
count = 0;
errorCount = 0;
Array.ConstrainedCopy(data, 0, value, length, count);
length += count;
}
else
{
System.Threading.Thread.Sleep(1);
}
}
return value;
}
} return null;
} catch (SocketException ex)
{
System.Threading.Thread.Sleep(1000);
errorCount++;
if (errorCount > 3)
{
errorCount = 0;
throw new Exception("SocketError");
}
else
{
return SendCommand(cmd, receiveLength);
}
}
catch (Exception e)
{
System.Threading.Thread.Sleep(1000);
throw e;
}
}
}现在的情况是经常在catch中捕获ErrorCode=0的异常,这是怎么回事,有人知道吗
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货