当前位置:网站首页>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 ;
边栏推荐
- Typescript get function parameter type
- UDP编程
- 软考高级(信息系统项目管理师)高频考点:项目质量管理
- 如何实现文字动画效果
- 2022-07-05 use TPCC to conduct sub query test on stonedb
- 自定义 swap 函数
- Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)
- Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
- 欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
- ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
猜你喜欢

Clip +json parsing converts the sound in the video into text

Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"

剑指offer刷题记录1

Mise en place d'un environnement de développement OP - tee basé sur qemuv8

DR-Net: dual-rotation network with feature map enhancement for medical image segmentation

Traversal of a tree in first order, middle order, and then order

Export MySQL table data in pure mode

关于声子和热输运计算中BORN电荷和non-analytic修正的问题

C# 三种方式实现Socket数据接收

pytorch_YOLOX剪枝【附代码】
随机推荐
Puppeter connects to the existing Chrome browser
Clip +json parsing converts the sound in the video into text
Installation and use of labelimg
How to confirm the storage mode of the current system by program?
MySQL数据库基本操作-DML
What are the specific steps and schedule of IELTS speaking?
Web APIs DOM 时间对象
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
Custom swap function
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
Export MySQL table data in pure mode
What are the interface tests? What are the general test points?
Pit encountered by handwritten ABA
Is there any requirement for the value after the case keyword?
Detailed explanation of ThreadLocal
Matlab tips (27) grey prediction
memcached
Aardio - 不声明直接传float数值的方法
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
CSDN 上传图片取消自动加水印的方法