/// <summary> /// 连接服务端 /// </summary> /// <returns></returns> public bool StartConnet(ref string errMsg) { DisConn(); clientSk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { if (GetInternetConStatus.GetNetConStatus(endIpInfo.Address.ToString()) == 3) { clientSk.Connect(endIpInfo);//连接服务器 } else { return(false); } } catch (Exception ex) { errMsg = ex.ToString(); return(false); } if (clientSk.Connected == true) { //通过Clientsoket发送数据 //int receiveLength = clientSk.Receive(refBuffer); clientSk.BeginReceive(refBuffer, 0, refBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null); return(true); } else { return(false); } }
//服务开启 public void StartServer() { try { //服务器IP地址 //IPAddress ip = IPAddress.Parse("127.0.0.1"); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (GetInternetConStatus.GetNetConStatus(ipendpoint.Address.ToString()) == 3) { serverSocket.Bind(ipendpoint);//new IPEndPoint(ip, myProt)); //绑定IP地址:端口 } else { return; } serverSocket.Listen(10); //设定最多10个排队连接请求 Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString()); //通过Clientsoket发送数据 Thread myThread = new Thread(ListenClientConnect); myThread.IsBackground = true; myThread.Name = "StartServer"; myThread.Start(); Console.ReadLine(); } catch (Exception ex) { } }
private void checkInternet(object sender, DoWorkEventArgs e) { //监测是否连接互联网 int iNetStatus = GetInternetConStatus.GetNetConStatus("baidu.com"); if (iNetStatus == 3 || iNetStatus == 2) { Led_InternetOk.Value = true; } else { Led_InternetOk.Value = false; } }
/// <summary> /// 一个 AsyncCallback 委托,它引用操作完成时要调用的方法 /// </summary> /// <param name="Ar"></param> public void ReceiveCallBack(IAsyncResult Ar) { try { if (GetInternetConStatus.GetNetConStatus(endIpInfo.Address.ToString()) == 3) { if (clientSk.Connected == true) { //clientSk.Connect(endIpInfo);//连接服务器 Msg msg = new Msg(); if (clientSk.Poll(10, SelectMode.SelectRead) == false) { int Rnd = clientSk.EndReceive(Ar); //string a1 = GetHexadecimalValue(ToHexString(refBuffer).Substring(ToHexString(refBuffer).Length - 8, 8)).ToString(); string a2 = GetHexadecimalValue(ToHexString(refBuffer).Substring(4, 8)).ToString(); /*MODIFLY 2018/10/11 by Wang Sizhe START*/ msg.Message = Encoding.UTF8.GetString(refBuffer, 0, Rnd); //GetHexadecimalValue(ToHexString(refBuffer)).ToString(); /*MODIFLY 2018/10/11 by Wang Sizhe END*/ msg.Messageb = a2; //string msg = GetHexadecimalValue(ToHexString(refBuffer)).ToString();//GetHexadecimalValue(Encoding.Default.GetString(refBuffer, 0, Rnd)).ToString();// Encoding.Default.GetString(refBuffer, 0, Rnd); if (ReceiveMsg != null && !string.IsNullOrEmpty(msg.Message)) { ReceiveMsg(msg);//引发事件 } clientSk.BeginReceive(refBuffer, 0, refBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null); } else { // clientSk.Connected = false; msg.Message = "服务器断开!"; if (ReceiveMsg != null && !string.IsNullOrEmpty(msg.Message)) { ReceiveMsg(msg);//引发事件 } clientSk.BeginReceive(refBuffer, 0, refBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null); clientSk.Dispose();//加入部分 } } } } catch (Exception ex) { } }
private void FrmIndex_Load(object sender, EventArgs e) { this.Top = 10; this.Left = 10; using (BugTraceEntities context = new BugTraceEntities(EntityContextHelper.GetEntityConnString())) { #region 加载当前项目信息 var projectCode = UserInfo.GetInstence().DefaultProjectCode; if (!string.IsNullOrWhiteSpace(projectCode)) { var projectInfo = context.PPM_ProjectInfo.Where(p => p.ProjectCode == projectCode).FirstOrDefault(); if (projectInfo != null) { txtCurrProjectName.Text = projectInfo.ProjectName; txtSVN.Text = projectInfo.SVN; txtDevServerInfo.Text = projectInfo.DevServerInfo; txtPubServerInfo.Text = projectInfo.PubServerInfo; txtOtherInfo.Text = projectInfo.OtherInfo; var updatePerson = context.SYS_User.Where(p => p.UserCode == projectInfo.UpdatePerson).FirstOrDefault(); if (updatePerson != null) { string updatePersonName = updatePerson.UserName; this.lblUpdateInfo.Text = "(最后一次修改人:" + updatePersonName + ",修改时间:" + (projectInfo.UpdateDate == null ? "未记录" : projectInfo.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")) + ")"; } else { this.lblUpdateInfo.Text = ""; } } } #endregion } #region 开启一个新线程,检测未读邮件信息 this.dgvEmail.AutoGenerateColumns = false; this.dgvEmail.AllowUserToAddRows = false; Thread thread = new Thread(new ThreadStart(delegate ///监听所有客户端连接,并添加到ListBox控件里 { dtblEmail = new DataTable(); dtblEmail.Columns.Add("Subject", typeof(string)); dtblEmail.Columns.Add("ReceiveDate", typeof(DateTime)); dtblEmail.Columns.Add("From", typeof(string)); while (true) { UnReadEmailCount = 0; int netStatus = GetInternetConStatus.GetNetConStatus(); if (netStatus == 2 || netStatus == 3) { dtblEmail.Rows.Clear(); try { QueryEmailStatus(); } catch (Exception ex) { MyLog.LogError("查询未读邮件异常!", ex); } } Thread.Sleep(1000 * 60 * 10);//每10分钟查看邮件信息 } })); thread.IsBackground = true; thread.Start(); #endregion #region 获取外网ip及城市信息,及城市的天气信息 Thread threadWeather = new Thread(new ThreadStart(delegate //监听所有客户端连接,并添加到ListBox控件里 { try { string hostname; System.Net.IPHostEntry localhost; System.Net.IPAddress[] localaddr; hostname = System.Net.Dns.GetHostName(); localhost = System.Net.Dns.GetHostEntry(hostname); localaddr = localhost.AddressList; //localaddr中就是本机ip地址 foreach (IPAddress ipInfo in localaddr) { if (ipInfo.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { this.Invoke(new MethodInvoker(delegate { lblInnerIP.Text = ipInfo.ToString(); })); } } cn.com.webxml.www.WeatherWebService client = new cn.com.webxml.www.WeatherWebService(); string[] weaInfo = client.getWeatherbyCityName("青岛"); this.Invoke(new MethodInvoker(delegate { Label lblDate = new Label(); lblDate.Text = weaInfo[13]; lblDate.Width = 140; lblDate.Padding = new System.Windows.Forms.Padding(0, 7, 0, 0); Label lblTemperature = new Label(); lblTemperature.Text = weaInfo[12]; lblTemperature.Padding = new System.Windows.Forms.Padding(0, 7, 0, 0); Label lblWind = new Label(); lblWind.Text = weaInfo[14]; lblWind.Padding = new System.Windows.Forms.Padding(0, 7, 0, 0); PictureBox pbImage = new PictureBox(); string strPath = System.Environment.CurrentDirectory + "\\Resource\\weather\\" + weaInfo[15]; pbImage.Image = Image.FromFile(strPath); flowWeather1.Controls.Add(lblDate); flowWeather1.Controls.Add(lblTemperature); flowWeather1.Controls.Add(lblWind); flowWeather1.Controls.Add(pbImage); Label lblDate2 = new Label(); lblDate2.Text = weaInfo[18]; lblDate2.Width = 140; lblDate2.Padding = new System.Windows.Forms.Padding(0, 7, 0, 0); Label lblTemperature2 = new Label(); lblTemperature2.Text = weaInfo[17]; lblTemperature2.Padding = new System.Windows.Forms.Padding(0, 7, 0, 0); Label lblWind2 = new Label(); lblWind2.Text = weaInfo[19]; lblWind2.Padding = new System.Windows.Forms.Padding(0, 7, 0, 0); PictureBox pbImage2 = new PictureBox(); string strPath2 = System.Environment.CurrentDirectory + "\\Resource\\weather\\" + weaInfo[20]; pbImage2.Image = Image.FromFile(strPath2); flowWeather2.Controls.Add(lblDate2); flowWeather2.Controls.Add(lblTemperature2); flowWeather2.Controls.Add(lblWind2); flowWeather2.Controls.Add(pbImage2); })); } catch (Exception ex) { MyLog.LogError("获取IP信息失败!", ex); } })); threadWeather.IsBackground = true; threadWeather.Start(); #endregion }