主要解决问题:粘包与分包
1.什么是粘包:
当我们客户端快速的向服务端发送短小消息时,服务端接受消息会出现粘包的情况,比如:当我没for循环的向服务端发送100次数字,而服务段可能只会受到10次消息,而其中发送的这些消息则是粘在了一起发送的,这就是粘包现象。
2.出现粘包现象的原因:
TCP是以流形式的,他会开辟一个缓冲区,发送端往其中写入数据 每过一段时间就发送出去 然后接收端接收到这些数据 但是并不是说我发送了一次数据就肯定发送出去了 数据会在缓冲区中 有可能后续发送的数据和之前发送的数据同时存在缓冲区中随后一起发送 这就是粘包的一种形式 接收端也有产生粘包的情况 如果应用程序没有及时处理缓冲区中的数据 那么后续到达的数据会继续存放到缓冲区中 也就是2次接收的数据同时存在缓冲区中 下次取缓冲区的时候就会取出2次粘包后的数据 这是粘包的另外一种形式 还有其他许多形式 比如填充缓冲区到一半缓冲区满了直接发送了 但是其实那个包还没填充完全 这个就是不完整的粘包了 剩余数据会在下次发送的时候补上
3.解决方法:
就是采用"数据长度+实际数据"的格式来发送数据 这个"数据长度"的格式是固定宽度的 比如4字节 可以表示0~4GB的宽度了 足够用了 这个宽度说明了后续实际数据的宽度 这样你就可以把粘包后的数据按照正确的宽度取出来了。每次都是取出4字节 随后按照正确的宽度取出后续部分的就OK了
简单来说就是我们将要发送的数据,和要发送的数据占用的字节长度拼接起来发送,当我们在服务端解析的时候在对合成的这个数据进行解析,具体解析过程我们通过代码展示
代码在(一)(二)的基础上完成
首先我们先处理客户端发送时的数据长度,和数据内容整合,代码很简单,不需要过度解释
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JungleWarClient
{
class Message
{
public static byte [] GetBytes(string str)
{
byte[] byteStr = Encoding.UTF8.GetBytes(str);
int Lenght = byteStr.Length;
byte[] LenghtByte = BitConverter.GetBytes(Lenght);
return LenghtByte.Concat(byteStr).ToArray();
}
}
}
然后我们在客户端进行发送消息时,只需要将消息交给这个方法处理过后在进行发送就可以了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Collections;
namespace JungleWarClient
{
class Program
{
static void Main(string[] args)
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 666));
//接收从服务器发送过来的消息
byte[] data = new byte[1024];
int count = clientSocket.Receive(data);
string strReceive = Encoding.UTF8.GetString(data, 0, count);
Console.WriteLine(strReceive);
//向服务器发送消息
//while (true)
//{
// string strSend = Console.ReadLine();
// if (strSend=="c")
// {
// clientSocket.Close();
// return;
// }
// byte[] dataSend = Encoding.UTF8.GetBytes(strSend);
// clientSocket.Send(dataSend);
//}
for (int i = 0; i < 100; i++)
{
clientSocket.Send(Message.GetBytes(i.ToString()));
}
Console.ReadKey();
}
}
}
首先我们也是先对接收到的消息进行处理,代码我已经注释的非常清楚,应该可以理解了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JungleWarServer
{
class Message
{
private byte[] data = new byte[1024];
private int startIndex = 0;//存取了多少个字节的数据在数组里
public void AddCount(int count)
{
startIndex += count;
}
public byte[] Data
{
get { return data; }
}
public int StartIndex
{
get { return startIndex; }
}
public int RemainSize
{
get { return data.Length - startIndex; }
}
/// <summary>
/// 解析数据
/// </summary>
public void ReadMessage()
{
while (true)
{
if (startIndex <= 4)//当存储的数据小于四时说明里面的数据不足,等待下一次补充存储
{
return;
}
int count = BitConverter.ToInt32(data, 0);//将byte数组中的指定位置的四个字节转为int
if ((startIndex - 4) >= count)//字节数组中剩余字节大于一次消息传送的字节。也就是字节数组中的数据必须要大于要解析的数据长度
{
string s = Encoding.UTF8.GetString(data, 4, count);//从第四位字节以后开始解析,前四位代表数据长度
Console.WriteLine("解析出一条数据"+s);
Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);//将已经解析过的位置之后的数据移动到首位,移动的数据长度是减去已经解析过的数据。相当于覆盖已经解析过的数据,直到当前读取客户端消息的字节少于等于4也就是字节数组中没有数据了。
startIndex -= (count + 4);
}
else
{
break;
}
}
}
}
}
对服务端代码稍作修改using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace JungleWarServer
{
class Program
{
static void Main(string[] args)
{
StartServerAsync();
//StartServerSync();
Console.ReadKey();
}
static void StartServerAsync()
{
//1.创建服务端套接字
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//2.创建服务端ip地址与端口号
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 666);
//3.服务端与ip地址端口号绑定
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(50);//开始监听端口号,处理链接的队列
//Socket clientSocket = serverSocket.Accept();//接收一个客户端链接
serverSocket.BeginAccept(StartClientServerAsync, serverSocket);
}
static Message _message =new Message();
private static void StartClientServerAsync(IAsyncResult ar)
{
Socket serverSocket = ar.AsyncState as Socket;
Socket clientSocket = serverSocket.EndAccept(ar);
//向客户端发送消息
string str = "Hello !客户端。。。";
byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
clientSocket.Send(data);
//异步接收客户端发送的消息,不阻塞主线程
//开始监听客户端数据的传递,当接收到客户端数据时才调用传入的回调函数
clientSocket.BeginReceive(_message.Data, _message.StartIndex, _message.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket);
serverSocket.BeginAccept(StartClientServerAsync, serverSocket);
}
static byte[] dataBuffer = new byte[1024];
private static void ReceiveCallBack(IAsyncResult ar)
{
Socket clientSocket =null;
try
{
clientSocket = ar.AsyncState as Socket;
int count = clientSocket.EndReceive(ar);//一次接受了多少字节
if (count==0)
{
//clientSocket.Close();
return;
}
_message.AddCount(count);//修改存储数据的大小
//string strReceive = Encoding.UTF8.GetString(dataBuffer, 0, count);
//Console.WriteLine(strReceive);
//clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);
_message.ReadMessage();//解析数据
clientSocket.BeginReceive(_message.Data, _message.StartIndex, _message.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (clientSocket!=null)
{
clientSocket.Close();
}
}
}
}
}