using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace QXXWCX { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //QXXWCXIP //string _ip = "192.168.1.204"; string _ip = "192.168.1.204"; //端口号 int _port = 11010; Socket _client_QXXWCX; bool canSend = false; //数据接收线程 Thread th_QXXWCX = null; Thread th_FIle = null; [Obsolete] //连接服务器 private void btnConnect_Click(object sender, EventArgs e) { _client_QXXWCX = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPEndPoint point = new IPEndPoint(IPAddress.Parse(_ip), _port); _client_QXXWCX.Connect(point); th_QXXWCX = new Thread(ReceiveMessage_MEMS); th_QXXWCX.IsBackground = true; th_QXXWCX.Start(); Console.WriteLine("已连接服务器" + "\r\n"); CallInvokeUIUpdate("UpdatertbMessage", "已连接服务器!\n"); } catch (Exception) { Console.WriteLine("服务器没有开启" + "\r\n"); CallInvokeUIUpdate("UpdatertbMessage", "服务器没有开启!\n"); } } public void SendMessage_MEMS(byte[] messageBytes) { if (_client_QXXWCX.Connected) { try { _client_QXXWCX.Send(messageBytes); CallInvokeUIUpdate("UpdatertbMessage", "发送数据:" + BitConverter.ToString(messageBytes) + "\n"); } catch (Exception) { Console.WriteLine("发送数据失败!" + "\r\n"); CallInvokeUIUpdate("UpdatertbMessage", "发送数据失败!\n"); } } else { Console.WriteLine("连接断开!" + "\r\n"); CallInvokeUIUpdate("UpdatertbMessage", "连接断开!\n"); } } // 弹出窗口 提示 void SendMsgSilent(string msg) { void msgSend() { MessageBox.Show(msg, "提示"); } Thread td_msg = new Thread(msgSend); td_msg.Start(); } private static bool PasswordEquals(byte[] b1, byte[] b2) { if (b1 == null || b2 == null) return false; for (int i = 0; i < b2.Length; i++) { if (b1[i] != b2[i]) return false; } return true; } /// /// /// public void ReceiveMessage_MEMS() { byte[] messageBytes = null; int num = 0; int a = 0; try { while (true) { //while (canSend == false) //{ messageBytes = new byte[_client_QXXWCX.ReceiveBufferSize]; num = _client_QXXWCX.Receive(messageBytes); byte[] buffer = new byte[num]; for (int i = 0; i < num; i++) { buffer[i] = messageBytes[i]; Console.WriteLine(buffer[i]); } Console.WriteLine(BitConverter.ToString(buffer)); CallInvokeUIUpdate("UpdatertbMessage", "接收数据:" + BitConverter.ToString(buffer) + "\n"); Console.WriteLine("测试节点==》CallInvokeUIUpdate"); if (canSend == false&&buffer[0] == 0x41 && buffer[1] == 0x35 && buffer[2] == 0x42 && buffer[3] == 0x35) { ParsingData(buffer); } else if(canSend) { //Console.WriteLine("开始接收图片!"); //CallInvokeUIUpdate("UpdatertbMessage", "开始接收图片!\n"); FileStream wrtr; //文件读写类 byte[] data; //int rect = _client_QXXWCX.Receive(data, 0, 4, 0); //用来接收图片字节流长度 //int size = BitConverter.ToInt32(data, 0); //16进制转成int型 int rect; int size = picLength; int dataleft = size - num; data = new byte[size]; //创建byte组 byte[] result = new byte[size]; //wrtr = new FileStream(Environment.CurrentDirectory + "\\picturefile\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt", FileMode.Create,FileAccess.Write); //创建新文件"new.jpg" string ascii = Encoding.Default.GetString(buffer); //result[i] = buffer[i]; Console.WriteLine(ascii); string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveDir\"; //设置当前目录 if (!System.IO.Directory.Exists(CurDir)) System.IO.Directory.CreateDirectory(CurDir); //该路径不存在时,在当前文件目录下创建文件夹"导出.." //不存在该文件时先创建 string datetime = DateTime.Now.ToString("yyyyMMddHHmmss"); //String filePath = CurDir + datetime + ".txt"; // 生成txt文件 //测试文件 可以生成base64编码txt文件,能用转图片 String filePath = CurDir + "base64" + ".txt"; // 生成txt文件 System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, true); //文件已覆盖方式添加内容 file1.Write(ascii); //保存数据到文件 file1.Close(); //关闭文件 file1.Dispose(); //释放对象 /* for (int i=0;i invokeAction = new Action(CallInvokeUIUpdate); //判断操作控件的线程是否创建控件的线程 //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法 if (this.InvokeRequired) { //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用) this.Invoke(invokeAction, code, arg); } else { //窗体线程,即主线程 switch (code) { case "UpdatertbMessage": rtbMessage.AppendText(arg); break; default: break; } } } /// /// 16进制转普通字符串 /// /// public static string HexToString(string Hexdata) { string result = string.Empty; byte[] arrByte = new byte[Hexdata.Length / 2]; int index = 0; for (int i = 0; i < Hexdata.Length; i += 2) { arrByte[index++] = Convert.ToByte(Hexdata.Substring(i, 2), 16); } result = System.Text.Encoding.UTF8.GetString(arrByte); return result; } public static string ByteToString(byte[] bytes) { string length = BitConverter.ToString(bytes, 0, bytes.Length); string[] length1 = length.Split('-'); string length2 = ""; for (int i = 0; i < length1.Length; i++) { length2 += length1[i]; } return length2; } /// /// 字符串转16进制字节数组 /// private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } } }