C# 通過Socket讀取大量數(shù)據(jù)的示例
在C#中經(jīng)常會用到Socket去接收和發(fā)送數(shù)據(jù),而且也是非常方便的,有時候我們會向服務(wù)端去請求數(shù)據(jù),如果返回的數(shù)據(jù)量很大,比如超過10M甚至是更多,那么該怎樣去接收數(shù)據(jù)呢?下面以一個在項(xiàng)目中用到的實(shí)例去分析和解釋這個問題,先看看下面的這段代碼?
/// <summary> /// 返回?cái)z像頭信息 /// </summary> private void RcvCameraInfos(object obj) { string sourceIp = System.Configuration.ConfigurationSettings.AppSettings["SourceIP"].ToString(); string sourcePort = System.Configuration.ConfigurationSettings.AppSettings["SourcePort"].ToString(); Socket mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(sourceIp), int.Parse(sourcePort)); mysocket.Connect(ipEndPoint); DateTime start = DateTime.Now; string s = "<?xml version=\"1.0\" ?>"; s += "<info name=\"getcameracodematrixtable\"/>\0"; byte[] buffer = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(s); mysocket.Send(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 = mysocket.Receive(datas, recvsize, dataSize - recvsize, SocketFlags.None); if (ret <= 0) { break; } recvsize += ret; if (datas[recvsize - 1] == 0) { break; } if (recvsize >= dataSize) { byte[] buff2 = new byte[dataSize + 1024]; datas.CopyTo(buff2,0); datas = buff2; dataSize += 1024; } } xmlString = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetString(datas, 0, recvsize - 1); DateTime end = DateTime.Now; TimeSpan span = end - start; System.Windows.MessageBox.Show("總共花費(fèi)時間:"+span.TotalSeconds.ToString()+"秒"); this._videoSourceXmlString = xmlString; if (cameraDictionnary != null) { if (OnCamraInfoRcvCompleted != null) OnCamraInfoRcvCompleted(xmlString); } }
上述這段代碼的核心是在While循環(huán)里面,我們首先接收的BufferSize,這里我們定義2048*1000個字節(jié)的大小,ret = mysocket.Receive(datas, recvsize, dataSize - recvsize, SocketFlags.None);通過Socket的這個同步方法來進(jìn)行接收,datas是我們接收數(shù)據(jù)的Byte數(shù)組,recvsize是當(dāng)前接收的字節(jié)起點(diǎn)(offset),dataSize-recvsize是接收的緩沖區(qū)大小,在這個While循環(huán)里面,只要是recvsize < dataSize就會不停的去接收數(shù)據(jù),當(dāng)然如果數(shù)據(jù)量很大的話總有一個時刻recvsize >= dataSize,這個時候我怎就需要增加dataSize了,這里我們?nèi)討B(tài)增加1KB的大小,通過這樣一個邊界控制我們就能夠去準(zhǔn)確獲取所有的數(shù)據(jù)了......
當(dāng)然這種方式接收數(shù)據(jù)的缺點(diǎn)就是通過同步的方式,如果接收的數(shù)據(jù)太多的話,那么花費(fèi)的時間可能就過長了,特別是在更新UI界面的時候,需要采用異步非阻塞的Socket來接收數(shù)據(jù)了,或者單獨(dú)來開一個線程來進(jìn)行數(shù)據(jù)接收,然后通過Application.Current.Dispatcher.BeginInvoke的方式來更新到UI上面,不然界面就會卡死,這個需要我們?nèi)フJ(rèn)真分析......
另外一個當(dāng)我們這樣接收數(shù)據(jù)然后存入XML文件時,可能XML數(shù)據(jù)都是連在一起并不能主動換行,這個可以通過下面的方式來解決。
private void SaveCurrentInfoToXML(string recevInfo) { string filePath=System.AppDomain.CurrentDomain.BaseDirectory+"CameraInfo.xml"; XmlDocument xd = new XmlDocument(); if (File.Exists(filePath)) { xd.Load(filePath); } else { XmlDeclaration xmlDec; XmlElement xmlEle; xmlDec = xd.CreateXmlDeclaration("1.0","UTF-8",null); xd.AppendChild(xmlDec); xmlEle = xd.CreateElement("Info"); xd.AppendChild(xmlEle); } xd.LoadXml(recevInfo); XmlTextWriter xtw = new XmlTextWriter(filePath, Encoding.UTF8); xtw.Formatting = Formatting.Indented; xd.Save(xtw); }
這里我們接收到XML數(shù)據(jù)后,通過XmlTextWriter寫入到XML文件中時,需要設(shè)置縮進(jìn)格式:xtw.Formatting = Formatting.Indented;這樣寫入數(shù)據(jù)時數(shù)據(jù)就會完整,并且處于對齊方式。
以上就是C# 通過Socket讀取大量數(shù)據(jù)的示例的詳細(xì)內(nèi)容,更多關(guān)于C# 通過Socket讀取數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Asp.Net中避免重復(fù)提交和彈出提示框的實(shí)例代碼
本文分為前臺和后臺代碼實(shí)現(xiàn)避免重復(fù)提交和彈出提示框效果,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的的朋友參考下2017-02-02C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法
這篇文章主要介紹了C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法,可實(shí)現(xiàn)用回車鍵代替點(diǎn)擊按鈕的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09C#檢測遠(yuǎn)程計(jì)算機(jī)端口是否打開的方法
這篇文章主要介紹了C#檢測遠(yuǎn)程計(jì)算機(jī)端口是否打開的方法,實(shí)例分析了C#實(shí)現(xiàn)檢測遠(yuǎn)程端口開啟的技巧,需要的朋友可以參考下2015-03-03C#保存listbox中數(shù)據(jù)到文本文件的方法
這篇文章主要介紹了C#保存listbox中數(shù)據(jù)到文本文件的方法,涉及C#操作listbox數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04