//private static CountManager countManager = new CountManager(); //private void CountManager_ExceedLimitation(string obj) //{ // if (Validation() == false) // { // MessageBox.Show("No license provided"); // } //} private bool Validation() { var keyFile = "publicKey.xml"; string publicKey = ""; if (File.Exists(keyFile)) { publicKey = File.ReadAllText(keyFile); } var licenseValidator = new LicenseValidator(publicKey, "license.lic"); try { licenseValidator.AssertValidLicense(); var log = $"{licenseValidator.Name} authorized License to {licenseValidator.LicenseAttributes["name"]}\n"; this.Text = "SIAPM Attend Form authorized"; LogHelper.Info(log); ResultText.AppendText(log); return(true); } catch (Exception ex) { LogHelper.Error("Validation", ex); ResultText.AppendText("No license provided or validation is failed.\n"); } return(false); }
private void button1_Click(object sender, EventArgs e) { if (Validation() == false) { MessageBox.Show("No license provided\n"); return; } if (targetDate.CompareTo(default(DateTime)) == 0) { MessageBox.Show("please set target time", "Attend log", MessageBoxButtons.OK, MessageBoxIcon.Question); return; } try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { var fName = openFileDialog1.FileName; Utils.CalucateEmployeAttendence(fName, targetDate); MessageBox.Show("Successful imported.", "Attend log", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { ResultText.AppendText(ex.Message); } }
void RecMsg(object sokConnectionparn) { Socket sokClient = sokConnectionparn as Socket; while (true) { // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; if (length <= 0) { removeClient(sokClient, "Length isn't non negative."); break; } } catch (SocketException SokEx) { removeClient(sokClient, SokEx.Message); break; } catch (Exception ex) { removeClient(sokClient, ex.Message); break; } string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);// 将接受到的字节数据转化成字符串; protocol(strMsg); ResultText.AppendText("Received message from tester: " + strMsg + "\n"); } }
private void generateKeys(bool isLoad = false) { if (File.Exists("privateKey.xml") || File.Exists("publicKey.xml")) { if (isLoad == false) { var result = MessageBox.Show("The key is existed, override it?", "Warning", MessageBoxButtons.YesNo); if (result == DialogResult.No) { return; } } else { return; } } var privateKey = ""; var publicKey = ""; LicenseGenerator.GenerateLicenseKey(out privateKey, out publicKey); File.WriteAllText("privateKey.xml", privateKey); File.WriteAllText("publicKey.xml", publicKey); ResultText.AppendText("The Key is created, please backup it.\n"); }
/// <summary> /// call back after message is recevied from TCP server /// </summary> private void recMsg() { while (true) { Thread.Sleep(800); // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; } catch (SocketException se) { message = "异常;" + se.Message; ResultText.AppendText(message + "\n"); return; } catch (Exception e) { message = "异常:" + e.Message; ResultText.AppendText(message + "\n"); return; } strMsgFromMachine = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);// 将接受到的字节数据转化成字符串; ResultText.AppendText("Machine: " + strMsgFromMachine + "\n"); protocol(strMsgFromMachine); } }
private void BeginListenButton_Click(object sender, EventArgs e) { // 创建负责监听的套接字,注意其中的参数; socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 获得文本框中的IP对象; IPAddress address = IPAddress.Parse(MachineIPText.Text.Trim()); // 创建包含ip和端口号的网络节点对象; IPEndPoint endPoint = new IPEndPoint(address, int.Parse(MachinePortTextBox.Text.Trim())); try { // 将负责监听的套接字绑定到唯一的ip和端口上; socketWatch.Bind(endPoint); } catch (SocketException se) { MessageBox.Show("异常:" + se.Message); return; } // 设置监听队列的长度; socketWatch.Listen(10); // 创建负责监听的线程; threadWatch = new Thread(WatchConnecting); threadWatch.IsBackground = true; threadWatch.Start(); ResultText.AppendText("服务器启动监听成功!\n"); }
private void removeClient(Socket sokClient, string ex) { ResultText.AppendText("异常:" + ex + "\n"); // 从 通信套接字 集合中删除被中断连接的通信套接字; dict.Remove(sokClient.RemoteEndPoint.ToString()); // 从通信线程集合中删除被中断连接的通信线程对象; dictThread.Remove(sokClient.RemoteEndPoint.ToString()); // 从列表中移除被中断的连接IP lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString()); }
private int sendMessage(string strMsg) { int result = -1; //string strMsg = IDText.Text.Trim() + "," + PWText.Text.Trim(); byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); if (sockClient != null) { result = sockClient.Send(arrMsg); // 发送消息; } strMsgToMachine = "Tester cmd: " + strMsg; ResultText.AppendText(strMsgToMachine + "\n"); return(result); }
/// <summary> /// 监听客户端请求的方法; /// </summary> void WatchConnecting() { while (true) // 持续不断的监听客户端的连接请求; { // 开始监听客户端连接请求,Accept方法会阻断当前的线程; Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字; // 想列表控件中添加客户端的IP信息; lbOnline.Items.Add(sokConnection.RemoteEndPoint.ToString()); // 将与客户端连接的 套接字 对象添加到集合中; dict.Add(sokConnection.RemoteEndPoint.ToString(), sokConnection); ResultText.AppendText("客户端连接成功!\n"); Thread thr = new Thread(RecMsg); thr.IsBackground = true; thr.Start(sokConnection); dictThread.Add(sokConnection.RemoteEndPoint.ToString(), thr); // 将新建的线程 添加 到线程的集合中去。 } }
public bool connectToTCPServer() { IPEndPoint endPoint = new IPEndPoint(machineIp, machinePort); sockClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { ResultText.AppendText("与服务器连接中...\n"); sockClient.Connect(endPoint); isConnectToTCPServer = true; } catch (SocketException se) { ResultText.AppendText(se.Message); return(isConnectToTCPServer); //this.Close(); } ResultText.AppendText("与服务器连接成功!!!\n"); threadClient = new Thread(recMsg); threadClient.IsBackground = true; threadClient.Start(); return(isConnectToTCPServer); }
private void CalculateResult() { ResultText.Text = ""; if (File.Exists(@Utility.DATAPATH)) { string ___BLOCKDATA = File.ReadAllText(@Utility.DATAPATH); JObject BLOCKCHAIN = JObject.Parse(___BLOCKDATA); JArray items = (JArray)BLOCKCHAIN["blocks"]; string Currenthash, BlockHash, PrevHash; if (items.Count > 1) { for (int i = 0; i < items.Count - 1; i++) { BlockHash = (string)items[i]["blockhash"]; PrevHash = (string)items[i + 1]["previoushash"]; if (!BlocksChain.Validation(PrevHash, BlockHash)) { Chainflag = false; ResultText.Text += "\r\n------------------------------------"; ResultText.Text += "\r\nCHAIN ERROR AT BLOCK : " + (i + 1).ToString(); ResultText.Text += "\r\n------------------------------------"; break; } else { Chainflag = true; } } if (Chainflag) { for (int y = 1; y < items.Count; y++) { Currenthash = BlocksChain.GenerateBlockHash((string)items[y]["votehash"], (string)items[y]["timestamp"], (string)items[y]["previoushash"], Configuration.SaltValue); if (BlocksChain.BlockValidation(Currenthash, (string)items[y]["blockhash"])) { ResultText.Text += "\r\n------------------------------------"; ResultText.Text += "\r\nCORRUPT DATA IN BLOCK : " + (y + 1).ToString(); ResultText.Text += "\r\n------------------------------------"; Dataflag = false; break; } else { Dataflag = true; } } } if (Chainflag && Dataflag) { ResultText.AppendText("\r\n-------------------------------------"); ResultText.AppendText("\r\nVALID BLOCK CHAIN -- COMPUTING RESULT"); ResultText.AppendText("\r\n-------------------------------------"); } for (int z = 0; z < items.Count; z++) { for (int ID = 70001; ID <= 70005; ID++) { Thread.Sleep(500); for (int UID = 10001; UID <= 10005; UID++) { //ResultText.Text += "\r\n"; if ((string)items[z]["votehash"] == SILICON64.GenerateHash(ID.ToString() + UID.ToString())) { Console.Beep(700, 500); ResultText.AppendText("\r\n\r\nBLOCK - [" + (z + 1).ToString() + "]\r\n"); ResultText.AppendText("\r\nVOTE HASH >>" + (string)items[z]["votehash"]); ResultText.AppendText("\r\nBLOCK HASH >>" + (string)items[z]["blockhash"]); if (ID == 70001) { A++; } if (ID == 70002) { B++; } if (ID == 70003) { C++; } if (ID == 70004) { D++; } if (ID == 70005) { E++; } Thread.Sleep(1500); continue; } } } } ResultText.Text += "\r\n\r\n"; Console.Beep(500, 2500); ResultText.Text += "\r\n70001 ALPHA : "+ A.ToString(); ResultText.Text += "\r\n70002 BETA : "+ B.ToString(); ResultText.Text += "\r\n70003 DELTA : "+ C.ToString(); ResultText.Text += "\r\n70004 GAMMA : "+ D.ToString(); ResultText.Text += "\r\n70005 ECHO : "+ E.ToString(); } else { ResultText.Text = "EMPTY CHAIN"; } } else { ResultText.Text = "Blocks not found!!"; } }