HttpServer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Dynamic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading;
  11. using Newtonsoft.Json;
  12. using Newtonsoft.Json.Linq;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. namespace UAS_MES_NEW.PublicMethod
  16. {
  17. public class BindItem
  18. {
  19. [JsonProperty("bindKey")]
  20. public string BindKey { get; set; }
  21. [JsonProperty("bindValue")]
  22. public string BindValue { get; set; }
  23. [JsonProperty("keyType")]
  24. public string KeyType { get; set; }
  25. [JsonProperty("keyValue")]
  26. public string KeyValue { get; set; }
  27. [JsonProperty("hashType")]
  28. public string HashType { get; set; }
  29. [JsonProperty("hashKey")]
  30. public string HashKey { get; set; }
  31. }
  32. public class RootObject
  33. {
  34. [JsonProperty("productModuleType")]
  35. public string ProductModuleType { get; set; }
  36. [JsonProperty("productMainClass")]
  37. public string ProductMainClass { get; set; }
  38. [JsonProperty("productMediumClass")]
  39. public string ProductMediumClass { get; set; }
  40. [JsonProperty("productSubClass")]
  41. public string ProductSubClass { get; set; }
  42. [JsonProperty("task")]
  43. public string Task { get; set; }
  44. [JsonProperty("processType")]
  45. public string ProcessType { get; set; }
  46. [JsonProperty("userId")]
  47. public string UserId { get; set; }
  48. [JsonProperty("bindList")]
  49. public List<BindItem> BindList { get; set; }
  50. }
  51. public class DeviceInfo
  52. {
  53. [JsonProperty("tool_name")]
  54. public string ToolName { get; set; }
  55. [JsonProperty("response")]
  56. public string Response { get; set; }
  57. [JsonProperty("status")]
  58. public int Status { get; set; }
  59. [JsonProperty("product_name")]
  60. public string ProductName { get; set; }
  61. [JsonProperty("product_mode")]
  62. public string ProductMode { get; set; }
  63. [JsonProperty("material_code")]
  64. public string MaterialCode { get; set; }
  65. [JsonProperty("batch_no")]
  66. public string BatchNo { get; set; }
  67. [JsonProperty("production_date")]
  68. public string ProductionDate { get; set; }
  69. [JsonProperty("big_box_no")]
  70. public string BigBoxNo { get; set; }
  71. [JsonProperty("box_no")]
  72. public string BoxNo { get; set; }
  73. [JsonProperty("reserve10")]
  74. public string Reserve10 { get; set; }
  75. [JsonProperty("packing_num")]
  76. public int PackingNum { get; set; }
  77. [JsonProperty("soft_version")]
  78. public string SoftVersion { get; set; }
  79. [JsonProperty("hd_version")]
  80. public string HdVersion { get; set; }
  81. [JsonProperty("voltage")]
  82. public string Voltage { get; set; }
  83. [JsonProperty("power")]
  84. public string Power { get; set; }
  85. [JsonProperty("order_no")]
  86. public string OrderNo { get; set; }
  87. // 使用字典或列表来存储序列化的数组数据
  88. public List<string> DSN { get; set; } = new List<string>();
  89. public List<string> MacStart { get; set; } = new List<string>();
  90. public List<string> MacEnd { get; set; } = new List<string>();
  91. public List<string> DeviceId { get; set; } = new List<string>();
  92. public List<string> DevEnNo { get; set; } = new List<string>();
  93. public List<string> Reserve3 { get; set; } = new List<string>();
  94. public List<string> Reserve4 { get; set; } = new List<string>();
  95. }
  96. public class DeviceInfoConverter : JsonConverter<DeviceInfo>
  97. {
  98. public override DeviceInfo ReadJson(JsonReader reader, System.Type objectType, DeviceInfo existingValue, bool hasExistingValue, JsonSerializer serializer)
  99. {
  100. JObject jsonObject = JObject.Load(reader);
  101. DeviceInfo deviceInfo = new DeviceInfo();
  102. // 解析基本字段
  103. serializer.Populate(jsonObject.CreateReader(), deviceInfo);
  104. // 解析数组字段
  105. ParseArrayFields(jsonObject, deviceInfo);
  106. return deviceInfo;
  107. }
  108. public override void WriteJson(JsonWriter writer, DeviceInfo value, JsonSerializer serializer)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. private void ParseArrayFields(JObject jsonObject, DeviceInfo deviceInfo)
  113. {
  114. int index = 0;
  115. // 解析 d_sn 数组
  116. while (true)
  117. {
  118. string key = $"d_sn{index}";
  119. if (jsonObject.TryGetValue(key, out JToken value))
  120. {
  121. deviceInfo.DSN.Add(value.ToString());
  122. index++;
  123. }
  124. else
  125. {
  126. break;
  127. }
  128. }
  129. // 解析 mac_start 数组
  130. index = 0;
  131. while (true)
  132. {
  133. string key = $"mac_start{index}";
  134. if (jsonObject.TryGetValue(key, out JToken value))
  135. {
  136. deviceInfo.MacStart.Add(value.ToString());
  137. index++;
  138. }
  139. else
  140. {
  141. break;
  142. }
  143. }
  144. // 解析 mac_end 数组
  145. index = 0;
  146. while (true)
  147. {
  148. string key = $"mac_end{index}";
  149. if (jsonObject.TryGetValue(key, out JToken value))
  150. {
  151. deviceInfo.MacEnd.Add(value.ToString());
  152. index++;
  153. }
  154. else
  155. {
  156. break;
  157. }
  158. }
  159. // 解析 dev_en_no 数组
  160. index = 0;
  161. while (true)
  162. {
  163. string key = $"dev_en_no{index}";
  164. if (jsonObject.TryGetValue(key, out JToken value))
  165. {
  166. deviceInfo.DevEnNo.Add(value.ToString());
  167. index++;
  168. }
  169. else
  170. {
  171. break;
  172. }
  173. }
  174. // 解析 reserve3 数组
  175. index = 0;
  176. while (true)
  177. {
  178. string key = $"reserve3_{index}";
  179. if (jsonObject.TryGetValue(key, out JToken value))
  180. {
  181. deviceInfo.Reserve3.Add(value.ToString());
  182. index++;
  183. }
  184. else
  185. {
  186. break;
  187. }
  188. }
  189. // 解析 reserve4 数组
  190. index = 0;
  191. while (true)
  192. {
  193. string key = $"reserve4_{index}";
  194. if (jsonObject.TryGetValue(key, out JToken value))
  195. {
  196. deviceInfo.Reserve4.Add(value.ToString());
  197. index++;
  198. }
  199. else
  200. {
  201. break;
  202. }
  203. }
  204. // 解析 DeviceId 数组
  205. index = 0;
  206. while (true)
  207. {
  208. string key = $"DeviceId{index}";
  209. if (jsonObject.TryGetValue(key, out JToken value))
  210. {
  211. deviceInfo.DeviceId.Add(value.ToString());
  212. index++;
  213. }
  214. else
  215. {
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. public class HttpServer
  222. {
  223. static DataHelper dh = new DataHelper();
  224. public static int SN_SERVICE_PORT = 21610;
  225. public static int MAX_BUF_SIZE = 4096;
  226. static string ComputeSha256Hash(string input)
  227. {
  228. using (SHA256 sha256 = SHA256.Create())
  229. {
  230. byte[] bytes = Encoding.UTF8.GetBytes(input);
  231. byte[] hashBytes = sha256.ComputeHash(bytes);
  232. StringBuilder builder = new StringBuilder();
  233. for (int i = 0; i < hashBytes.Length; i++)
  234. {
  235. builder.Append(hashBytes[i].ToString("x2"));
  236. }
  237. return builder.ToString().ToUpper();
  238. }
  239. }
  240. //获取SN的EN号,彩盒标机身标一个工站一起打印,需要SN单个获取
  241. public static void GetEN(string iSN)
  242. {
  243. DataTable dt = (DataTable)dh.ExecuteSql("select ms_makecode,pr_prefix,pr_prebigxbox,pr_regcode from makeserial left join product on ms_prodcode=pr_code where ms_sncode ='" + iSN + "' order by ms_id desc", "select");
  244. string prefix = "";
  245. string regcode = "";
  246. string makecode = "";
  247. string prebigxbox = "";
  248. if (dt.Rows.Count > 0)
  249. {
  250. prefix = dt.Rows[0]["pr_prefix"].ToString();
  251. regcode = dt.Rows[0]["pr_regcode"].ToString();
  252. makecode = dt.Rows[0]["ms_makecode"].ToString();
  253. prebigxbox = dt.Rows[0]["pr_prebigxbox"].ToString();
  254. }
  255. dynamic obj = new ExpandoObject();
  256. obj.tool_name = "zte";
  257. obj.request = "Storage2";
  258. obj.User = "mes";
  259. obj.password = "258456";
  260. obj.po_sn = "250916001001";
  261. obj.wholeDeviceCode = iSN;
  262. obj.reg_code = regcode;
  263. obj.prefix_en_no = prefix;
  264. obj.prefix_big_box = prebigxbox;
  265. obj.packing_num = 6;
  266. obj.isn = iSN;
  267. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  268. string returnstr = ToServerReq(json);
  269. JObject deviceObj = JObject.Parse(returnstr);
  270. string WholeDeviceCode = deviceObj["WholeDeviceCode"].ToString();
  271. string SN = deviceObj["SN"].ToString();
  272. string dev_en_no = deviceObj["dev_en_no"].ToString();
  273. string mac = deviceObj["mac"].ToString();
  274. string mac_start = deviceObj["mac_start"].ToString();
  275. string mac_end = deviceObj["mac_end"].ToString();
  276. string reg_code = deviceObj["reg_code"].ToString();
  277. string reserve3 = deviceObj["reserve3"].ToString();
  278. string status = deviceObj["status"].ToString();
  279. string device_type = deviceObj["device_type"].ToString();
  280. string en_no = deviceObj["en_no"].ToString();
  281. dh.ExecuteSql("insert into ZTEDATA(ZD_ID,ZD_D_SN,zd_WholeDeviceCode,ZD_DEV_EN_NO,ZD_SN, ZD_MAKECODE, ZD_TYPE, ZD_VALUE,zd_mac,ZD_MAC_START, ZD_MAC_END,ZD_RESERVE3,zd_enno,zd_devicetype,zd_regcode)" +
  282. "values(ZTEDATA_seq.nextval,'" + SN + "','" + WholeDeviceCode + "','" + dev_en_no + "','" + iSN + "','" + makecode + "','彩盒机身标','','" + mac + "','" + mac_start + "','" + mac_end + "','" + reserve3 + "','" + en_no + "','" + device_type + "','" + reg_code + "')", "insert");
  283. Console.WriteLine(json);
  284. }
  285. private string HandleWebException(WebException ex)
  286. {
  287. if (ex.Response != null)
  288. {
  289. using (HttpWebResponse response = (HttpWebResponse)ex.Response)
  290. using (Stream stream = response.GetResponseStream())
  291. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  292. {
  293. string errorResponse = reader.ReadToEnd();
  294. Console.WriteLine($"HTTP错误状态码: {(int)response.StatusCode} {response.StatusCode}");
  295. Console.WriteLine($"错误响应: {errorResponse}");
  296. return errorResponse;
  297. }
  298. }
  299. else
  300. {
  301. string errorMsg = $"网络错误: {ex.Message}";
  302. Console.WriteLine(errorMsg);
  303. return errorMsg;
  304. }
  305. }
  306. //发送外箱号数据,打印前获取
  307. public static void SendBoxData(string iBox)
  308. {
  309. string sn = dh.getFieldDataByCondition("packagedetail", "replace(wm_concat(pd_barcode),',','-')", "pd_outboxcode='" + iBox + "'").ToString();
  310. DataTable dt = (DataTable)dh.ExecuteSql("select pr_custmachinetype,ms_makecode from makeserial left join product on ms_prodcode=pr_code where ms_outboxcode ='" + iBox + "' order by ms_id desc", "select");
  311. string pr_custmachinetype = "";
  312. string makecode = "";
  313. if (dt.Rows.Count > 0)
  314. {
  315. pr_custmachinetype = dt.Rows[0]["pr_custmachinetype"].ToString();
  316. makecode = dt.Rows[0]["ms_makecode"].ToString();
  317. }
  318. var root = new RootObject
  319. {
  320. ProductModuleType = "整机",
  321. ProductMainClass = "固网_CPE",
  322. ProductMediumClass = "CPE",
  323. ProductSubClass = pr_custmachinetype,
  324. Task = makecode,
  325. ProcessType = "生产过程",
  326. UserId = User.UserName,
  327. BindList = new List<BindItem>()
  328. };
  329. root.BindList.Add(new BindItem
  330. {
  331. BindKey = "cartonSn",
  332. BindValue = iBox,
  333. KeyType = "macbox",
  334. KeyValue = sn,
  335. HashType = "SHA256",
  336. HashKey = ComputeSha256Hash(sn)
  337. });
  338. dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
  339. for (int i = 0; i < dt.Rows.Count; i++)
  340. {
  341. root.BindList.Add(new BindItem
  342. {
  343. BindKey = "mac",
  344. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  345. KeyType = "EN",
  346. KeyValue = "4Y12R9300001",
  347. HashType = "NON-HASH",
  348. HashKey = "4Y12R9300001"
  349. });
  350. root.BindList.Add(new BindItem
  351. {
  352. BindKey = "mac",
  353. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  354. KeyType = "电源条码",
  355. KeyValue = "1U1Y2505100013640",
  356. HashType = "NON-HASH",
  357. HashKey = "1U1Y2505100013640"
  358. });
  359. root.BindList.Add(new BindItem
  360. {
  361. BindKey = "mac",
  362. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  363. KeyType = "JY-License",
  364. KeyValue = "Qk1K4rmuowTDl+NEtSIeHx1td6Tzzm0wDrw/AMCvJ6mrXQ5wsSgoxia+T5Cj+uH4xrGUrjGrZvh/WUQN88iDrPO7kuTA0mvo/Ay+AsFJYnn00JQYQkN9ul+a+Qh0EPBqCVa4ikllHcD1Pqq4Eubs6XXNFhKOtK7F7FMotfGzzAVg7RhKVoVRVFcK6ubSVWXW+SnGx5muH+WZPaiHLjt+kxBR8tZJuBl1LQvMWP74JSWKkcFWwcWlARnCJePvRTvKTDd5e7q9rpBpAc7Z79XYQ6Fs4eXM44O6/hA88YW/BK0TZq3AaaMxH8BpJnwZt4lIyuqXispeaB2eieFBR0JwWg==",
  365. HashType = "SHA256",
  366. HashKey = ComputeSha256Hash(sn),
  367. });
  368. }
  369. string json = JsonConvert.SerializeObject(root, Formatting.Indented);
  370. string url = "https://apigwfw.zte.com.cn:2443/mdsdev/api/mds-web-outservice/v1/api/rest/mds/saveDistinctSnBatch";
  371. try
  372. {
  373. // 忽略SSL证书验证
  374. ServicePointManager.ServerCertificateValidationCallback =
  375. (sender, certificate, chain, sslPolicyErrors) => true;
  376. ServicePointManager.Expect100Continue = false;
  377. using (WebClient client = new WebClient())
  378. {
  379. // 设置请求头
  380. client.Headers.Add("appcode", "91678314583d4c2bbf185bc5573ac73d");
  381. client.Headers.Add("Content-Type", "application/json; charset=utf-8");
  382. client.Encoding = Encoding.UTF8;
  383. // 发送POST请求
  384. string response = client.UploadString(url, "POST", json);
  385. Console.WriteLine($"请求成功!");
  386. Console.WriteLine($"响应内容: {response}");
  387. }
  388. }
  389. catch (Exception ex)
  390. {
  391. Console.WriteLine($"发生错误: {ex.Message}");
  392. throw;
  393. }
  394. }
  395. //获取外箱号信息
  396. public static void GetOutBoxInfo(string iBox)
  397. {
  398. dynamic obj = new ExpandoObject();
  399. var expandoDict = obj as IDictionary<string, object>;
  400. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode,pd_makecode,ZD_DEV_EN_NO from packagedetail left join ZTEDATA on zd_makecode=pd_makecode and pd_barcode=zd_sn where pd_outboxcode='" + iBox + "'", "select");
  401. string makecode = "";
  402. for (int i = 0; i < dt.Rows.Count; i++)
  403. {
  404. string enNoKey = $"en_no{i}";
  405. string enNoValue = dt.Rows[i]["ZD_DEV_EN_NO"].ToString();
  406. expandoDict[enNoKey] = enNoValue;
  407. makecode = dt.Rows[i]["pd_makecode"].ToString();
  408. }
  409. obj.request = "Q_wai_xiang";
  410. obj.packing_num = dt.Rows.Count;
  411. obj.User = "mes";
  412. obj.po_sn = "250916001001";
  413. obj.password = "258456";
  414. obj.tool_name = "zte";
  415. obj.noType = 0;
  416. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  417. string returnstr = ToServerReq(json);
  418. var settings = new JsonSerializerSettings
  419. {
  420. Converters = new List<JsonConverter> { new DeviceInfoConverter() }
  421. };
  422. DeviceInfo deviceInfo = JsonConvert.DeserializeObject<DeviceInfo>(returnstr, settings);
  423. for (int i = 0; i < deviceInfo.DSN.Count; i++)
  424. {
  425. Console.WriteLine($"设备 {i}:");
  426. Console.WriteLine($" 序列号: {deviceInfo.DSN[i]}");
  427. Console.WriteLine($" MAC起始: {deviceInfo.MacStart[i]}");
  428. Console.WriteLine($" MAC结束: {deviceInfo.MacEnd[i]}");
  429. Console.WriteLine($" 设备编码: {deviceInfo.DevEnNo[i]}");
  430. Console.WriteLine($" 预留3: {deviceInfo.Reserve3[i]}");
  431. }
  432. //将返回的信息存入数据库
  433. for (int i = 0; i < deviceInfo.DSN.Count; i++)
  434. {
  435. dh.ExecuteSql("insert into ZTEDATA(ZD_ID,ZD_SN, ZD_MAKECODE, ZD_TYPE, ZD_VALUE,ZD_product_mode, ZD_PRODUCTNAME, ZD_MATERIAL_CODE, ZD_BATCH_NO, ZD_SOFT_VERSION, ZD_HD_VERSION, " +
  436. "ZD_VOLTAGE, ZD_POWER, ZD_ORDER_NO, ZD_BIG_BOX_NO, ZD_D_SN, ZD_DEV_EN_NO, ZD_RESERVE3, ZD_DEVICEID, ZD_MAC_START, ZD_MAC_END,zd_production_date, ZD_INDATE)" +
  437. "values(ZTEDATA_seq.nextval,'" + deviceInfo.DSN[i] + "','" + makecode + "','BOX','" + deviceInfo.DSN[i] + "','" + deviceInfo.ProductMode + "','" + deviceInfo.ProductName + "','" + deviceInfo.MaterialCode + "','" + deviceInfo.BatchNo + "','" + deviceInfo.SoftVersion + "','" + deviceInfo.HdVersion + "'," +
  438. "'" + deviceInfo.Voltage + "','" + deviceInfo.Power + "','" + deviceInfo.OrderNo + "','" + deviceInfo.BigBoxNo + "','" + deviceInfo.DSN[i] + "'," +
  439. "'" + deviceInfo.DevEnNo[i] + "', '" + deviceInfo.Reserve3[i] + "','" + deviceInfo.DeviceId[i] + "','" + deviceInfo.MacStart[i] + "','" + deviceInfo.MacEnd[i] + "','" + deviceInfo.ProductionDate + "',sysdate)", "insert");
  440. }
  441. Console.WriteLine(json);
  442. }
  443. //获取栈板信息
  444. public static void GetPalletInfo(string iBox)
  445. {
  446. DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode,pa_makecode from package where pa_mothercode='" + iBox + "'", "select");
  447. dynamic obj = new ExpandoObject();
  448. var expandoDict = obj as IDictionary<string, object>;
  449. string makecode = "";
  450. for (int i = 0; i < dt.Rows.Count; i++)
  451. {
  452. string enNoKey = $"big_box_no{i}";
  453. string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString();
  454. expandoDict[enNoKey] = enNoValue;
  455. makecode = dt.Rows[i]["pa_makecode"].ToString();
  456. }
  457. obj.request = "pallet";
  458. obj.packing_num = dt.Rows.Count;
  459. obj.User = "mes";
  460. obj.po_sn = makecode;
  461. obj.password = "258456";
  462. obj.tool_name = "zte";
  463. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  464. string returnstr = ToServerReq(json);
  465. Console.WriteLine(returnstr);
  466. Console.WriteLine(json);
  467. }
  468. public static readonly uint PROTOCOL_FLAG = 0x4C4F4F54;
  469. public static string ToServerReq(string strJSON)
  470. {
  471. int nRet = 0;
  472. string strServerIP = "192.168.1.160";
  473. string strResp = "";
  474. IPAddress ip = IPAddress.Parse(strServerIP);
  475. Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  476. while (true)
  477. {
  478. try
  479. {
  480. clientSocket.Connect(new IPEndPoint(ip, SN_SERVICE_PORT));
  481. }
  482. catch
  483. {
  484. nRet = -13;
  485. break;
  486. }
  487. try
  488. {
  489. FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(null);
  490. byte[] bytJson = Encoding.UTF8.GetBytes(strJSON); //Encoding.ASCII.GetBytes(strJSON);
  491. stHead.setDataLen(bytJson.Length);
  492. byte[] bytBuf = new byte[FACT_DATA_HEAD.MY_LEN + bytJson.Length];
  493. byte[] bytSrcHead = stHead.getBytes();
  494. Array.Copy(bytSrcHead, 0, bytBuf, 0, bytSrcHead.Length);
  495. Array.Copy(bytJson, 0, bytBuf, FACT_DATA_HEAD.MY_LEN, bytJson.Length);
  496. clientSocket.Send(bytBuf);
  497. }
  498. catch
  499. {
  500. nRet = -12;
  501. break;
  502. }
  503. //判定接口回传的flag和本地传过去的是否一致
  504. byte[] bytRecv = new byte[MAX_BUF_SIZE];
  505. int nRecvSize = 0, nSize = 0;
  506. nRecvSize = clientSocket.Receive(bytRecv, 0, FACT_DATA_HEAD.MY_LEN, SocketFlags.None);
  507. if (nRecvSize > 0)
  508. {
  509. FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(bytRecv);
  510. Console.WriteLine(stHead.getFlag());
  511. Console.WriteLine(FACT_DATA_HEAD.PROTOCOL_FLAG);
  512. if (stHead.getFlag() == FACT_DATA_HEAD.PROTOCOL_FLAG)
  513. {
  514. int nPos = 0;
  515. nSize = stHead.getDatalen();
  516. while (nPos < nSize)
  517. {
  518. nRecvSize = clientSocket.Receive(bytRecv, nPos, nSize - nPos, SocketFlags.None);
  519. if (nRecvSize < 0)
  520. {
  521. nRet = -14;
  522. break;
  523. }
  524. nPos += nRecvSize;
  525. }
  526. strResp = Encoding.Default.GetString(bytRecv, 0, nSize);
  527. }
  528. else nRet = -11;
  529. }
  530. else nRet = -10;
  531. break;
  532. }
  533. if (nRet > -13) clientSocket.Shutdown(SocketShutdown.Both);
  534. clientSocket.Close();
  535. return strResp;
  536. }
  537. }
  538. }