Sockets are often used in C# to receive and send data, and it is also very convenient. Sometimes we will request data from the server. If the returned data is large, such as more than 10M or even more, how should we receive data? Let’s analyze and explain this problem with an example used in the project. Let’s take a look at the following code first?
/// <summary> /// Return to camera information /// </summary> private void RcvCameraInfos(object obj) { string sourceIp = ["SourceIP"].ToString(); string sourcePort = ["SourcePort"].ToString(); Socket mysocket = new Socket(, , ); IPEndPoint ipEndPoint = new IPEndPoint((sourceIp), (sourcePort)); (ipEndPoint); DateTime start = ; string s = "<?xml version=\"1.0\" ?>"; s += "<info name=\"getcameracodematrixtable\"/>\0"; byte[] buffer = ("GB2312").GetBytes(s); (buffer); Action<string> OnCamraInfoRcvCompleted = obj as Action<string>; int recvsize = 0; int dataSize = 2048 * 1000; int ret = 0; byte[] datas = new byte[dataSize]; SortedList<string, string[]> cameraDictionnary = new SortedList<string, string[]>(); string xmlString = ""; while (recvsize < dataSize) { ret = (datas, recvsize, dataSize - recvsize, ); if (ret <= 0) { break; } recvsize += ret; if (datas[recvsize - 1] == 0) { break; } if (recvsize >= dataSize) { byte[] buff2 = new byte[dataSize + 1024]; (buff2,0); datas = buff2; dataSize += 1024; } } xmlString = ("GB2312").GetString(datas, 0, recvsize - 1); DateTime end = ; TimeSpan span = end - start; ("Total time spent:"+()+"Second"); this._videoSourceXmlString = xmlString; if (cameraDictionnary != null) { if (OnCamraInfoRcvCompleted != null) OnCamraInfoRcvCompleted(xmlString); } }
The core of the above code is in the While loop. We first receive the BufferSize. Here we define the size of 2048*1000 bytes. ret = (datas, recvsize, dataSize - recvsize, ); receive through the synchronization method of Socket. datas is the Byte array of data we receive, recvsize is the current received byte starting point (offset), and dataSize-recvsize is the buffer size of the received. In this While loop, as long as recvsize < dataSize, it will keep receiving data. Of course, if the data volume is large, there will always be a moment recvsize >= dataSize, why do I need to increase dataSize at this time? Here we dynamically increase the size of 1KB. Through such a boundary control, we can accurately obtain all the data...
Of course, the disadvantage of receiving data in this way is that it is synchronous. If too much data is received, the time may take too long. Especially when updating the UI interface, asynchronous non-blocking Sockets need to be used to receive data, or open a thread alone to receive data, and then update to the UI through the method, otherwise the interface will be stuck. This requires us to carefully analyze...
Another thing is that when we receive data like this and store it in XML file, the XML data may be connected together and cannot be actively wrapped. This can be solved in the following way.
private void SaveCurrentInfoToXML(string recevInfo) { string filePath=+""; XmlDocument xd = new XmlDocument(); if ((filePath)) { (filePath); } else { XmlDeclaration xmlDec; XmlElement xmlEle; xmlDec = ("1.0","UTF-8",null); (xmlDec); xmlEle = ("Info"); (xmlEle); } (recevInfo); XmlTextWriter xtw = new XmlTextWriter(filePath, Encoding.UTF8); = ; (xtw); }
Here, after receiving the XML data, when we write it to the XML file through the XmlTextWriter, we need to set the indentation format: = ; In this way, the data will be complete and in alignment when writing the data.
The above is the detailed content of the example of C# reading a large amount of data through Socket. For more information about C# reading a large amount of data through Socket, please pay attention to my other related articles!