当前位置:网站首页>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(() => {}); 这个可以去掉;
边栏推荐
- Shortcut keys in the terminal
- QT | UDP broadcast communication, simple use case
- Management background --4, delete classification
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- 中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
- Management background --5, sub classification
- Dealing with the crash of QT quick project in offscreen mode
- Management background --1 Create classification
- [linear algebra] determinant of order 1.3 n
- Applet system update prompt, and force the applet to restart and use the new version
猜你喜欢

C#实现水晶报表绑定数据并实现打印4-条形码

PVL EDI project case
![[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式

剑指offer刷题记录1

软考高级(信息系统项目管理师)高频考点:项目质量管理

labelimg的安装与使用

Attack and defense world ditf Misc

MySQL----初识MySQL

数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
随机推荐
Force deduction question 500, keyboard line, JS implementation
UDP编程
How do I write Flask's excellent debug log message to a file in production?
3DMax指定面贴图
【雅思口语】安娜口语学习记录part1
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
【sdx62】WCN685X将bdwlan.bin和bdwlan.txt相互转化操作方法
Classification, function and usage of MySQL constraints
GD32F4XX串口接收中断和闲时中断配置
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
Applet system update prompt, and force the applet to restart and use the new version
case 关键字后面的值有什么要求吗?
手写ABA遇到的坑
CCNA Cisco network EIGRP protocol
Spatial domain and frequency domain image compression of images
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
HDR image reconstruction from a single exposure using deep CNNs阅读札记
Management background --2 Classification list
小程序系统更新提示,并强制小程序重启并使用新版本
Build op-tee development environment based on qemuv8