This commit is contained in:
sin365 2024-06-19 16:05:29 +08:00
parent ae44b8eba9
commit 665ef09bf7
8 changed files with 150 additions and 121 deletions

View File

@ -23,11 +23,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client-Cli", "Simple\Client
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetLib_Standard2", "NetLib_Standard2", "{F4C45C48-8011-4782-B0B3-99164D611A6C}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetLib_Standard2", "NetLib_Standard2", "{F4C45C48-8011-4782-B0B3-99164D611A6C}"
EndProject 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 EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ServerNetwork.Standard2", "NetLib_Standard2\HaoYueNet.ServerNetwork.Standard2\HaoYueNet.ServerNetwork.Standard2.csproj", "{6BACBAAB-3777-4165-A2F7-7F9B517286B4}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ServerNetwork.Standard2", "NetLib_Standard2\HaoYueNet.ServerNetwork.Standard2\HaoYueNet.ServerNetwork.Standard2.csproj", "{6BACBAAB-3777-4165-A2F7-7F9B517286B4}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -244,41 +244,45 @@ namespace HaoYueNet.ClientNetwork
OnDataCallBack(CmdID, Error, resultdata); OnDataCallBack(CmdID, Error, resultdata);
} }
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
private void Recive(object o) private void Recive(object o)
{ {
var client = o as Socket; var client = o as Socket;
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true) while (true)
{ {
byte[] buffer = new byte[1024 * 1024 * 2]; int effective = 0;
int effective=0;
try try
{ {
effective = client.Receive(buffer); effective = client.Receive(reciveBuffer);
if (effective == 0) 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(); OnCloseReady();
return; return;
//断开连接 //断开连接
} }
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中 byte[] getData = reciveMemoryStream.ToArray();//将内存流中的消息体写入字节数组
byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
int StartIndex = 0;//设置一个读取数据的起始下标 int StartIndex = 0;//设置一个读取数据的起始下标
while (true) while (true)
{ {
if (effective > 0)//如果接受到的消息不为0不为空 if (effective > 0)//如果接受到的消息不为0不为空
{ {
int HeadLength = 0;//包头长度(包头+包体) int HeadLength = 0;//包头长度(包头+包体)
@ -302,10 +306,10 @@ namespace HaoYueNet.ClientNetwork
*/ */
//流复用的方式 不用重新new申请 //流复用的方式 不用重新new申请
memoryStream.Position = 0; reciveMemoryStream.Position = 0;
memoryStream.SetLength(0); reciveMemoryStream.SetLength(0);
memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流 reciveMemoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
break; break;
} }
else else
@ -314,6 +318,7 @@ namespace HaoYueNet.ClientNetwork
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray()); //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
int CoreLenght = HeadLength - 4; int CoreLenght = HeadLength - 4;
//改为Array.Copy 提升效率 //改为Array.Copy 提升效率
//byte[] retData = new byte[CoreLenght]; //byte[] retData = new byte[CoreLenght];
//Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght); //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Net.Sockets; using System.Net.Sockets;
namespace HaoYueNet.ClientNetworkNet.Standard2 namespace HaoYueNet.ClientNetwork.Standard2
{ {
public static class BaseData public static class BaseData
{ {

View File

@ -4,9 +4,9 @@ using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; 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 public class NetworkHelperCore
{ {
@ -26,9 +26,9 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
private static int MaxSendIndexNum = 3; 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 static int TimerInterval = 3000;
@ -39,7 +39,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
public static int LastConnectPort; public static int LastConnectPort;
public bool bDetailedLog = false; 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("==>初始化网络核心"); LogOut("==>初始化网络核心");
@ -64,7 +64,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
//带回调的 //带回调的
try try
{ {
if(bDetailedLog) if (bDetailedLog)
LogOut("连接到远程IP " + IP + ":" + port); LogOut("连接到远程IP " + IP + ":" + port);
else else
LogOut("连接到远程服务"); LogOut("连接到远程服务");
@ -151,7 +151,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
/// <param name="data"></param> /// <param name="data"></param>
private void SendWithIndex(byte[] data) private void SendWithIndex(byte[] data)
{ {
lock (sendLock) lock (sendLock)
{ {
//增加发送计数 //增加发送计数
SendIndex = MaxSendIndexNum; SendIndex = MaxSendIndexNum;
@ -185,7 +185,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
/// </summary> /// </summary>
/// <param name="CMDID"></param> /// <param name="CMDID"></param>
/// <param name="data">序列化之后的数据</param> /// <param name="data">序列化之后的数据</param>
public void SendToServer(int CMDID,byte[] data) public void SendToServer(int CMDID, byte[] data)
{ {
//LogOut("准备数据 CMDID=> "+CMDID); //LogOut("准备数据 CMDID=> "+CMDID);
/* /*
@ -236,7 +236,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
{ {
OnCloseReady(); OnCloseReady();
} }
private void DataCallBackReady(byte[] data) private void DataCallBackReady(byte[] data)
{ {
@ -260,36 +260,41 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
OnReceiveData(CmdID, Error, resultdata); OnReceiveData(CmdID, Error, resultdata);
} }
MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 byte[] reciveBuffer = new byte[1024 * 1024 * 2];
private void Recive(object o) private void Recive(object o)
{ {
var client = o as Socket; var client = o as Socket;
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true) while (true)
{ {
byte[] buffer = new byte[1024 * 1024 * 2]; int effective = 0;
int effective=0;
try try
{ {
effective = client.Receive(buffer); effective = client.Receive(reciveBuffer);
if (effective == 0) 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(); OnCloseReady();
return; return;
//断开连接 //断开连接
} }
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中 byte[] getData = reciveMemoryStream.ToArray();//将内存流中的消息体写入字节数组
byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
int StartIndex = 0;//设置一个读取数据的起始下标 int StartIndex = 0;//设置一个读取数据的起始下标
while (true) while (true)
@ -317,10 +322,10 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
*/ */
//流复用的方式 不用重新new申请 //流复用的方式 不用重新new申请
memoryStream.Position = 0; reciveMemoryStream.Position = 0;
memoryStream.SetLength(0); reciveMemoryStream.SetLength(0);
memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流 reciveMemoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
break; break;
} }
else else
@ -336,18 +341,14 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
//DataCallBackReady(retData); //DataCallBackReady(retData);
//用Span //用Span
//Span<byte> getData_span = getData; Span<byte> getData_span = getData;
//getData_span = getData_span.Slice(StartIndex + 4,CoreLenght); getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
byte[] getData_span = new byte[CoreLenght]; DataCallBackReady(getData_span.ToArray());
//DATA
Buffer.BlockCopy(getData, StartIndex + 4, getData_span, 0, CoreLenght);
DataCallBackReady(getData_span);
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部) StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
} }
} }
} }
} }
} }

View File

@ -3,9 +3,9 @@ using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; 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 public class NetworkHelperP2PCore
{ {
@ -18,16 +18,16 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
private static int MaxSendIndexNum = 3; 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 static int TimerInterval = 3000;
private System.Timers.Timer _heartTimer; 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("==>初始化网络核心"); LogOut("==>初始化网络核心");
@ -42,7 +42,6 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport)); IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport));
client.Bind(ipe); client.Bind(ipe);
} }
Connect(IP, port);
} }
public bool Connect(string IP, int port) public bool Connect(string IP, int port)
@ -226,7 +225,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
{ {
OnCloseReady(); OnCloseReady();
} }
private void DataCallBackReady(byte[] data) private void DataCallBackReady(byte[] data)
{ {
@ -248,41 +247,45 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
OnDataCallBack(CmdID, Error, resultdata); OnDataCallBack(CmdID, Error, resultdata);
} }
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
private void Recive(object o) private void Recive(object o)
{ {
var client = o as Socket; var client = o as Socket;
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true) while (true)
{ {
byte[] buffer = new byte[1024 * 1024 * 2]; int effective = 0;
int effective=0;
try try
{ {
effective = client.Receive(buffer); effective = client.Receive(reciveBuffer);
if (effective == 0) 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(); OnCloseReady();
return; return;
//断开连接 //断开连接
} }
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中 byte[] getData = reciveMemoryStream.ToArray();//将内存流中的消息体写入字节数组
byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
int StartIndex = 0;//设置一个读取数据的起始下标 int StartIndex = 0;//设置一个读取数据的起始下标
while (true) while (true)
{ {
if (effective > 0)//如果接受到的消息不为0不为空 if (effective > 0)//如果接受到的消息不为0不为空
{ {
int HeadLength = 0;//包头长度(包头+包体) int HeadLength = 0;//包头长度(包头+包体)
@ -306,22 +309,31 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
*/ */
//流复用的方式 不用重新new申请 //流复用的方式 不用重新new申请
memoryStream.Position = 0; reciveMemoryStream.Position = 0;
memoryStream.SetLength(0); reciveMemoryStream.SetLength(0);
memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流 reciveMemoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
break; break;
} }
else else
{ {
//把头去掉,就可以吃了,蛋白质是牛肉的六倍 //把头去掉,就可以吃了,蛋白质是牛肉的六倍
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
int CoreLenght = HeadLength - 4; 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]; byte[] getData_span = new byte[CoreLenght];
//DATA //DATA
Buffer.BlockCopy(getData, StartIndex + 4, getData_span, 0, CoreLenght); Buffer.BlockCopy(getData, StartIndex + 4, getData_span, 0, CoreLenght);
DataCallBackReady(getData_span); DataCallBackReady(getData_span);
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部) StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
} }
} }
} }

