当前位置:网站首页>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 ;
边栏推荐
- The SQL response is slow. What are your troubleshooting ideas?
- 云原生技术--- 容器知识点
- Uniapp setting background image effect demo (sorting)
- View
- QT信号和槽
- Traversal of a tree in first order, middle order, and then order
- QT signal and slot
- ThreadLocal详解
- case 关键字后面的值有什么要求吗?
- BasicVSR_ Plusplus master test videos and pictures
猜你喜欢
(十八)LCD1602实验
树的先序中序后序遍历
Should novice programmers memorize code?
View
Mysql 身份认证绕过漏洞(CVE-2012-2122)
LeetCode 练习——剑指 Offer 26. 树的子结构
Config:invalid signature solution and troubleshooting details
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
随机推荐
Puppeteer连接已有Chrome浏览器
volatile关键字
How do I write Flask's excellent debug log message to a file in production?
树的先序中序后序遍历
Signed and unsigned keywords
Windows auzre background operation interface of Microsoft's cloud computing products
Extern keyword
视图(view)
What are the specific steps and schedule of IELTS speaking?
That's why you can't understand recursion
case 关键字后面的值有什么要求吗?
Export MySQL table data in pure mode
return 关键字
Detailed explanation of ThreadLocal
MySQL ---- first acquaintance with MySQL
Web APIs DOM 时间对象
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
SQL server generates auto increment sequence number
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
Self made j-flash burning tool -- QT calls jlinkarm DLL mode