// 启动端口映射 public string StartMap() { LogHelpMe.WriteLog(string.Format("{0} 映射启动了哦", this.Name), LogHelpMe.LogEnum.提示); // 创建一个socket监听 //Socket beginSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { if (this.Point_in_host == "Any IP") { serverListener = new TcpListener(new IPEndPoint(IPAddress.Any, this.point_in_port)); } else { serverListener = new TcpListener(new IPEndPoint(IPAddress.Parse(this.Point_in_host), this.point_in_port)); } serverListener.Start(100); serverListener.BeginAcceptTcpClient(on_local_connected, serverListener); } catch (Exception exp) { LogHelpMe.WriteLog(exp.Message, LogHelpMe.LogEnum.错误); return("wrong"); } return(""); }
private void TsBtnRun_Click(object sender, EventArgs e) { if (!Ischeck) { if (!checkIP()) { return; } } if (lVMapper.SelectedItems.Count < 1) { return; } var result = PmList.Where(x => x.Id == Convert.ToInt32(lVMapper.SelectedItems[0].Tag)); PortMap pm = result.First(); if (result.Count() == 0) { return; } string result1 = pm.StartMap(); if (result1 != "") { LogHelpMe.WriteLog(string.Format("映射组'{0}'无法启动", pm.Name), LogHelpMe.LogEnum.提示); return; } LogHelpMe.WriteLog(string.Format("映射组'{0}'手动成功启动\n", pm.Name), LogHelpMe.LogEnum.提示); pm.IsRun = true; lVMapper.SelectedItems[0].ImageIndex = 0; this.tsBtnRun.Enabled = false; this.tsBtnStop.Enabled = true; }
private void TsBtnStop_Click(object sender, EventArgs e) { if (this.lVMapper.SelectedItems.Count < 1) { return; } int id = Convert.ToInt32(this.lVMapper.SelectedItems[0].Tag); var result = PmList.Where(x => x.Id == id); if (result.Count() > 0) { LogHelpMe.WriteLog(string.Format("映射组'{0}'关闭,手动停止", result.First().Name), LogHelpMe.LogEnum.提示); result.First().IsRun = false; result.First().IsAuto = false; ClearClientSockets(result.First()); this.tsBtnRun.Enabled = true; this.tsBtnStop.Enabled = false; this.lVMapper.SelectedItems[0].ImageIndex = 1; } }
private void on_local_connected(IAsyncResult ar) { TcpClient beginClient; try { beginClient = serverListener.EndAcceptTcpClient(ar); } catch (Exception ee) { Zhangqi.LogHelpMe.WriteLog(ee.Message + ee.StackTrace, LogHelpMe.LogEnum.错误); return; } serverListener.BeginAcceptTcpClient(on_local_connected, serverListener); FrmMapper.IsClear = false; if (tcpClientList.Count > 0) { if (tcpClientList.Count >= this.MaxNum) { string message = (string.Format("客户端'{0}'无法连接,{1} 总连接数目超限", beginClient.Client.RemoteEndPoint.ToString(), this.Name)); LogHelpMe.WriteLog(message, LogHelpMe.LogEnum.警告); FrmMapper.IsClear = true; return; } if (SingleDic.ContainsKey(beginClient.Client.RemoteEndPoint.ToIP())) { if (SingleDic != null && SingleDic[beginClient.Client.RemoteEndPoint.ToIP()].Count >= this.SingleNum) { string message = (string.Format("客户端'{0}'无法连接,{1} 单连接数目超限", beginClient.Client.RemoteEndPoint.ToString(), this.Name)); LogHelpMe.WriteLog(message, LogHelpMe.LogEnum.警告); FrmMapper.IsClear = true; return; } } } tcpClientList.Add(beginClient); if (SingleDic == null) { return; } LogHelpMe.WriteLog(string.Format("客户端'{0}'连接进来,{1} 该IP单连接数目为 {2}", beginClient.Client.RemoteEndPoint.ToString(), Name, SingleDic[beginClient.Client.RemoteEndPoint.ToIP()].Count), LogHelpMe.LogEnum.提示); // 连接到转发的Socket TcpClient remoteClient = new TcpClient(); try { TransClient transClient = new TransClient() { RemoteClient = remoteClient, BeginClient = beginClient }; remoteClient.Connect(IPAddress.Parse(this.Point_out_host), this.Point_out_port); beginClient.Client.BeginReceive(send_buf, 0, 4096, SocketFlags.None, new AsyncCallback(AsyncSend), transClient); remoteClient.Client.BeginReceive(recieve_buf, 0, 4096, SocketFlags.None, new AsyncCallback(AsyncRecieve), transClient); FrmMapper.IsClear = true; } catch { LogHelpMe.WriteLog(string.Format("{0} 转发端口失败,监听服务器没有开启", Name), LogHelpMe.LogEnum.错误); tcpClientList.Remove(beginClient); } }
// 循环定时启动 private void Run() { while (true) { if (this.InvokeRequired) { this.BeginInvoke(new Action(() => UpdateData())); } for (int i = 0; i < PmList.Count; i++) { DateTime startTime = Convert.ToDateTime(PmList[i].Start_time); if (DateTime.Compare(startTime, DateTime.Now) > 0) { continue; } DateTime stopTime = Convert.ToDateTime(PmList[i].Stop_time); if (PmList[i].Start_time == PmList[i].Stop_time) { continue; } if (DateTime.Compare(DateTime.Now, stopTime) >= 0) { if (PmList[i].IsRun) { LogHelpMe.WriteLog(string.Format("映射组'{0}'关闭,定时结束时间到", PmList[i].Name), LogHelpMe.LogEnum.提示); ClearClientSockets(PmList[i]); PmList[i].IsRun = false; if (this.InvokeRequired) { this.Invoke(new Action(() => { foreach (ListViewItem item in lVMapper.Items) { if (item.Tag.ToString() == PmList[i].Id.ToString()) { item.ImageIndex = 1; break; } } })); } } continue; } if (PmList[i].IsRun) { continue; } if (PmList[i].IsAuto && IsFirstEnter) { if (MessageBox.Show(string.Format("{0} 时间范围满足启动要求,是否启动?", PmList[i].Name), "提示", MessageBoxButtons.YesNo) == DialogResult.No) { PmList[i].IsAuto = false; continue; } } if (!PmList[i].IsAuto) { continue; } string result = PmList[i].StartMap(); if (result != "") { LogHelpMe.WriteLog(string.Format("映射组'{0}'无法启动", PmList[i].Name), LogHelpMe.LogEnum.提示); continue; } LogHelpMe.WriteLog(string.Format("映射组'{0}'定时成功启动\n", PmList[i].Name), LogHelpMe.LogEnum.提示); PmList[i].IsRun = true; if (this.InvokeRequired) { this.Invoke(new Action(() => { foreach (ListViewItem item in lVMapper.Items) { if (item.Tag.ToString() == PmList[i].Id.ToString()) { item.ImageIndex = 0; break; } } })); } } IsFirstEnter = false; Thread.Sleep(1000); } }