对我而言。粘包是由于短时间传送的数据多且小,socket会进行优化,使其一部分数据合并发送。而分包是由于传送的数据大,消耗资源大,且丢失重新发送又麻烦,所以系统自动会将数据进行拆分发送。
不过,还是举例说明好点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint point = new IPEndPoint(ip, 88);
client.Connect(point);
//接收
byte[] buffer = new byte[1024];
int count = client.Receive(buffer);
Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, count));
//发送
/* while (true)
{
string s = Console.ReadLine();
if (s=="c")
{
//主动关闭
client.Close();
return;
}
client.Send(Encoding.UTF8.GetBytes(s));
}*/
for(int i = 0; i < 100; i++)
{
client.Send(Encoding.UTF8.GetBytes(i.ToString()));
}
Console.ReadKey();
client.Close();
}
}
}
在客户端传送100个数据给服务器。按理说,服务端应该分批接收100个数据,然而并没有。
系统将后面一部分数据合并了。
而分包呢
string i = "会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和" +
"会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和会计师大后方士大夫实打实附上附件是覅u是覅是isfi覅是uiui是hi和毒素还是粉色粉色ui和i萨芬和";
client.Send(Encoding.UTF8.GetBytes(i.ToString()));
请无视内容,随便打的。正常来说服务端应该接收一次数据。然而
服务端接收了两次。
对于这个问题,我采取一种方法。首先将要传送的数据的长度记录下来,并将转化为字节。而这个过程我用的BitConverter.ToInt32函数,因为不管数据长度多少,都能转化为4个字节,使其结构固定,最后将长度字节与信息结合传送,这样的话,对方就可以知道该信息要读取多少。
废话少说,直接上代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Message
{
public static byte[] GetByte(string str)
{
byte[] str_byte = Encoding.UTF8.GetBytes(str);
int count = str_byte.Length;
byte[] start_byte = BitConverter.GetBytes(count);
byte[] new_byte = start_byte.Concat(str_byte).ToArray();
return new_byte;
}
}
}
此脚本位于客户端,将要传送的数据序列化。同时用粘包例子进行修改。
for (int i = 0; i < 100; i++)
{
client.Send(Message.GetByte(i.ToString ()));
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
2.然后这个脚本位于服务端,将传来的数据进行解析。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sever
{
class Message
{
private byte[] date = new byte[1024];
private int startindex = 0;
public void add_byte(int count)
{
startindex += count;
}
public byte[] set_byte
{
get { return date; }
}
public int Startindex
{
get { return startindex; }
}
public int remove_byte
{
get { return date.Length - startindex; }
}
public void receivedata()
{
while (true)
{
if (startindex <= 4) return;
int count = BitConverter.ToInt32(date, 0);//表示信息长度
if(startindex - 4 > count)
{
string s = Encoding.UTF8.GetString(date, 4, count);
Console.WriteLine("接收的数据:" + s);
Array.Copy(date, count + 4, date, 0, startindex - 4 - count);
startindex -= (count + 4);
}
else
{
break;
}
}
}
}
}
然后对服务端的脚本也修改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Sever
{
class Program
{
static byte[] buffer = new byte[1024];
static void Main(string[] args)
{
Socket severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint severpoint = new IPEndPoint(ip,88);
severSocket.Bind(severpoint);
severSocket.Listen(10);
severSocket.BeginAccept(accept,severSocket);
Console.ReadKey();
}
static Message mesg = new Message();
static void accept(IAsyncResult ar)
{
Socket severSocket = ar.AsyncState as Socket;
Socket client = severSocket.EndAccept(ar);
//发数据
string str = "hello";
client.Send(Encoding.UTF8.GetBytes(str));
//接收
client.BeginReceive (mesg.set_byte, mesg.Startindex, mesg.remove_byte , SocketFlags.None, receive, client);
severSocket.BeginAccept(accept, severSocket);
}
static void receive(IAsyncResult ar)
{
Socket client = null;
try {
client = ar.AsyncState as Socket;
int count = client.EndReceive(ar);
if (count == 0)
{
//正常关闭
client.Close();return;
}
mesg.add_byte(count);
/*string str = Encoding.UTF8.GetString(buffer, 0, count);
Console.WriteLine("接收的数据:"+str);*/
mesg.receivedata();
client.BeginReceive(mesg.set_byte, mesg.Startindex, mesg.remove_byte, SocketFlags.None, receive, client);
}
catch(Exception e)//非正常关闭
{
Console.WriteLine(e);
if (client != null)
{
client.Close();
}
}
}
}
}
最后的结果