如何在C#中使用websocket时将大数据拆分成块? [英] How to split a large data into chunks while using websocket in C#?

查看:139
本文介绍了如何在C#中使用websocket时将大数据拆分成块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#中使用websocket时将大数据的缓冲区分成块。



我试图通过网络套接字发送,但我不是获取整个数据。

byte [] buffer = encoding.GetBytes(stringtoSend);我想把它从客户端分成服务器端。



我尝试过:



核心代码如下:

I want to split a buffer of a large data into chunks while using websocket in c#.

I am trying to send over a web socket but I am not getting the entire data.
byte[] buffer = encoding.GetBytes(stringtoSend); I want to split it as chunks to the server side from client.

What I have tried:

Core Code is as below:

byte[] buffer = encoding.GetBytes(stringtoSend);

                   bool EndofMessageStatus = false;



                 await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, EndofMessageStatus, CancellationToken.None);

推荐答案

如果在接收端遇到问题,以块的形式发送数据将无法解决问题。它会使你的代码更复杂,并可能引入新的问题。



尝试解决接收问题。



我只能在这里猜测:

您正在发送字符串,并且在接收时检测长度存在问题。然后首先发送传输数据的长度,然后发送有效负载。所以接收者知道他必须接收多少数据。定义这样的称为协议并且对于套接字通信是通用的。
If you have problems at the receiving side, sending data in chunks will not solve them. It will make your code more complex and might introduce new problems.

Try to solve the receiving problems instead.

I can only guess here:
You are sending a string and have problems to detect the length when receiving. Then send the length of transferred data first followed by the payload. So the receiver knows how many data he has to receive. Defining such is called a "protocol" and common for socket communications.


这篇关于如何在C#中使用websocket时将大数据拆分成块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