当前位置:网站首页>C# 三种方式实现Socket数据接收
C# 三种方式实现Socket数据接收
2022-07-06 14:48:00 【Phil Arist】
Stream.Read 方法
当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。
语法:
public abstract int Read(byte[] buffer, int offset, int count)
参数:
buffer: 字节数组。此方法返回时,该缓冲区包含指定的字符数组,该数组的 offset 和 (offset + count -1) 之间的值由从当前源中读取的字节替换。
offset: buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。
count: 要从当前流中最多读取的字节数。
返回值:
读入缓冲区中的总字节数。如果当前可用的字节数没有请求的字节数那么多,则总字节数可能小于请求的字节数,或者如果已到达流的末尾,则为零 (0)。
备注:
此方法的实现从当前流中读取最多的 count 个字节,并将它们存储在从 offset 开始的 buffer 中。流中的当前位置提升已读取的字节数;但是,如果出现异常,流中的当前位置保持不变。实现返回已读取的字节数。仅当位置当前位于流的末尾时,返回值才为零。如果没有任何可用的数据,该实现将一直阻塞到至少有一个字节的数据可读为止。仅当流中不再有其他的数据,而且也不再需要更多的数据(如已关闭的套接字或文件尾)时,Read 才返回 0。即使尚未到达流的末尾,实现仍可以随意返回少于所请求的字节。
之前一般采用如下方式进行数据接收:
int recv;//定义接收数据长度变量
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
//接收端所监听的接口,ip也可以用IPAddress.Any
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//初始化一个Socket对象
socket.Bind(ipEnd);//绑定套接字到一个IP地址和一个端口上(bind());
socket.Listen(10);
while (true)
{
byte[] data = new byte[1024];//对data清零
Socket clientSocket = socket.Accept(); //一旦接受连接,创建一个客户端
recv = clientSocket.Receive(data);
if (recv == 0) //如果收到的数据长度小于0,则退出
break;
string stringData = "0x" + BitConverter.ToString(data).Replace("-", " 0x").ToLower();
this.Invoke((EventHandler)delegate
{
richTextBox1.Text += DateTime.Now.ToString("yy-MM-dd hh:mm:ss") + stringData + "\n";
});
}之前用的时候没发现什么问题,但是今天在测试金属门数据接收的时候发现会丢数据,金属门每隔十秒给我一次数据,用上面这个差不多60秒才能收到一组数据,针对以上问题,做了如下修改:
将数据接收放到 while (true),数据接收正常。
以下分别采用三种方式实现了数据的正常接收,代码如下:

测试:
Task.Run(() => {}); 这个可以去掉;
边栏推荐
- case 关键字后面的值有什么要求吗?
- Applet system update prompt, and force the applet to restart and use the new version
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- [sciter]: encapsulate the notification bar component based on sciter
- C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
- Shortcut keys in the terminal
- 插入排序与希尔排序
- Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
- 3DMax指定面贴图
- 软考高级(信息系统项目管理师)高频考点:项目质量管理
猜你喜欢

Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem

Leetcode question brushing (XI) -- sequential questions brushing 51 to 55

新手程序员该不该背代码?

2022年6月国产数据库大事记-墨天轮

Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode

小常识:保险中的“保全”是什么?

网络基础入门理解
![pytorch_ Yolox pruning [with code]](/img/98/31d6258635ce48ac53819d0ca12d1d.jpg)
pytorch_ Yolox pruning [with code]

【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真

Installation and use of labelimg
随机推荐
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
i. Mx6ull build boa server details and some of the problems encountered
MySQL数据库基本操作-DML
Senior soft test (Information System Project Manager) high frequency test site: project quality management
小程序系统更新提示,并强制小程序重启并使用新版本
3DMax指定面贴图
(十八)LCD1602实验
空结构体多大?
Inno setup packaging and signing Guide
二叉(搜索)树的最近公共祖先 ●●
【雅思口语】安娜口语学习记录part1
中国VOCs催化剂行业研究与投资战略报告(2022版)
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Chapter 4: talk about class loader again
Attack and defense world miscall
LeetCode刷题(十一)——顺序刷题51至55
NetXpert XG2帮您解决“布线安装与维护”难题
Daily question 1: force deduction: 225: realize stack with queue
解决项目跨域问题
Inno Setup 打包及签名指南