diff --git a/HaoYueNet.sln b/HaoYueNet.sln
index 396c5a4..1c5a707 100644
--- a/HaoYueNet.sln
+++ b/HaoYueNet.sln
@@ -23,11 +23,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client-Cli", "Simple\Client
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetLib_Standard2", "NetLib_Standard2", "{F4C45C48-8011-4782-B0B3-99164D611A6C}"
EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ClientNetworkNet.Standard2", "NetLib_Standard2\HaoYueNet.ClientNetworkNet.Standard2\HaoYueNet.ClientNetworkNet.Standard2.csproj", "{16AF64F5-6BED-4BD5-AD41-39816AD56769}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ClientNetwork.Standard2", "NetLib_Standard2\HaoYueNet.ClientNetworkNet.Standard2\HaoYueNet.ClientNetwork.Standard2.csproj", "{16AF64F5-6BED-4BD5-AD41-39816AD56769}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ServerNetwork.Standard2", "NetLib_Standard2\HaoYueNet.ServerNetwork.Standard2\HaoYueNet.ServerNetwork.Standard2.csproj", "{6BACBAAB-3777-4165-A2F7-7F9B517286B4}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientSaeaTest", "Simple\ClientSaeaTest\ClientSaeaTest.csproj", "{AE22B541-A21E-48F1-9913-C1ACEBF21874}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientSaeaTest", "Simple\ClientSaeaTest\ClientSaeaTest.csproj", "{AE22B541-A21E-48F1-9913-C1ACEBF21874}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
index 290d147..05374e5 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
@@ -244,41 +244,45 @@ namespace HaoYueNet.ClientNetwork
OnDataCallBack(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;
+ 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)
+ 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)
{
-
-
if (effective > 0)//如果接受到的消息不为0(不为空)
{
int HeadLength = 0;//包头长度(包头+包体)
@@ -302,10 +306,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
@@ -314,6 +318,7 @@ namespace HaoYueNet.ClientNetwork
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
int CoreLenght = HeadLength - 4;
+
//改为Array.Copy 提升效率
//byte[] retData = new byte[CoreLenght];
//Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/BaseData.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/BaseData.cs
index 86f99aa..2daaeb4 100644
--- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/BaseData.cs
+++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/BaseData.cs
@@ -1,7 +1,7 @@
using System;
using System.Net.Sockets;
-namespace HaoYueNet.ClientNetworkNet.Standard2
+namespace HaoYueNet.ClientNetwork.Standard2
{
public static class BaseData
{
diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetworkNet.Standard2.csproj b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetwork.Standard2.csproj
similarity index 100%
rename from NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetworkNet.Standard2.csproj
rename to NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetwork.Standard2.csproj
diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperCore.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperCore.cs
index d2c239f..b7e728d 100644
--- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperCore.cs
+++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperCore.cs
@@ -4,9 +4,9 @@ using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
-using static HaoYueNet.ClientNetworkNet.Standard2.BaseData;
+using static HaoYueNet.ClientNetwork.Standard2.BaseData;
-namespace HaoYueNet.ClientNetworkNet.Standard2
+namespace HaoYueNet.ClientNetwork.Standard2
{
public class NetworkHelperCore
{
@@ -26,9 +26,9 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
private static int MaxSendIndexNum = 3;
//响应倒计时计数
- private static int RevIndex=0;
+ private static int RevIndex = 0;
//发送倒计时计数
- private static int SendIndex=0;
+ private static int SendIndex = 0;
//计时器间隔
private static int TimerInterval = 3000;
@@ -39,7 +39,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
public static int LastConnectPort;
public bool bDetailedLog = false;
- public bool Init(string IP, int port,bool isHadDetailedLog = true, bool bBindReuseAddress = false,int bBindport = 0)
+ public bool Init(string IP, int port, bool isHadDetailedLog = true, bool bBindReuseAddress = false, int bBindport = 0)
{
LogOut("==>初始化网络核心");
@@ -64,7 +64,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
//带回调的
try
{
- if(bDetailedLog)
+ if (bDetailedLog)
LogOut("连接到远程IP " + IP + ":" + port);
else
LogOut("连接到远程服务");
@@ -151,7 +151,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
///
private void SendWithIndex(byte[] data)
{
- lock (sendLock)
+ lock (sendLock)
{
//增加发送计数
SendIndex = MaxSendIndexNum;
@@ -185,7 +185,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
///
///
/// 序列化之后的数据
- public void SendToServer(int CMDID,byte[] data)
+ public void SendToServer(int CMDID, byte[] data)
{
//LogOut("准备数据 CMDID=> "+CMDID);
/*
@@ -236,7 +236,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
{
OnCloseReady();
}
-
+
private void DataCallBackReady(byte[] data)
{
@@ -260,36 +260,41 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
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;
+ 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)
+ 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)
@@ -317,10 +322,10 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
*/
//流复用的方式 不用重新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
@@ -336,18 +341,14 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
//DataCallBackReady(retData);
//用Span
- //Span getData_span = getData;
- //getData_span = getData_span.Slice(StartIndex + 4,CoreLenght);
- byte[] getData_span = new byte[CoreLenght];
- //DATA
- Buffer.BlockCopy(getData, StartIndex + 4, getData_span, 0, CoreLenght);
- DataCallBackReady(getData_span);
+ Span getData_span = getData;
+ getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
+ DataCallBackReady(getData_span.ToArray());
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
}
-
}
}
diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperP2PCore.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperP2PCore.cs
index 3720a12..ce9106d 100644
--- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperP2PCore.cs
+++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/NetworkHelperP2PCore.cs
@@ -3,9 +3,9 @@ using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
-using static HaoYueNet.ClientNetworkNet.Standard2.BaseData;
+using static HaoYueNet.ClientNetwork.Standard2.BaseData;
-namespace HaoYueNet.ClientNetworkNet.Standard2
+namespace HaoYueNet.ClientNetwork.Standard2
{
public class NetworkHelperP2PCore
{
@@ -18,16 +18,16 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
private static int MaxSendIndexNum = 3;
//响应倒计时计数
- private static int RevIndex=0;
+ private static int RevIndex = 0;
//发送倒计时计数
- private static int SendIndex=0;
+ private static int SendIndex = 0;
//计时器间隔
private static int TimerInterval = 3000;
private System.Timers.Timer _heartTimer;
- public void Init(string IP, int port, bool bBindReuseAddress = false,int bBindport = 0)
+ public void Init(bool bBindReuseAddress = false, int bBindport = 0)
{
LogOut("==>初始化网络核心");
@@ -42,7 +42,6 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport));
client.Bind(ipe);
}
- Connect(IP, port);
}
public bool Connect(string IP, int port)
@@ -226,7 +225,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
{
OnCloseReady();
}
-
+
private void DataCallBackReady(byte[] data)
{
@@ -248,41 +247,45 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
OnDataCallBack(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;
+ 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)
+ 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)
{
-
-
if (effective > 0)//如果接受到的消息不为0(不为空)
{
int HeadLength = 0;//包头长度(包头+包体)
@@ -306,22 +309,31 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
*/
//流复用的方式 不用重新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
{
//把头去掉,就可以吃了,蛋白质是牛肉的六倍
+ //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
+
int CoreLenght = HeadLength - 4;
+
+ //改为Array.Copy 提升效率
+ //byte[] retData = new byte[CoreLenght];
+ //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
+ //DataCallBackReady(retData);
+
byte[] getData_span = new byte[CoreLenght];
//DATA
Buffer.BlockCopy(getData, StartIndex + 4, getData_span, 0, CoreLenght);
DataCallBackReady(getData_span);
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
+
}
}
}
diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs
index 87ffd68..29d6bf3 100644
--- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs
+++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs
@@ -7,12 +7,12 @@ using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
-namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
+namespace HaoYueNet.ClientNetwork.Standard2.OtherMode
{
public class NetworkHelperCore_ListenerMode
{
private Socket serversocket;
- private Dictionary mDictHandleClient;
+ private Dictionary mDictHandleClient;
//响应倒计时计数最大值
private static int MaxRevIndexNum = 50;
@@ -33,7 +33,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
public void Init(int port)
{
- mDictHandleClient = new Dictionary();
+ mDictHandleClient = new Dictionary();
LogOut("==>初始化NetworkHelperCore_ListenerMode");
serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
@@ -69,7 +69,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
task.Start();
}
-#region
+ #region
///
/// 追加Socket返回下标
@@ -97,12 +97,12 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
mDictHandleClient.Remove(socket.Handle);
}
}
-#endregion
+ #endregion
~NetworkHelperCore_ListenerMode()
{
- IntPtr[] keys = mDictHandleClient.Keys.ToArray();
- for (uint i = 0; i < keys.Length; i++)
+ nint[] keys = mDictHandleClient.Keys.ToArray();
+ for (uint i = 0; i < keys.Length; i++)
{
mDictHandleClient[keys[i]].Close();
}
@@ -115,7 +115,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
//data = SendDataWithHead(data);
try
{
- SendWithIndex(socket,data);
+ SendWithIndex(socket, data);
}
catch (Exception ex)
{
@@ -130,7 +130,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
/// 发送数据并计数
///
///
- private void SendWithIndex(Socket socket,byte[] data)
+ private void SendWithIndex(Socket socket, byte[] data)
{
//增加发送计数
SendIndex = MaxSendIndexNum;
@@ -149,7 +149,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
SendToSocket(socket, data);
}
-#region 事件定义
+ #region 事件定义
public delegate void OnConnectedHandler(Socket socket);
public delegate void OnReceiveDataHandler(Socket sk, byte[] data);
@@ -157,7 +157,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
public delegate void OnDisconnectHandler(Socket sk);
public delegate void OnNetLogHandler(string msg);
-#endregion
+ #endregion
public event OnConnectedHandler OnConnected;
@@ -187,46 +187,52 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
OnCloseReady(socket);
}
- private void DataCallBackReady(Socket socket,byte[] data)
+ private void DataCallBackReady(Socket socket, byte[] data)
{
//增加接收计数
RevIndex = MaxRevIndexNum;
- OnReceive(socket,data);
+ 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());
- //流复用的方式 不用重新new申请
- memoryStream.Position = 0;
- memoryStream.SetLength(0);
- }
+
+ reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
+ DataCallBackReady(client, reciveMemoryStream.ToArray());
+ //流复用的方式 不用重新new申请
+ reciveMemoryStream.Position = 0;
+ reciveMemoryStream.SetLength(0);
}
}
diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs
index c8e40f0..861224e 100644
--- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs
+++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs
@@ -3,9 +3,9 @@ using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
-using static HaoYueNet.ClientNetworkNet.Standard2.BaseData;
+using static HaoYueNet.ClientNetwork.Standard2.BaseData;
-namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
+namespace HaoYueNet.ClientNetwork.Standard2.OtherMode
{
public class NetworkHelperCore_SourceMode
{
@@ -195,39 +195,44 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.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());
- //流复用的方式 不用重新new申请
- memoryStream.Position = 0;
- memoryStream.SetLength(0);
}
+
+ reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
+ DataCallBackReady(reciveMemoryStream.ToArray());
+ //流复用的方式 不用重新new申请
+ reciveMemoryStream.Position = 0;
+ reciveMemoryStream.SetLength(0);
}
}