|
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|
|
using System.Data;
|
|
|
using System.Dynamic;
|
|
|
using System.IO;
|
|
|
+using System.Net;
|
|
|
using System.Net.Sockets;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
@@ -14,6 +15,7 @@ using UAS_MES_NEW.Entity;
|
|
|
namespace UAS_MES_NEW.PublicMethod
|
|
|
{
|
|
|
|
|
|
+
|
|
|
public class BindItem
|
|
|
{
|
|
|
[JsonProperty("bindKey")]
|
|
|
@@ -92,6 +94,8 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
class HttpServer
|
|
|
{
|
|
|
static DataHelper dh = new DataHelper();
|
|
|
+ public static int SN_SERVICE_PORT = 8234;
|
|
|
+ public static int MAX_BUF_SIZE = 4096;
|
|
|
static string ComputeSha256Hash(string input)
|
|
|
{
|
|
|
using (SHA256 sha256 = SHA256.Create())
|
|
|
@@ -150,7 +154,7 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
string device_type = deviceObj["device_type"].ToString();
|
|
|
string en_no = deviceObj["en_no"].ToString();
|
|
|
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)" +
|
|
|
- "values(ZTEDATA_seq.nextval,'" + SN + "','" + WholeDeviceCode + "','" + dev_en_no + "','" + iSN + "','" + makecode + "','彩盒机身标','" + mac + "','" + mac_start + "','" + mac_end + "','" + reserve3 + "','" + en_no + "','" + device_type + "','" + reg_code + "')", "insert");
|
|
|
+ "values(ZTEDATA_seq.nextval,'" + SN + "','" + WholeDeviceCode + "','" + dev_en_no + "','" + iSN + "','" + makecode + "','彩盒机身标','','" + mac + "','" + mac_start + "','" + mac_end + "','" + reserve3 + "','" + en_no + "','" + device_type + "','" + reg_code + "')", "insert");
|
|
|
Console.WriteLine(json);
|
|
|
}
|
|
|
|
|
|
@@ -290,33 +294,51 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
//发送数据
|
|
|
public static string SendData(string json)
|
|
|
{
|
|
|
- string serverIP = "192.168.1.160";
|
|
|
- int port = 21610;
|
|
|
+ string serverIP = "10.1.162.175";
|
|
|
+ int port = 8234;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- // 创建TCP客户端并连接到服务器
|
|
|
TcpClient client = new TcpClient();
|
|
|
client.Connect(serverIP, port);
|
|
|
- // 获取网络流
|
|
|
NetworkStream stream = client.GetStream();
|
|
|
|
|
|
- // 要发送的请求报文(根据实际需求修改)
|
|
|
- byte[] packet = BuildPacket(json);
|
|
|
- // 发送请求
|
|
|
+ // 发送数据
|
|
|
+ byte[] data = Encoding.UTF8.GetBytes(json);
|
|
|
+ byte[] lengthPrefix = BitConverter.GetBytes(data.Length);
|
|
|
+ byte[] packet = new byte[lengthPrefix.Length + data.Length];
|
|
|
+
|
|
|
+ Buffer.BlockCopy(lengthPrefix, 0, packet, 0, lengthPrefix.Length);
|
|
|
+ Buffer.BlockCopy(data, 0, packet, lengthPrefix.Length, data.Length);
|
|
|
+
|
|
|
stream.Write(packet, 0, packet.Length);
|
|
|
Console.WriteLine($"已发送请求: {json}");
|
|
|
|
|
|
- // 接收响应
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int bytesRead = stream.Read(buffer, 0, buffer.Length);
|
|
|
- string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
|
|
|
+ // 接收数据 - 先读取长度
|
|
|
+ byte[] lengthBuffer = new byte[4];
|
|
|
+ int totalBytesRead = 0;
|
|
|
+ while (totalBytesRead < lengthBuffer.Length)
|
|
|
+ {
|
|
|
+ int bytesRead = stream.Read(lengthBuffer, totalBytesRead, lengthBuffer.Length - totalBytesRead);
|
|
|
+ if (bytesRead == 0) break;
|
|
|
+ totalBytesRead += bytesRead;
|
|
|
+ }
|
|
|
+
|
|
|
+ int messageLength = BitConverter.ToInt32(lengthBuffer, 0);
|
|
|
+
|
|
|
+ // 根据长度读取完整数据
|
|
|
+ byte[] messageBuffer = new byte[messageLength];
|
|
|
+ totalBytesRead = 0;
|
|
|
+ while (totalBytesRead < messageLength)
|
|
|
+ {
|
|
|
+ int bytesRead = stream.Read(messageBuffer, totalBytesRead, messageLength - totalBytesRead);
|
|
|
+ if (bytesRead == 0) break;
|
|
|
+ totalBytesRead += bytesRead;
|
|
|
+ }
|
|
|
|
|
|
+ string responseMessage = Encoding.UTF8.GetString(messageBuffer, 0, totalBytesRead);
|
|
|
Console.WriteLine($"收到响应: {responseMessage}");
|
|
|
|
|
|
- // 关闭流和连接
|
|
|
- stream.Close();
|
|
|
- client.Close();
|
|
|
return responseMessage;
|
|
|
}
|
|
|
catch (SocketException ex)
|
|
|
@@ -357,5 +379,79 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
return ms.ToArray();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static string ToServerReq(string strJSON)
|
|
|
+ {
|
|
|
+ int nRet = 0;
|
|
|
+ string strServerIP = "10.1.162.175";
|
|
|
+ string strResp = "";
|
|
|
+ IPAddress ip = IPAddress.Parse(strServerIP);
|
|
|
+ Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ clientSocket.Connect(new IPEndPoint(ip, SN_SERVICE_PORT));
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ nRet = -13;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(null);
|
|
|
+ byte[] bytJson = Encoding.UTF8.GetBytes(strJSON); //Encoding.ASCII.GetBytes(strJSON);
|
|
|
+ stHead.setDataLen(bytJson.Length);
|
|
|
+ byte[] bytBuf = new byte[FACT_DATA_HEAD.MY_LEN + bytJson.Length];
|
|
|
+ byte[] bytSrcHead = stHead.getBytes();
|
|
|
+ Array.Copy(bytSrcHead, 0, bytBuf, 0, bytSrcHead.Length);
|
|
|
+ Array.Copy(bytJson, 0, bytBuf, FACT_DATA_HEAD.MY_LEN, bytJson.Length);
|
|
|
+ clientSocket.Send(bytBuf);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ nRet = -12;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] bytRecv = new byte[MAX_BUF_SIZE];
|
|
|
+ int nRecvSize = 0, nSize = 0;
|
|
|
+ nRecvSize = clientSocket.Receive(bytRecv, 0, FACT_DATA_HEAD.MY_LEN, SocketFlags.None);
|
|
|
+
|
|
|
+ // string strTmp;
|
|
|
+ // strTmp = string.Format("clientSocket.Receive={0}", nRecvSize);
|
|
|
+ // Trace.WriteLine(strTmp);
|
|
|
+
|
|
|
+ if (nRecvSize > 0)
|
|
|
+ {
|
|
|
+ FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(bytRecv);
|
|
|
+ Console.WriteLine(stHead.getFlag());
|
|
|
+ Console.WriteLine(FACT_DATA_HEAD.PROTOCOL_FLAG);
|
|
|
+ //if (stHead.getFlag() == FACT_DATA_HEAD.PROTOCOL_FLAG)
|
|
|
+ {
|
|
|
+ int nPos = 0;
|
|
|
+ nSize = stHead.getDatalen();
|
|
|
+ while (nPos < nSize)
|
|
|
+ {
|
|
|
+ nRecvSize = clientSocket.Receive(bytRecv, nPos, 1869881979 - nPos, SocketFlags.None);
|
|
|
+ if (nRecvSize < 0)
|
|
|
+ {
|
|
|
+ nRet = -14;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ nPos += nRecvSize;
|
|
|
+ }
|
|
|
+ strResp = Encoding.Default.GetString(bytRecv, 0, nSize);
|
|
|
+ }
|
|
|
+ //else nRet = -11;
|
|
|
+ }
|
|
|
+ else nRet = -10;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (nRet > -13) clientSocket.Shutdown(SocketShutdown.Both);
|
|
|
+ clientSocket.Close();
|
|
|
+ return strResp;
|
|
|
+ }
|
|
|
}
|
|
|
}
|