View File

@ -7,12 +7,12 @@ using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode namespace HaoYueNet.ClientNetwork.Standard2.OtherMode
{ {
public class NetworkHelperCore_ListenerMode public class NetworkHelperCore_ListenerMode
{ {
private Socket serversocket; private Socket serversocket;
private Dictionary<IntPtr, Socket> mDictHandleClient; private Dictionary<nint, Socket> mDictHandleClient;
//响应倒计时计数最大值 //响应倒计时计数最大值
private static int MaxRevIndexNum = 50; private static int MaxRevIndexNum = 50;
@ -33,7 +33,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
public void Init(int port) public void Init(int port)
{ {
mDictHandleClient = new Dictionary<IntPtr, Socket>(); mDictHandleClient = new Dictionary<nint, Socket>();
LogOut("==>初始化NetworkHelperCore_ListenerMode"); LogOut("==>初始化NetworkHelperCore_ListenerMode");
serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
@ -69,7 +69,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
task.Start(); task.Start();
} }
#region #region
/// <summary> /// <summary>
/// 追加Socket返回下标 /// 追加Socket返回下标
@ -97,12 +97,12 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
mDictHandleClient.Remove(socket.Handle); mDictHandleClient.Remove(socket.Handle);
} }
} }
#endregion #endregion
~NetworkHelperCore_ListenerMode() ~NetworkHelperCore_ListenerMode()
{ {
IntPtr[] keys = mDictHandleClient.Keys.ToArray(); nint[] keys = mDictHandleClient.Keys.ToArray();
for (uint i = 0; i < keys.Length; i++) for (uint i = 0; i < keys.Length; i++)
{ {
mDictHandleClient[keys[i]].Close(); mDictHandleClient[keys[i]].Close();
} }
@ -115,7 +115,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
//data = SendDataWithHead(data); //data = SendDataWithHead(data);
try try
{ {
SendWithIndex(socket,data); SendWithIndex(socket, data);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -130,7 +130,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
/// 发送数据并计数 /// 发送数据并计数
/// </summary> /// </summary>
/// <param name="data"></param> /// <param name="data"></param>
private void SendWithIndex(Socket socket,byte[] data) private void SendWithIndex(Socket socket, byte[] data)
{ {
//增加发送计数 //增加发送计数
SendIndex = MaxSendIndexNum; SendIndex = MaxSendIndexNum;
@ -149,7 +149,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
SendToSocket(socket, data); SendToSocket(socket, data);
} }
#region #region
public delegate void OnConnectedHandler(Socket socket); public delegate void OnConnectedHandler(Socket socket);
public delegate void OnReceiveDataHandler(Socket sk, byte[] data); 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 OnDisconnectHandler(Socket sk);
public delegate void OnNetLogHandler(string msg); public delegate void OnNetLogHandler(string msg);
#endregion #endregion
public event OnConnectedHandler OnConnected; public event OnConnectedHandler OnConnected;
@ -187,46 +187,52 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
OnCloseReady(socket); OnCloseReady(socket);
} }
private void DataCallBackReady(Socket socket,byte[] data) private void DataCallBackReady(Socket socket, byte[] data)
{ {
//增加接收计数 //增加接收计数
RevIndex = MaxRevIndexNum; RevIndex = MaxRevIndexNum;
OnReceive(socket,data); OnReceive(socket, data);
} }
MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
private void Recive(object o) private void Recive(object o)
{ {
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
var client = o as Socket; var client = o as Socket;
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true) while (true)
{ {
byte[] buffer = new byte[1024 * 1024 * 2];
int effective = 0; int effective = 0;
try try
{ {
effective = client.Receive(buffer); effective = client.Receive(reciveBuffer);
if (effective == 0) if (effective == 0)//为0表示已经断开连接放到后面处理
{ {
continue; //清理数据
reciveMemoryStream.SetLength(0);
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
//远程主机强迫关闭了一个现有的连接
OnCloseReady(client);
return;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
//清理数据
reciveMemoryStream.SetLength(0);
reciveMemoryStream.Seek(0, SeekOrigin.Begin);
//远程主机强迫关闭了一个现有的连接 //远程主机强迫关闭了一个现有的连接
OnCloseReady(client); OnCloseReady(client);
return; return;
//断开连接 //断开连接
} }
if (effective > 0)//如果接受到的消息不为0不为空
{ reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中 DataCallBackReady(client, reciveMemoryStream.ToArray());
DataCallBackReady(client, memoryStream.ToArray()); //流复用的方式 不用重新new申请
//流复用的方式 不用重新new申请 reciveMemoryStream.Position = 0;
memoryStream.Position = 0; reciveMemoryStream.SetLength(0);
memoryStream.SetLength(0);
}
} }
} }

View File

@ -3,9 +3,9 @@ using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; 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 public class NetworkHelperCore_SourceMode
{ {
@ -195,39 +195,44 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
OnReceiveData(data); OnReceiveData(data);
} }
MemoryStream memoryStream = new MemoryStream();//开辟一个内存流 MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流
byte[] reciveBuffer = new byte[1024 * 1024 * 2];
private void Recive(object o) private void Recive(object o)
{ {
var client = o as Socket; var client = o as Socket;
//MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
while (true) while (true)
{ {
byte[] buffer = new byte[1024 * 1024 * 2];
int effective = 0; int effective = 0;
try try
{ {
effective = client.Receive(buffer); effective = client.Receive(reciveBuffer);
if (effective == 0) 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(); OnCloseReady();
return; 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);
} }
} }