当前位置:网站首页>C three ways to realize socket data reception
C three ways to realize socket data reception
2022-07-06 22:42:00 【Phil Arist】
Stream.Read Method
When overridden in a derived class , Reads a sequence of bytes from the current stream , And raise the position in this stream by the number of bytes read .
grammar :
public abstract int Read(byte[] buffer, int offset, int count)
Parameters :
buffer: Byte array . When this method returns , The buffer contains the specified character array , Of the array offset and (offset + count -1) The values between are replaced by bytes read from the current source .
offset: buffer Zero based byte offset in , From here, the data read from the current stream is stored .
count: The maximum number of bytes to read from the current stream .
Return value :
The total number of bytes read into the buffer . If the number of bytes currently available is not as many as the number of bytes requested , The total number of bytes may be less than the number of bytes requested , Or if you have reached the end of the stream , It's zero (0).
remarks :
The implementation of this method reads the most from the current stream count Bytes , And store them from offset At the beginning buffer in . The current position in the stream raises the number of bytes read ; however , If there is an anomaly , The current position in the stream remains unchanged . The implementation returns the number of bytes read . Only if the position is currently at the end of the stream , The return value is zero . If there is no data available , The implementation will block until at least one byte of data is readable . Only if there is no more data in the stream , And no more data is needed ( Such as closed socket or end of file ) when ,Read To return to 0. Even if the end of the stream has not been reached , The implementation is still free to return less than the requested bytes .
Previously, the following methods were generally used for data reception :
int recv;// Define the receive data length variable
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
// Interface monitored by the receiver ,ip It can also be used. IPAddress.Any
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Initialize a Socket object
socket.Bind(ipEnd);// Bind socket to a IP Address and a port (bind());
socket.Listen(10);
while (true)
{
byte[] data = new byte[1024];// Yes data Zero clearing
Socket clientSocket = socket.Accept(); // Once the connection is accepted , Create a client
recv = clientSocket.Receive(data);
if (recv == 0) // If the received data length is less than 0, The exit
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";
});
}
I didn't find any problems when I used it before , But today, when testing the data reception of metal door, I found that data will be lost , The metal door gives me data every ten seconds , Use the above one almost 60 Seconds to receive a set of data , For the above problems , The following changes have been made :
Put the data reception into while (true), Data reception is normal .
The following three methods are used to realize the normal reception of data , The code is as follows :
test :
Task.Run(() => {}); This can be removed ;
边栏推荐
- 枚举与#define 宏的区别
- How to use flexible arrays?
- OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
- Aardio - does not declare the method of directly passing float values
- Build op-tee development environment based on qemuv8
- 项目复盘模板
- 【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
- case 关键字后面的值有什么要求吗?
- Web APIs DOM time object
- MySQL----初识MySQL
猜你喜欢
Installation and use of labelimg
【编译原理】做了一半的LR(0)分析器
在IPv6中 链路本地地址的优势
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
AdaViT——自适应选择计算结构的动态网络
【LeetCode】19、 删除链表的倒数第 N 个结点
Aardio - 不声明直接传float数值的方法
Aardio - construct a multi button component with customplus library +plus
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
pytorch_ Yolox pruning [with code]
随机推荐
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
Senior soft test (Information System Project Manager) high frequency test site: project quality management
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
BasicVSR_PlusPlus-master测试视频、图片
Windows Auzre 微软的云计算产品的后台操作界面
signed、unsigned关键字
sizeof关键字
动作捕捉用于蛇运动分析及蛇形机器人开发
go多样化定时任务通用实现与封装
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
OpenNMS separation database
UDP programming
Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries
Aardio - integrate variable values into a string of text through variable names
【雅思口语】安娜口语学习记录part1
MySQL----初识MySQL
Volatile keyword
Pit encountered by handwritten ABA
[IELTS speaking] Anna's oral learning record part1
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型