完善基本模式的监听断开处理
This commit is contained in:
parent
25ad38edc5
commit
ae44b8eba9
@ -257,36 +257,41 @@ namespace HaoYueNet.ClientNetwork
|
||||
OnReceiveData(CmdID, Error, resultdata);
|
||||
}
|
||||
|
||||
|
||||
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
|
||||
MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
|
||||
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
|
||||
private void Recive(object o)
|
||||
{
|
||||
var client = o as Socket;
|
||||
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
|
||||
|
||||
while (true)
|
||||
{
|
||||
byte[] buffer = new byte[1024 * 1024 * 2];
|
||||
int effective = 0;
|
||||
try
|
||||
{
|
||||
effective = client.Receive(buffer);
|
||||
if (effective == 0)
|
||||
effective = client.Receive(reciveBuffer);
|
||||
if (effective == 0)//为0表示已经断开连接
|
||||
{
|
||||
continue;
|
||||
//清理数据
|
||||
reciveMemoryStream.SetLength(0);
|
||||
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
|
||||
//远程主机强迫关闭了一个现有的连接
|
||||
OnCloseReady();
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
//清理数据
|
||||
reciveMemoryStream.SetLength(0);
|
||||
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
//远程主机强迫关闭了一个现有的连接
|
||||
OnCloseReady();
|
||||
return;
|
||||
//断开连接
|
||||
}
|
||||
|
||||
|
||||
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
|
||||
byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
|
||||
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
|
||||
byte[] getData = reciveMemoryStream.ToArray();//将内存流中的消息体写入字节数组
|
||||
int StartIndex = 0;//设置一个读取数据的起始下标
|
||||
|
||||
while (true)
|
||||
@ -314,10 +319,10 @@ namespace HaoYueNet.ClientNetwork
|
||||
*/
|
||||
|
||||
//流复用的方式 不用重新new申请
|
||||
memoryStream.Position = 0;
|
||||
memoryStream.SetLength(0);
|
||||
reciveMemoryStream.Position = 0;
|
||||
reciveMemoryStream.SetLength(0);
|
||||
|
||||
memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
|
||||
reciveMemoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
|
||||
break;
|
||||
}
|
||||
else
|
||||
@ -341,7 +346,6 @@ namespace HaoYueNet.ClientNetwork
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,39 +189,45 @@ namespace HaoYueNet.ClientNetwork.OtherMode
|
||||
OnReceive(socket,data);
|
||||
}
|
||||
|
||||
MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
|
||||
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
|
||||
private void Recive(object o)
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
|
||||
var client = o as Socket;
|
||||
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
|
||||
|
||||
while (true)
|
||||
{
|
||||
byte[] buffer = new byte[1024 * 1024 * 2];
|
||||
int effective = 0;
|
||||
try
|
||||
{
|
||||
effective = client.Receive(buffer);
|
||||
if (effective == 0)
|
||||
effective = client.Receive(reciveBuffer);
|
||||
if (effective == 0)//为0表示已经断开连接,放到后面处理
|
||||
{
|
||||
continue;
|
||||
//清理数据
|
||||
reciveMemoryStream.SetLength(0);
|
||||
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
|
||||
//远程主机强迫关闭了一个现有的连接
|
||||
OnCloseReady(client);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//清理数据
|
||||
reciveMemoryStream.SetLength(0);
|
||||
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
//远程主机强迫关闭了一个现有的连接
|
||||
OnCloseReady(client);
|
||||
return;
|
||||
//断开连接
|
||||
}
|
||||
if (effective > 0)//如果接受到的消息不为0(不为空)
|
||||
{
|
||||
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
|
||||
DataCallBackReady(client, memoryStream.ToArray());
|
||||
|
||||
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
|
||||
DataCallBackReady(client, reciveMemoryStream.ToArray());
|
||||
//流复用的方式 不用重新new申请
|
||||
memoryStream.Position = 0;
|
||||
memoryStream.SetLength(0);
|
||||
}
|
||||
reciveMemoryStream.Position = 0;
|
||||
reciveMemoryStream.SetLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,39 +192,44 @@ namespace HaoYueNet.ClientNetwork.OtherMode
|
||||
OnReceiveData(data);
|
||||
}
|
||||
|
||||
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
|
||||
MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
|
||||
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
|
||||
private void Recive(object o)
|
||||
{
|
||||
var client = o as Socket;
|
||||
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
|
||||
|
||||
while (true)
|
||||
{
|
||||
byte[] buffer = new byte[1024 * 1024 * 2];
|
||||
int effective = 0;
|
||||
try
|
||||
{
|
||||
effective = client.Receive(buffer);
|
||||
if (effective == 0)
|
||||
effective = client.Receive(reciveBuffer);
|
||||
if (effective == 0)//为0表示已经断开连接,放到后面处理
|
||||
{
|
||||
continue;
|
||||
//尝试性,清理数据
|
||||
reciveMemoryStream.SetLength(0);
|
||||
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
|
||||
//远程主机强迫关闭了一个现有的连接
|
||||
OnCloseReady();
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//尝试性,清理数据
|
||||
reciveMemoryStream.SetLength(0);
|
||||
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
//断开连接
|
||||
//远程主机强迫关闭了一个现有的连接
|
||||
OnCloseReady();
|
||||
return;
|
||||
//断开连接
|
||||
}
|
||||
if (effective > 0)//如果接受到的消息不为0(不为空)
|
||||
{
|
||||
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
|
||||
DataCallBackReady(memoryStream.ToArray());
|
||||
|
||||
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
|
||||
DataCallBackReady(reciveMemoryStream.ToArray());
|
||||
//流复用的方式 不用重新new申请
|
||||
memoryStream.Position = 0;
|
||||
memoryStream.SetLength(0);
|
||||
}
|
||||
reciveMemoryStream.Position = 0;
|
||||
reciveMemoryStream.SetLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -414,7 +414,7 @@ namespace HaoYueNet.ServerNetwork
|
||||
}
|
||||
else
|
||||
{
|
||||
//尝试性,清理数据
|
||||
//清理数据
|
||||
token.memoryStream.SetLength(0);
|
||||
token.memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user