..
This commit is contained in:
parent
ae44b8eba9
commit
665ef09bf7
@ -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
|
||||||
|
@ -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);
|
||||||
|
@ -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
|
||||||
{
|
{
|
||||||
|
@ -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
|
||||||
{
|
{
|
||||||
@ -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;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
{
|
{
|
||||||
@ -27,7 +27,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2
|
|||||||
|
|
||||||
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)
|
||||||
@ -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;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
@ -101,7 +101,7 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
|
|||||||
|
|
||||||
~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();
|
||||||
@ -194,39 +194,45 @@ namespace HaoYueNet.ClientNetworkNet.Standard2.OtherMode
|
|||||||
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申请
|
||||||
memoryStream.Position = 0;
|
reciveMemoryStream.Position = 0;
|
||||||
memoryStream.SetLength(0);
|
reciveMemoryStream.SetLength(0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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(不为空)
|
|
||||||
{
|
reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中
|
||||||
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
|
DataCallBackReady(reciveMemoryStream.ToArray());
|
||||||
DataCallBackReady(memoryStream.ToArray());
|
|
||||||
//流复用的方式 不用重新new申请
|
//流复用的方式 不用重新new申请
|
||||||
memoryStream.Position = 0;
|
reciveMemoryStream.Position = 0;
|
||||||
memoryStream.SetLength(0);
|
reciveMemoryStream.SetLength(0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user