当前位置:网站首页>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 ;
边栏推荐
- MySQL authentication bypass vulnerability (cve-2012-2122)
- signed、unsigned关键字
- CSDN 上传图片取消自动加水印的方法
- Installation and use of labelimg
- Gd32f4xx serial port receive interrupt and idle interrupt configuration
- memcached
- 2022-07-05 stonedb sub query processing parsing time analysis
- 柔性数组到底如何使用呢?
- Const keyword
- 专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
猜你喜欢

Aardio - 通过变量名将变量值整合到一串文本中

Pit encountered by handwritten ABA

Aardio - integrate variable values into a string of text through variable names

Signed and unsigned keywords

Custom swap function

NPDP认证|产品经理如何跨职能/跨团队沟通?

Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries

That's why you can't understand recursion

树的先序中序后序遍历

2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
随机推荐
Improving Multimodal Accuracy Through Modality Pre-training and Attention
做国外LEAD2022年下半年几点建议
case 关键字后面的值有什么要求吗?
2014阿里巴巴web前实习生项目分析(1)
Extern keyword
Aardio - construct a multi button component with customplus library +plus
How to use flexible arrays?
【无标题】
Detailed explanation of ThreadLocal
Machine test question 1
ThreadLocal详解
Installation and use of labelimg
Aardio - integrate variable values into a string of text through variable names
Adavit -- dynamic network with adaptive selection of computing structure
UVa 11732 – strcmp() Anyone?
Is there any requirement for the value after the case keyword?
Matlab tips (27) grey prediction
cuda 探索
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
Aardio - 不声明直接传float数值的方法