//bool isRec = true; //与客户端通信的套接字 是否 监听消息 #region 发送消息 到指定的客户端 -btnSend_Click //发送消息 到指定的客户端 private void btnSend_Click(object sender, EventArgs e) { //byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(txtInput.Text.Trim()); //从下拉框中 获得 要哪个客户端发送数据 string time; string connectionSokKey = cboClient.Text; if (!string.IsNullOrEmpty(connectionSokKey)) { //从字典集合中根据键获得 负责与该客户端通信的套接字,并调用send方法发送数据过去 dictConn[connectionSokKey].Send(txtInput.Text.Trim()); SYSTEMTIME_INFO StInfo; StInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref StInfo); time = StInfo.wYear.ToString() + "/" + StInfo.wMonth.ToString() + "/" + StInfo.wDay.ToString() + " "; time += (StInfo.wHour + 8).ToString() + ":" + StInfo.wMinute.ToString(); txtLog.AppendText(time + "rn"); txtLog.AppendText("对" + cboClient.Text + "说:" + txtInput.Text.Trim() + "rn"); txtInput.Text = ""; //sokMsg.Send(arrMsg); } else { MessageBox.Show("请选择要发送的子计算机~~"); } }
private void GetInfo_Click() { //调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径 const int nChars = 128; StringBuilder Buff = new StringBuilder(nChars); ComputerInfo.GetWindowsDirectory(Buff, nChars); string Windows路径 = "Windows路径:" + Buff.ToString(); ComputerInfo.GetSystemDirectory(Buff, nChars); string 系统路径 = "系统路径:" + Buff.ToString(); //调用GetSystemInfo函数获取CPU的相关信息 CPU_INFO CpuInfo; CpuInfo = new CPU_INFO(); ComputerInfo.GetSystemInfo(ref CpuInfo); string CPU数量 = "本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU"; string CPU的类型 = "CPU的类型为" + CpuInfo.dwProcessorType.ToString(); string CPU等级 = "CPU等级为" + CpuInfo.dwProcessorLevel.ToString(); string CPU的OEM = "CPU的OEM ID为" + CpuInfo.dwOemId.ToString(); string CPU中的页面 = "CPU中的页面大小为" + CpuInfo.dwPageSize.ToString(); //调用GlobalMemoryStatus函数获取内存的相关信息 MEMORY_INFO MemInfo; MemInfo = new MEMORY_INFO(); ComputerInfo.GlobalMemoryStatus(ref MemInfo); string 内存正在使用 = MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用"; string 物理内存 = "物理内存共有" + MemInfo.ullTotalPhys.ToString() + "字节"; string 可使用的物理内存 = "可使用的物理内存有" + MemInfo.ullAvailPhys.ToString() + "字节"; string 交换文件总大小 = "交换文件总大小为" + MemInfo.ullTotalPageFile.ToString() + "字节"; string 尚可交换文件大小 = "尚可交换文件大小为" + MemInfo.ullAvailPageFile.ToString() + "字节"; string 总虚拟内存 = "总虚拟内存有" + MemInfo.ullTotalVirtual.ToString() + "字节"; string 未用虚拟内存 = "未用虚拟内存有" + MemInfo.ullAvailVirtual.ToString() + "字节"; //调用GetSystemTime函数获取系统时间信息 SYSTEMTIME_INFO StInfo; StInfo = new SYSTEMTIME_INFO(); ComputerInfo.GetSystemTime(ref StInfo); string 时间 = StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日"; string 时间2 = (StInfo.wHour + 8).ToString() + "点" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒"; }
//群消息 private void btnSendMsgAll_Click(object sender, EventArgs e) { string time; foreach (ConnectionClient conn in dictConn.Values) { conn.Send(txtInput.Text.Trim()); } SYSTEMTIME_INFO StInfo; StInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref StInfo); time = StInfo.wYear.ToString() + "/" + StInfo.wMonth.ToString() + "/" + StInfo.wDay.ToString() + " "; time += (StInfo.wHour + 8).ToString() + ":" + StInfo.wMinute.ToString(); txtLog.AppendText(time + "rn"); txtLog.AppendText("群发消息:" + txtInput.Text.Trim() + "rn"); txtInput.Text = ""; }
/// <summary> /// 调用API获取系统相关信息 /// </summary> /// <returns></returns> public string initAPIData() { string info = ""; //获取Windows路径 const int nChars = 128; StringBuilder buff = new StringBuilder(nChars); GetWindowsDirectory(buff, nChars); info = string.Format("Windows路径:{0}\n", buff.ToString()); //获取系统路径 GetSystemDirectory(buff, nChars); info += string.Format("系统路径:{0}\n", buff.ToString()); //获取CPU信息 CPU_INFO cpuInfo = new CPU_INFO(); GetSystemInfo(ref cpuInfo); info += string.Format( "\n本机中有{0}个CPU\nCPU类型为:{1}\nCPU等级为:{2}\nCPU的OEM ID为:{3}CPU的页面大小为:{4}\n", cpuInfo.dwNumOFProcessors.ToString(), cpuInfo.dwProcessorType.ToString(), cpuInfo.dwProcessorLevel.ToString(), cpuInfo.dwOemId.ToString(), cpuInfo.dwPageSize.ToString()); //获取内存信息 MEMORY_INFO memInfo = new MEMORY_INFO(); GlobalMemoryStatus(ref memInfo); info += string.Format("内存正在使用{0}K\n", Math.Round(memInfo.dwMemoryLoad / (1.0 * 1024), 2).ToString()); info += string.Format("物理内存共有{0}M\n", Math.Round(memInfo.dwTotalPhys / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("可使用的物理内存共有{0}M\n", Math.Round(memInfo.dwAvailPhys / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("交换文件总大小为{0}M\n", Math.Round(memInfo.dwTotalPageFile / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("尚可交换文件大小为{0}M\n", Math.Round(memInfo.dwAvailPageFile / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("总虚拟内存为{0}M\n", Math.Round(memInfo.dwTotalVirtual / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("未用虚拟内存为{0}M\n", Math.Round(memInfo.dwAvailVirtual / (1.0 * 1024 * 1024), 2).ToString()); //获取系统时间信息 SYSTEMTIME_INFO stInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref stInfo); info += string.Format("\n系统时间:{0}年{1}月{2}日 星期{6} {3}时{4}分{5}秒", stInfo.wYear, stInfo.wMonth, stInfo.wDay, stInfo.wDayOfWeek, stInfo.wHour, stInfo.wMinute, stInfo.wMilliseconds); return(info); }
/// <summary> /// 调用API获取系统相关信息 /// </summary> /// <returns></returns> public string initAPIData() { string info = ""; //获取Windows路径 const int nChars = 128; StringBuilder buff = new StringBuilder(nChars); GetWindowsDirectory(buff, nChars); info = string.Format("Windows路径:{0}\n", buff.ToString()); //获取系统路径 GetSystemDirectory(buff, nChars); info += string.Format("系统路径:{0}\n", buff.ToString()); //获取CPU信息 CPU_INFO cpuInfo = new CPU_INFO(); GetSystemInfo(ref cpuInfo); info += string.Format( "\n本机中有{0}个CPU\nCPU类型为:{1}\nCPU等级为:{2}\nCPU的OEM ID为:{3}CPU的页面大小为:{4}\n", cpuInfo.dwNumOFProcessors.ToString(), cpuInfo.dwProcessorType.ToString(), cpuInfo.dwProcessorLevel.ToString(), cpuInfo.dwOemId.ToString(), cpuInfo.dwPageSize.ToString()); //获取内存信息 MEMORY_INFO memInfo = new MEMORY_INFO(); GlobalMemoryStatus(ref memInfo); info += string.Format("内存正在使用{0}K\n",Math.Round(memInfo.dwMemoryLoad / (1.0*1024),2).ToString()); info += string.Format("物理内存共有{0}M\n", Math.Round(memInfo.dwTotalPhys / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("可使用的物理内存共有{0}M\n", Math.Round(memInfo.dwAvailPhys / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("交换文件总大小为{0}M\n", Math.Round(memInfo.dwTotalPageFile / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("尚可交换文件大小为{0}M\n", Math.Round(memInfo.dwAvailPageFile / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("总虚拟内存为{0}M\n", Math.Round(memInfo.dwTotalVirtual / (1.0 * 1024 * 1024), 2).ToString()); info += string.Format("未用虚拟内存为{0}M\n", Math.Round(memInfo.dwAvailVirtual / (1.0 * 1024 * 1024), 2).ToString()); //获取系统时间信息 SYSTEMTIME_INFO stInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref stInfo); info += string.Format("\n系统时间:{0}年{1}月{2}日 星期{6} {3}时{4}分{5}秒", stInfo.wYear, stInfo.wMonth, stInfo.wDay, stInfo.wDayOfWeek, stInfo.wHour, stInfo.wMinute, stInfo.wMilliseconds); return info; }
/// <summary> /// 被线程调用 监听连接端口 /// </summary> void StartWatch() { string recode; while (isWatch) { //threadWatch.SetApartmentState(ApartmentState.STA); //监听 客户端 连接请求,但是,Accept会阻断当前线程 Socket sokMsg = sokWatch.Accept();//监听到请求,立即创建负责与该客户端套接字通信的套接字 ConnectionClient connection = new ConnectionClient(sokMsg, ShowMsg, RemoveClientConnection); //将负责与当前连接请求客户端 通信的套接字所在的连接通信类 对象 装入集合 dictConn.Add(sokMsg.RemoteEndPoint.ToString(), connection); //将 通信套接字 加入 集合,并以通信套接字的远程IpPort作为键 //dictSocket.Add(sokMsg.RemoteEndPoint.ToString(), sokMsg); //将 通信套接字的 客户端IP端口保存在下拉框里 cboClient.Items.Add(sokMsg.RemoteEndPoint.ToString()); //MessageBox.Show("有一个客户端新添加!"); recode = sokMsg.RemoteEndPoint.ToString(); //调用GetSystemTime函数获取系统时间信息 SYSTEMTIME_INFO StInfo; StInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref StInfo); recode += "子计算机在" + StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日"; recode += (StInfo.wHour + 8).ToString() + "点" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒" + "连接服务"; //记录每台子计算机连接服务主机的日志 StreamWriter m_sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + @"\file.DAT", true); m_sw.WriteLine(recode); m_sw.WriteLine("------------------------------------------------------------------"); m_sw.Close(); //MessageBox.Show(recode); dictConn[sokMsg.RemoteEndPoint.ToString()].SendTrue(); //启动一个新线程,负责监听该客户端发来的数据 //Thread threadConnection = new Thread(ReciveMsg); //threadConnection.IsBackground = true; //threadConnection.Start(sokMsg); } }
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
public static extern void GetSystemTime(ref SYSTEMTIME_INFO sysInfo);
//发送消息 到指定的客户端 private void btnSend_Click(object sender, EventArgs e) { //byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(txtInput.Text.Trim()); //从下拉框中 获得 要哪个客户端发送数据 string time; string connectionSokKey = cboClient.Text; if (!string.IsNullOrEmpty(connectionSokKey)) { //从字典集合中根据键获得 负责与该客户端通信的套接字,并调用send方法发送数据过去 dictConn[connectionSokKey].Send(txtInput.Text.Trim()); SYSTEMTIME_INFO StInfo; StInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref StInfo); time = StInfo.wYear.ToString() + "/" + StInfo.wMonth.ToString() + "/" + StInfo.wDay.ToString() + " "; time += (StInfo.wHour + 8).ToString() + ":" + StInfo.wMinute.ToString(); txtLog.AppendText(time + "rn"); txtLog.AppendText("对" + cboClient.Text + "说:" + txtInput.Text.Trim() + "rn"); txtInput.Text = ""; //sokMsg.Send(arrMsg); } else { MessageBox.Show("请选择要发送的子计算机~~"); } }
public List<string> initAPIData() { List<string> lstAPI = new List<string>(); CPU_INFO CpuInfo; CpuInfo = new CPU_INFO(); GetSystemInfo(ref CpuInfo); //CPU��Ϣ�Ķ�ȡ�Ǵ����,�ұ�ֻ��һ��CPU,���������� try { lstAPI.Add("CPU����:" + CpuInfo.dwNumberOfProcessors.ToString()); } catch { } try { lstAPI.Add("CPU����:" + CpuInfo.dwProcessorType.ToString()); } catch { } try { lstAPI.Add("CPU�ȼ�:" + CpuInfo.dwProcessorLevel.ToString()); } catch { } try { lstAPI.Add("CPU�е�ҳ���С:" + CpuInfo.dwPageSize.ToString()); } catch { } //����GlobalMemoryStatus������ȡ�ڴ�������Ϣ MEMORY_INFO MemInfo; MemInfo = new MEMORY_INFO(); GlobalMemoryStatus(ref MemInfo); try { lstAPI.Add("����ʹ���ڴ�ռ���ڴ�ٷֱ�:" + MemInfo.dwMemoryLoad.ToString() + "%"); } catch { } try { long dwTotalPhys = MemInfo.dwTotalPhys; dwTotalPhys = dwTotalPhys / 1000000; lstAPI.Add("�����ڴ湲��:" + dwTotalPhys.ToString() + " ��"); } catch { } try { long dwAvailPhys = MemInfo.dwAvailPhys; dwAvailPhys = dwAvailPhys / 1000000; lstAPI.Add("��ʹ�õ������ڴ�:" + dwAvailPhys.ToString() + " ��"); } catch { } try { long dwTotalPageFile = MemInfo.dwTotalPageFile; dwTotalPageFile = dwTotalPageFile / 1000000; lstAPI.Add("�����ļ��ܴ�С:" + dwTotalPageFile.ToString() + " ��"); } catch { } try { long dwAvailPageFile = MemInfo.dwAvailPageFile; dwAvailPageFile = dwAvailPageFile / 1000000; lstAPI.Add("�пɽ����ļ���С:" + dwAvailPageFile.ToString() + " ��"); } catch { } try { long dwTotalVirtual = MemInfo.dwTotalVirtual; dwTotalVirtual = dwTotalVirtual / 1000000; lstAPI.Add("�������ڴ�:" + dwTotalVirtual.ToString() + " ��"); } catch { } try { long dwAvailVirtual = MemInfo.dwAvailVirtual; dwAvailVirtual = dwAvailVirtual / 1000000; lstAPI.Add("δ�������ڴ�:" + dwAvailVirtual.ToString() + " ��"); } catch { } //����GetSystemTime������ȡϵͳʱ����Ϣ SYSTEMTIME_INFO StInfo; StInfo = new SYSTEMTIME_INFO(); GetSystemTime(ref StInfo); lstAPI.Add("������ʱ��:" + StInfo.wYear.ToString() + "��" + StInfo.wMonth.ToString() + "��" + StInfo.wDay.ToString() + "��" + (StInfo.wHour + 8).ToString() + "��" + StInfo.wMinute.ToString() + "��" + StInfo.wSecond.ToString() + "��"); return lstAPI; }