/** * 有客户端连接 **/ public void ClientAccepted(IAsyncResult ar) { if (hostSocket == null) { GC.Collect(); GC.WaitForPendingFinalizers(); } else { var sokWatch = ar.AsyncState as Socket; //这就是客户端的Socket实例,我们后续可以将其保存起来 var client = sokWatch.EndAccept(ar); dictCounter++;//从1开始标识 clientarry[dictCounter] = client; //将client保存到dictionary dictsocket.Add(dictCounter, client); //将client显示在listbox中 net_listBox1.Items.Add(dictCounter + ":" + client.RemoteEndPoint); //成功连接的客户端数量显示在状态栏 clientCounter++; showTip1.Text = "建立连接." + clientCounter; //接收客户端的消息 threadMsg[dictCounter] = new Thread(ReceiveMessage0); canshu aa = new canshu(); isRec[dictCounter] = true; aa.counter = dictCounter; threadMsg[dictCounter].IsBackground = true; threadMsg[dictCounter].Start((object)aa); //准备接受下一个客户端请求 sokWatch.BeginAccept(new AsyncCallback(ClientAccepted), sokWatch); } }
/** * 客户端接入服务器 **/ public void ClientAccepted0() { while (isWatch) { if (!isWatch) { GC.WaitForPendingFinalizers(); } else { //这就是客户端的Socket实例,我们后续可以将其保存起来 try { var client = hostSocket.Accept(); dictCounter++;//从1开始标识 clientarry[dictCounter] = client; //将client保存到dictionary dictsocket.Add(dictCounter, client); //将client显示在listbox中 net_listBox1.Items.Add(dictCounter + ":" + client.RemoteEndPoint); //将连接的客户端创建tabpage //d = new addDelegate(CreatTabpage); // Thread t = new Thread(new ThreadStart(add)); //t.Start(); //成功连接的客户端数量显示在状态栏 clientCounter++; showTip2.Text = "建立连接." + clientCounter; //接收客户端的消息 threadMsg[dictCounter] = new Thread(ReceiveMessage0); canshu aa = new canshu(); isRec[dictCounter] = true; aa.counter = dictCounter; threadMsg[dictCounter].IsBackground = true; threadMsg[dictCounter].Start((object)aa); } catch { return; } } } }
/** * 检测是否收到数据/客户端掉线 **/ public void ReceiveMessage0(object o) { canshu aa = (canshu)o; SaveTime[aa.counter] = System.DateTime.Now.ToString("HHmmss"); while (isRec[aa.counter]) { if (!dictsocket[dictCounter].Connected) { isRec[aa.counter] = false; //从list中移除选中的选项 net_listBox1.Items.Remove(aa.counter + ":" + clientarry[aa.counter].RemoteEndPoint); //从dictionary中移除选中项 dictsocket[aa.counter].Close(); dictsocket.Remove(aa.counter); clientCounter--; showTip1.Text = "建立连接." + clientCounter; } else { try { byte[] arrMsg = new byte[1024 * 1024]; //接收对应客户端发来的消息 int length = dictsocket[aa.counter].Receive(arrMsg); ccflag++; //保存原始数据 FlagEnd[aa.counter] = SaveTxt(arrMsg, length, aa.counter); if (FlagEnd[aa.counter]) //结束标识 { SaveTime[aa.counter] = System.DateTime.Now.ToString("HHmmss"); } lengthSum[aa.counter] += length; //将接收到的消息数组里真实消息转成字符串(关键函数) massageDeal(arrMsg, length); } catch { if (isRec[aa.counter]) { isRec[aa.counter] = false; net_listBox1.Items.Remove(aa.counter + ":" + dictsocket[aa.counter].RemoteEndPoint); dictsocket[aa.counter].Close(); dictsocket.Remove(aa.counter); clientCounter--; showTip2.Text = "建立连接." + clientCounter; } } } } }
double GetP(canshu can, double time) { return(can.beta0 + can.beta1 * time); }
public void ReceiveMessage0(object o) { canshu aa = (canshu)o; SaveTime[aa.counter] = System.DateTime.Now.ToString("HHmmss"); while (isRec[aa.counter]) { if (!dictsocket[dictCounter].Connected) { isRec[aa.counter] = false; //从list中移除选中的选项 listBox1.Items.Remove(aa.counter + ":" + clientarry[aa.counter].RemoteEndPoint); //从tab中移除 tabControl1.TabPages.Remove(pagearry[aa.counter]); //从dictionary中移除选中项 dictsocket[aa.counter].Close(); dictsocket.Remove(aa.counter); clientCounter--; txtStatus2.Text = "建立连接." + clientCounter; } else { try { byte[] arrMsg = new byte[1024 * 1024]; //接收 对应 客户端发来的消息 int length = dictsocket[aa.counter].Receive(arrMsg); ccflag++; //保存原始数据 FlagEnd[aa.counter] = SaveTxt(OriginalDataFolder + "\\" + SaveTime[aa.counter] + "." + aa.counter, arrMsg, length, aa.counter); if (FlagEnd[aa.counter]) //结束标识 { SaveTime[aa.counter] = System.DateTime.Now.ToString("HHmmss"); } lengthSum[aa.counter] += length; //将接收到的消息数组里真实消息转成字符串 string strMsg = System.Text.Encoding.Default.GetString(arrMsg, 0, length); //通过委托 显示消息到 窗体的文本框 boxarry[aa.counter].AppendText(strMsg); statuslabelarry[aa.counter].Text = "接收了" + lengthSum[aa.counter] + "字节."; } catch { if (isRec[aa.counter]) { isRec[aa.counter] = false; //从list中移除选中的选项 listBox1.Items.Remove(aa.counter + ":" + dictsocket[aa.counter].RemoteEndPoint); //从tab中移除 tabControl1.TabPages.Remove(pagearry[aa.counter]); //从dictionary中移除选中项 dictsocket[aa.counter].Close(); dictsocket.Remove(aa.counter); clientCounter--; txtStatus2.Text = "建立连接." + clientCounter; } } } } }
//创建tabpage public void CreatTabpage() { //创建button Button buttontemp = new Button(); buttontemp.Text = "清空数据"; canshu aa = new canshu(); aa.counter = dictCounter; EventArgs e = new EventArgs(); buttontemp.Click += new EventHandler(buttontemp_Click); buttonarry[dictCounter] = buttontemp; //创建状态栏 ToolStripStatusLabel Labeltemp = new ToolStripStatusLabel(); Labeltemp.Name = "label" + dictCounter; Labeltemp.Text = "label" + dictCounter; Labeltemp.Size = new System.Drawing.Size(20, 30); Labeltemp.Spring = true; statuslabelarry[dictCounter] = Labeltemp; //创建状态栏 ToolStrip striptemp = new ToolStrip(); //statustemp.Location = new System.Drawing.Point(500, 309); striptemp.Dock = System.Windows.Forms.DockStyle.Bottom; striptemp.Name = "status" + dictCounter; striptemp.Size = new System.Drawing.Size(20, 30); striptemp.Items.Add(statuslabelarry[dictCounter]); toolstriparry[dictCounter] = striptemp; //创建窗口 TextBox boxtemp = new TextBox(); boxtemp.Location = new System.Drawing.Point(100, 3); boxtemp.Name = "textBox" + dictCounter; boxtemp.Multiline = true; boxtemp.ScrollBars = System.Windows.Forms.ScrollBars.Both; boxtemp.Size = new System.Drawing.Size(100, 70); //boxtemp.TabIndex = dictCounter; boxtemp.Text = "this is textbox" + dictCounter; boxtemp.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); //boxtemp.Dock = System.Windows.Forms.DockStyle.Fill; boxarry[dictCounter] = boxtemp; //创建tab页面 TabPage pagetemp = new System.Windows.Forms.TabPage(); //pagetemp.Size = new System.Drawing.Size(300, 100); pagetemp.Name = "tabPage" + dictCounter; pagetemp.Text = dictCounter + ":" + clientarry[dictCounter].RemoteEndPoint.ToString(); pagetemp.UseVisualStyleBackColor = true; pagetemp.AutoScroll = true; pagetemp.Controls.Add(boxarry[dictCounter]); //添加textbox pagetemp.Controls.Add(toolstriparry[dictCounter]); //添加状态栏 pagetemp.Controls.Add(buttonarry[dictCounter]); //添加button pagearry[dictCounter] = pagetemp; tabControl1.Controls.Add(pagearry[dictCounter]);//添加tabpage tabControl1.Controls.SetChildIndex(pagearry[dictCounter], dictCounter); //显示刚连接的窗口 tabControl1.SelectTab("tabpage" + dictCounter); }