{
try
{
//
StateObject state = ar.AsyncState as StateObject;
//读取数据
int bytesRead = mSocket.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(UTF8Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
string sb = state.sb.ToString();
if (sb.Substring(sb.Length - 1, 1) == EndChar)
{
//接收完成
//激发事件
if (onStreamData != null)
onStreamData(mID, UTF8Encoding.UTF8.GetBytes(sb));
if (onStringData != null)
onStringData(mID, sb);
//
state = new StateObject();
state.workSocket = mSocket;
}
// Get the rest of the data.
mSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
{
try
{
//Thread.Sleep(10000);
//
byte[] state = ar.AsyncState as byte[];
//读取数据
int bytesRead = mSocket.EndReceive(ar);
if (bytesRead > 0)
{
byte[] receive = new byte[bytesRead];
Array.Copy(state, 0, receive, 0, bytesRead);
lock ((this.queMess as ICollection).SyncRoot)
{
this.queMess.Enqueue(receive);
}
if (threadMess == null)
{
threadMess = new Thread(new ThreadStart(CycleReadQueue));
threadMess.IsBackground = true;
threadMess.Start();
}
if (threadMess.ThreadState == ThreadState.Stopped)
{
threadMess = new Thread(new ThreadStart(CycleReadQueue));
threadMess.IsBackground = true;
threadMess.Start();
}
mSocket.BeginReceive(state, 0, BUFF_SIZE, 0, new AsyncCallback(ReceiveCallback), state);
}
}
{
byte[] receive = new byte[BUFF_SIZE];
int nPositon = 0;
int nTimes = 1;
while (queMess.Count > 0)
{
byte[] mess = queMess.Dequeue();
foreach (byte c in mess)
{
if (nPositon >= BUFF_SIZE * nTimes)
{
nTimes++;
Array.Resize(ref receive, BUFF_SIZE * nTimes);
}
Array.Copy(new byte[] { c }, 0, receive, nPositon, 1);
nPositon++;
string str = UTF8Encoding.UTF8.GetString(receive, 0, nPositon);
//string str = sb.ToString();
if (str.Substring(str.Length - 1, 1) == EndChar)
{
//接收完成
//激发事件
if (onStreamData != null)
onStreamData(mID, UTF8Encoding.UTF8.GetBytes(str));
if (onStringData != null)
onStringData(mID, str);
receive = new byte[BUFF_SIZE];
nPositon = 0;
nTimes = 1;
sb = new StringBuilder();
}
}
}
}