Ver código fonte

TCP客户端程序

callm 1 ano atrás
pai
commit
03d5ee59b8

+ 5 - 0
FileAnalysis/FileAnalysis.csproj

@@ -65,6 +65,10 @@
     <Reference Include="System.Net.Http" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
+    <Reference Include="UMESDLLService, Version=1.0.0.3, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\UAS_MesInterface(LGDZ)\bin\Debug\UMESDLLService.dll</HintPath>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="DataHelper.cs" />
@@ -76,6 +80,7 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="TCPClient.cs" />
     <EmbeddedResource Include="Form1.resx">
       <DependentUpon>Form1.cs</DependentUpon>
     </EmbeddedResource>

+ 20 - 58
FileAnalysis/Form1.cs

@@ -1,13 +1,10 @@
-using NPOI.SS.Formula.Functions;
-using NPOI.SS.UserModel;
-using System;
+using System;
 using System.Collections.Generic;
-using System.IO;
-using System.Net;
+using System.Drawing;
 using System.Security.Cryptography;
 using System.Text;
 using System.Windows.Forms;
-using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
+using UMESDLLService;
 
 namespace FileAnalysis
 {
@@ -18,59 +15,10 @@ namespace FileAnalysis
         {
             InitializeComponent();
         }
-
+        TCPClient client;
         private void Analysis_Click(object sender, EventArgs e)
         {
-            string url = "https://openapi.seewo.com/seewo-study-machine/device-manager/check-activate";
-            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
-            webrequest.Method = "POST";
-            webrequest.Timeout = 1000;
-            webrequest.ContentType = "application/x-www-form-urlencoded";
-            string secret = "QmxcghPaupQjjSOTV3NJLkPeyycEBuYk";
-            System.Collections.Hashtable pars = new System.Collections.Hashtable();
-            string sign = "x-sw-app-id3a1252d81dff4fd3b1ece153b3a9cb71x-sw-req-path/seewo-study-machine/device-manager/check-activatex-sw-version2";
-            webrequest.Headers.Add("x-sw-app-id", "3a1252d81dff4fd3b1ece153b3a9cb71"); //必填
-            webrequest.Headers.Add("x-sw-sign", GETMD5(secret + sign + secret));//必填
-            webrequest.Headers.Add("x-sw-req-path", "/seewo-study-machine/device-manager/check-activate");//必填
-            webrequest.Headers.Add("x-sw-version", "2");//必填
-            //webrequest.Headers.Add("x-sw-sign-type", "");
-            //webrequest.Headers.Add("x-sw-sign-headers", "");
-            //webrequest.Headers.Add("x-sw-timestamp", "");
-            //webrequest.Headers.Add("x-sw-content-md5", "");c
-            pars.Add("sn", "{sn:123}");
-            string buffer = "";
-            //发送POST数据 
-            if (!(pars == null || pars.Count == 0))
-            {
-                foreach (string key in pars.Keys)
-                {
-                    buffer = buffer + "&" + key + "=" + pars[key].ToString();
-                }
-                byte[] data = Encoding.UTF8.GetBytes(buffer);
-                using (Stream stream = webrequest.GetRequestStream())
-                {
-                    stream.Write(data, 0, data.Length);
-                }
-            }
-
-            string[] values = webrequest.Headers.GetValues("Content-Type");
-            WebResponse myResponse = webrequest.GetResponse();
-
-            using (Stream resStream = myResponse.GetResponseStream())//得到回写的流
-            {
-                StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
-
-                string Content = newReader.ReadToEnd();
-                ReturnData.Text = Content;
-                Dictionary<string, object> dic = new Dictionary<string, object>();
-                //dic = BaseUtil.ToDictionary(Content);
-                //if (!dic.ContainsKey("erpaccount"))
-                //{
-                //    oMsg = dic["reason"].ToString();
-                //    return false;
-                //}
-                newReader.Close();
-            }
+            client.Send("A001H10G1111111111111111110111100111111111111111111111111111111111111111111111111111111111111111111111111111 ");
         }
 
         private string GETMD5(string password)
@@ -144,10 +92,24 @@ namespace FileAnalysis
             }
             return ReturnData.ToArray();
         }
