public void run()
{
if( done )
done = false; while( !done )
{
try
{
if( socketChannel != null )
{
socketChannel.Blocking = false;
/** 读取数据 */
System.Array.Clear(receiveBuff, 0, receiveBuff.Length);
this.waitonenano();
int nBytes = socketChannel.Receive( receiveBuff);
while( nBytes > 0 || runOnceMore)
{
/** 将数据全部转移存入缓存 */
copyData ( receiveBuff, count, nBytes);
/** count代表objectBuf中现有的(剩下的)字节数 */
count += nBytes;
while( count >= 4)
{
if( dlen < 0)
{
dlen = calcLen(objectBuf);
} while ( count < dlen +4 )
{
System.Array.Clear(receiveBuff, 0, receiveBuff.Length);
nBytes = socketChannel.Receive(receiveBuff);
/** 将数据全部转移存入缓存 */
copyData ( receiveBuff, count, nBytes);
count += nBytes;
}
if( dlen > 0 /** 有数据 */
&& (count - 4) >= dlen /** 数据够用 */)
{
try
{
int comm = com.verygis.veryobject.pdu.VOPDUObjectCreator.getCommand( objectBuf, 4);
firePropertyChange( comm + "", 4, objectBuf);
}
catch(Exception e)
{
}
/** 重置,去掉已经使用的字节数,安全拷贝 */
System.Array.Copy(objectBuf, dlen + 4, objectBuf, 0, count - 4 - dlen);
count = count - 4 - dlen;
dlen = -1;
if (count >= 4)/** 处理最后一次 */
{
runOnceMore = true;
}
else
runOnceMore = false;
}
}
/** 继续读取数据 */
System.Array.Clear(receiveBuff, 0, receiveBuff.Length);
if ( count >= 4)
nBytes = socketChannel.Receive(receiveBuff);
else
{
nBytes = 0;
}
}
if( nBytes == 0 )
{
waitonenano();
}
if( nBytes == -1 )
{
socketChannel.Close();
socketChannel = null;
}
}
}
catch(System.IO.IOException e)
{
try
{
socketChannel.Close();
socketChannel = null;
setClientModeOff();
}
catch(System.IO.IOException e1)
{
}
}
}
}
private void copyData ( byte[] src_receiveBuff,
int count, int nBytes )
{
bool bExtend = false;
while( count + nBytes > objectBufSize )
{
bExtend = true;
objectBufSize *= 2;
}
if( bExtend )
{
byte[] tmp_buf = new byte[objectBufSize];
System.Array.Copy( objectBuf, 0, tmp_buf, 0, count);
objectBuf = null;
objectBuf = tmp_buf;
}
System.Array.Copy(src_receiveBuff,0, objectBuf,count, nBytes);
}
abstract public void setClientModeOn();
abstract public void setClientModeOff();
/**
* 返回客户端状态
*/
public String getClientMode()
{
return mode;
}
/**
* 获取当前连接的System.Net.Sockets.Socket
*/
public System.Net.Sockets.Socket getSocket()
{
return socketChannel;
}
}
{
if( done )
done = false; while( !done )
{
try
{
if( socketChannel != null )
{
socketChannel.Blocking = false;
/** 读取数据 */
System.Array.Clear(receiveBuff, 0, receiveBuff.Length);
this.waitonenano();
int nBytes = socketChannel.Receive( receiveBuff);
while( nBytes > 0 || runOnceMore)
{
/** 将数据全部转移存入缓存 */
copyData ( receiveBuff, count, nBytes);
/** count代表objectBuf中现有的(剩下的)字节数 */
count += nBytes;
while( count >= 4)
{
if( dlen < 0)
{
dlen = calcLen(objectBuf);
} while ( count < dlen +4 )
{
System.Array.Clear(receiveBuff, 0, receiveBuff.Length);
nBytes = socketChannel.Receive(receiveBuff);
/** 将数据全部转移存入缓存 */
copyData ( receiveBuff, count, nBytes);
count += nBytes;
}
if( dlen > 0 /** 有数据 */
&& (count - 4) >= dlen /** 数据够用 */)
{
try
{
int comm = com.verygis.veryobject.pdu.VOPDUObjectCreator.getCommand( objectBuf, 4);
firePropertyChange( comm + "", 4, objectBuf);
}
catch(Exception e)
{
}
/** 重置,去掉已经使用的字节数,安全拷贝 */
System.Array.Copy(objectBuf, dlen + 4, objectBuf, 0, count - 4 - dlen);
count = count - 4 - dlen;
dlen = -1;
if (count >= 4)/** 处理最后一次 */
{
runOnceMore = true;
}
else
runOnceMore = false;
}
}
/** 继续读取数据 */
System.Array.Clear(receiveBuff, 0, receiveBuff.Length);
if ( count >= 4)
nBytes = socketChannel.Receive(receiveBuff);
else
{
nBytes = 0;
}
}
if( nBytes == 0 )
{
waitonenano();
}
if( nBytes == -1 )
{
socketChannel.Close();
socketChannel = null;
}
}
}
catch(System.IO.IOException e)
{
try
{
socketChannel.Close();
socketChannel = null;
setClientModeOff();
}
catch(System.IO.IOException e1)
{
}
}
}
}
private void copyData ( byte[] src_receiveBuff,
int count, int nBytes )
{
bool bExtend = false;
while( count + nBytes > objectBufSize )
{
bExtend = true;
objectBufSize *= 2;
}
if( bExtend )
{
byte[] tmp_buf = new byte[objectBufSize];
System.Array.Copy( objectBuf, 0, tmp_buf, 0, count);
objectBuf = null;
objectBuf = tmp_buf;
}
System.Array.Copy(src_receiveBuff,0, objectBuf,count, nBytes);
}
abstract public void setClientModeOn();
abstract public void setClientModeOff();
/**
* 返回客户端状态
*/
public String getClientMode()
{
return mode;
}
/**
* 获取当前连接的System.Net.Sockets.Socket
*/
public System.Net.Sockets.Socket getSocket()
{
return socketChannel;
}
}
楼主可以精简一下 把出错的那部分注释一下好让别人容易看懂一点
我也遇到这样的问题了