C# BeginReceive粘包处理,网上没有一个能讲明白的,到现在我还是不明白你们想法

1 篇文章 0 订阅

假如客户端发送消息:长度(int 四个字节) + 包体(10个字节)

服务器接收:

byte[] bytes = new byte[1000];

接收:

this.socket.BeginReceive(headerByte, 0, bytes .Length, SocketFlags.None, CallAsyncCallback, this.socket); 

回调:

private void CallAsyncCallback(IAsyncResult ar)
{
       this.socket = (Socket)ar.AsyncState;
       int count = this.socket.EndReceive(ar); //当收到数据后回调,这个count一定就是四个字节,如果网络非常慢,它只收到一个字节怎么办?

       if(count!=0)
        {
                int header = BitConverter.ToInt32(bytes);

                其实 我很想问你们一下,你们确定,这里能马上收到四个字节?如果count只收到了1个字节,那能转成int吗?能取长度吗?
       }

网上大神能帮我解决一下这个问题吗?,头部占四个字节,也不能保证一次性就能收到四个字节呀,网上没有一个地方能说明白的,

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是一个简单的 C# Socket 通讯解决粘包的示例: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketServer { private Socket listener; private const int BUFFER_SIZE = 1024; private byte[] buffer = new byte[BUFFER_SIZE]; public void Start() { listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 8080)); listener.Listen(10); Console.WriteLine("Server started on port 8080."); while (true) { Socket client = listener.Accept(); Console.WriteLine("Client connected: " + client.RemoteEndPoint.ToString()); // Start receiving data from client client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client); } } private void OnReceive(IAsyncResult ar) { Socket client = (Socket)ar.AsyncState; int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // Convert received bytes to string string receivedData = Encoding.UTF8.GetString(buffer, 0, bytesRead); // Process received data Console.WriteLine("Received data: " + receivedData); // Continue receiving data from client client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client); } else { // Client has disconnected Console.WriteLine("Client disconnected: " + client.RemoteEndPoint.ToString()); client.Close(); } } } public class SocketClient { private Socket client; private const int BUFFER_SIZE = 1024; private byte[] buffer = new byte[BUFFER_SIZE]; public void Connect() { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.BeginConnect(new IPEndPoint(IPAddress.Loopback, 8080), new AsyncCallback(OnConnect), null); } private void OnConnect(IAsyncResult ar) { Console.WriteLine("Connected to server."); client.EndConnect(ar); // Start receiving data from server client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client); } private void OnReceive(IAsyncResult ar) { Socket client = (Socket)ar.AsyncState; int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // Convert received bytes to string string receivedData = Encoding.UTF8.GetString(buffer, 0, bytesRead); // Process received data Console.WriteLine("Received data: " + receivedData); // Continue receiving data from server client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client); } else { // Server has disconnected Console.WriteLine("Server disconnected."); client.Close(); } } public void Send(string data) { byte[] sendData = Encoding.UTF8.GetBytes(data); client.BeginSend(sendData, 0, sendData.Length, 0, new AsyncCallback(OnSend), null); } private void OnSend(IAsyncResult ar) { client.EndSend(ar); } } class Program { static void Main(string[] args) { // Start server SocketServer server = new SocketServer(); server.Start(); // Connect client to server SocketClient client = new SocketClient(); client.Connect(); // Send data to server client.Send("Hello, server!"); Console.ReadLine(); } } ``` 这个示例中,我们使用了异步的 BeginReceive 和 BeginSend 方法来在接收和发送数据时不阻塞主线程。在接收数据时,我们先接收到一部分数据,然后判断是否收到了完整的数据,如果没有就继续接收。在发送数据时,我们将要发送的数据转换成字节数组后,使用 BeginSend 方法发送,然后在回调函数中调用 EndSend 方法结束发送。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值