-
+        string nextLine;
         private void Form1_Load(object sender, EventArgs e)
         {
+            client = new TCPClient("172.16.15.10", "1030", "", "");
+            //label1.BackColor = Color.GreenYellow;
+            //string json = "";
+            //string json1 = "";
+            //MESHelper mes = new MESHelper();
+            //mes.GetMobileAllInfo("Q32024040002", out json1, out json);
+            //////mes.GetMobileAllInfo("F45112235030001",  out json1, out json);
+            //Console.WriteLine(json1);
+            //Console.WriteLine(json);
 
+            //StreamReader sR = new StreamReader(@"C:\Users\callm\Desktop\Android\036653943B010A11-C.txt", Encoding.Default);
+            //while ((nextLine = sR.ReadLine()) != null)
+            //{
+            //    ReturnData.AppendText(nextLine.ToString() + "\n");
+            //}
         }
     }
 }

+ 179 - 0
FileAnalysis/TCPClient.cs

@@ -0,0 +1,179 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FileAnalysis
+{
+    class TCPClient
+    {
+        private Socket socket;
+
+        private IPEndPoint serverFullAddr;//完整终端地址
+
+        private bool isOpen = false;
+
+        private bool receiveData;
+
+        private DataHelper dh;
+
+        private Dictionary<string, string> returnvalue = new Dictionary<string, string>();
+
+        public bool IsOpen
+        {
+            get
+            {
+                return isOpen;
+            }
+
+            set
+            {
+                isOpen = value;
+            }
+        }
+
+        public string IP
+        {
+            get
+            {
+                return iP;
+            }
+
+            set
+            {
+                iP = value;
+            }
+        }
+
+        public string Port
+        {
+            get
+            {
+                return port;
+            }
+
+            set
+            {
+                port = value;
+            }
+        }
+
+        public bool ReceiveData
+        {
+            get
+            {
+                return receiveData;
+            }
+
+            set
+            {
+                receiveData = value;
+            }
+        }
+
+        public Dictionary<string, string> Returnvalue
+        {
+            get
+            {
+                return returnvalue;
+            }
+
+            set
+            {
+                returnvalue = value;
+            }
+        }
+
+        internal DataHelper Dh
+        {
+            get
+            {
+                return dh;
+            }
+
+            set
+            {
+                dh = value;
+            }
+        }
+
+        private string iP;
+
+        private string port;
+
+        private int ResetCount = 0;
+
+        string decode = "";
+        string dename = "";
+
+        public TCPClient(string IP, string Port, string Decode, string Dename)
+        {
+            try
+            {
+                decode = Decode;
+                dename = Dename;
+                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), int.Parse(Port));//设置IP,端口
+                socket.ReceiveTimeout = 2000;
+                socket.Connect(serverFullAddr);
+            }
+            catch (Exception ex)
+            {
+
+            }
+        }
+
+        public void Send(string Command)
+        {
+            string str = "";
+            try
+            {
+                if (!socket.Connected || ResetCount == 20)
+                {
+                    socket.Close();
+                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                    socket.ReceiveTimeout = 2000;
+                    socket.Connect(serverFullAddr);
+                    ResetCount = 0;
+                }
+                byte[] arr = Encoding.ASCII.GetBytes(Command);
+                //Command = Command.Replace(" ", "");
+                //byte[] arr = new byte[Command.Length / 2];
+                //for (int i = 0; i < Command.Length / 2; i++)
+                //{
+                //    arr[i] = (byte)Convert.(Command.Substring(i * 2, 2), 16);
+                //}
+                socket.Send(arr);
+                //byte[] receive = new byte[1024 * 10];
+
+                //int length = socket.Receive(receive);
+                //str = ByteToHexadecimalString(receive, length);
+                //if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
+                //{
+                //    returnvalue.Add(socket.RemoteEndPoint.ToString(), str);
+                //}
+                //ResetCount = ResetCount + 1;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message + ex.StackTrace + "\n" + decode + " " + str);
+            }
+        }
+
+        public static string ByteToHexadecimalString(byte[] b, int length)
+        {
+            string returnStr = "";
+            if (b != null)
+            {
+                for (int i = 0; i < length; i++)
+                {
+                    returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
+                }
+            }
+            return returnStr.ToUpper();
+        }
+    }
+}

BIN
FileAnalysis/tool/UMESDLLService.dll