diff --git a/StuMgmServer/StuMgmServer/Server.Designer.cs b/StuMgmServer/StuMgmServer/Server.Designer.cs
index e7d47c6..db1b641 100644
--- a/StuMgmServer/StuMgmServer/Server.Designer.cs
+++ b/StuMgmServer/StuMgmServer/Server.Designer.cs
@@ -35,38 +35,34 @@
this.lblSwiSta = new System.Windows.Forms.Label();
this.btnSerSwitch = new System.Windows.Forms.Button();
this.lblPort = new System.Windows.Forms.Label();
- this.button1 = new System.Windows.Forms.Button();
- this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.tmr = new System.Windows.Forms.Timer(this.components);
+ this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.tlpAll.SuspendLayout();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tlpAll
//
- this.tlpAll.ColumnCount = 4;
- this.tlpAll.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
- this.tlpAll.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
- this.tlpAll.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
- this.tlpAll.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
+ this.tlpAll.ColumnCount = 2;
+ this.tlpAll.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 27.25581F));
+ this.tlpAll.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 72.74419F));
+ this.tlpAll.Controls.Add(this.richTextBox1, 0, 0);
this.tlpAll.Controls.Add(this.tableLayoutPanel1, 0, 0);
- this.tlpAll.Controls.Add(this.button1, 1, 0);
- this.tlpAll.Controls.Add(this.richTextBox1, 2, 0);
this.tlpAll.Dock = System.Windows.Forms.DockStyle.Fill;
this.tlpAll.Location = new System.Drawing.Point(0, 24);
this.tlpAll.Name = "tlpAll";
- this.tlpAll.RowCount = 2;
- this.tlpAll.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
- this.tlpAll.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
- this.tlpAll.Size = new System.Drawing.Size(1114, 486);
+ this.tlpAll.RowCount = 1;
+ this.tlpAll.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
+ this.tlpAll.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
+ this.tlpAll.Size = new System.Drawing.Size(1075, 413);
this.tlpAll.TabIndex = 0;
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 2;
- this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
- this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
+ this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45.8042F));
+ this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 54.1958F));
this.tableLayoutPanel1.Controls.Add(this.txtPort, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.lblSwiSta, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.btnSerSwitch, 1, 1);
@@ -77,16 +73,16 @@
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
- this.tableLayoutPanel1.Size = new System.Drawing.Size(272, 100);
+ this.tableLayoutPanel1.Size = new System.Drawing.Size(286, 100);
this.tableLayoutPanel1.TabIndex = 0;
//
// txtPort
//
this.txtPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.txtPort.Font = new System.Drawing.Font("Calibri", 10.8F);
- this.txtPort.Location = new System.Drawing.Point(139, 10);
+ this.txtPort.Location = new System.Drawing.Point(133, 10);
this.txtPort.Name = "txtPort";
- this.txtPort.Size = new System.Drawing.Size(130, 29);
+ this.txtPort.Size = new System.Drawing.Size(150, 29);
this.txtPort.TabIndex = 3;
this.txtPort.Text = "502";
this.txtPort.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
@@ -96,7 +92,7 @@
this.lblSwiSta.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblSwiSta.AutoSize = true;
this.lblSwiSta.Font = new System.Drawing.Font("Calibri", 10.8F);
- this.lblSwiSta.Location = new System.Drawing.Point(13, 63);
+ this.lblSwiSta.Location = new System.Drawing.Point(10, 63);
this.lblSwiSta.Name = "lblSwiSta";
this.lblSwiSta.Size = new System.Drawing.Size(110, 23);
this.lblSwiSta.TabIndex = 1;
@@ -106,9 +102,9 @@
//
this.btnSerSwitch.Dock = System.Windows.Forms.DockStyle.Fill;
this.btnSerSwitch.Font = new System.Drawing.Font("Calibri", 10.8F);
- this.btnSerSwitch.Location = new System.Drawing.Point(139, 53);
+ this.btnSerSwitch.Location = new System.Drawing.Point(133, 53);
this.btnSerSwitch.Name = "btnSerSwitch";
- this.btnSerSwitch.Size = new System.Drawing.Size(130, 44);
+ this.btnSerSwitch.Size = new System.Drawing.Size(150, 44);
this.btnSerSwitch.TabIndex = 2;
this.btnSerSwitch.Text = "开启服务器";
this.btnSerSwitch.UseVisualStyleBackColor = true;
@@ -119,48 +115,42 @@
this.lblPort.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblPort.AutoSize = true;
this.lblPort.Font = new System.Drawing.Font("Calibri", 10.8F);
- this.lblPort.Location = new System.Drawing.Point(23, 13);
+ this.lblPort.Location = new System.Drawing.Point(20, 13);
this.lblPort.Name = "lblPort";
this.lblPort.Size = new System.Drawing.Size(90, 23);
this.lblPort.TabIndex = 0;
this.lblPort.Text = "本地端口";
//
- // button1
- //
- this.button1.Location = new System.Drawing.Point(281, 3);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(207, 100);
- this.button1.TabIndex = 2;
- this.button1.Text = "button1";
- this.button1.UseVisualStyleBackColor = true;
- //
- // richTextBox1
- //
- this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.richTextBox1.Location = new System.Drawing.Point(559, 3);
- this.richTextBox1.Name = "richTextBox1";
- this.richTextBox1.Size = new System.Drawing.Size(272, 237);
- this.richTextBox1.TabIndex = 1;
- this.richTextBox1.Text = "";
- //
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
- this.menuStrip1.Size = new System.Drawing.Size(1114, 24);
+ this.menuStrip1.Size = new System.Drawing.Size(1075, 24);
this.menuStrip1.TabIndex = 1;
this.menuStrip1.Text = "menuStrip1";
//
// tmr
//
this.tmr.Enabled = true;
+ this.tmr.Interval = 1000;
+ this.tmr.Tick += new System.EventHandler(this.tmr_Tick);
+ //
+ // richTextBox1
+ //
+ this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
+ this.richTextBox1.Font = new System.Drawing.Font("Calibri", 10.8F);
+ this.richTextBox1.Location = new System.Drawing.Point(295, 3);
+ this.richTextBox1.Name = "richTextBox1";
+ this.richTextBox1.Size = new System.Drawing.Size(777, 407);
+ this.richTextBox1.TabIndex = 2;
+ this.richTextBox1.Text = "";
//
// Server
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(1114, 510);
+ this.ClientSize = new System.Drawing.Size(1075, 437);
this.Controls.Add(this.tlpAll);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
@@ -179,13 +169,12 @@
private System.Windows.Forms.TableLayoutPanel tlpAll;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.Label lblSwiSta;
- private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Button btnSerSwitch;
private System.Windows.Forms.Label lblPort;
private System.Windows.Forms.TextBox txtPort;
- private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer tmr;
+ private System.Windows.Forms.RichTextBox richTextBox1;
}
}
diff --git a/StuMgmServer/StuMgmServer/Server.cs b/StuMgmServer/StuMgmServer/Server.cs
index c1fbfbd..dc6f65d 100644
--- a/StuMgmServer/StuMgmServer/Server.cs
+++ b/StuMgmServer/StuMgmServer/Server.cs
@@ -13,19 +13,26 @@ namespace StuMgmServer
{
TcpConn tcpConn = new TcpConn();
Thread tAccept = null;
+ private delegate void SetTextCallback(string text);
- private delegate void SetTextCallbakc();
public Server()
{
InitializeComponent();
- //CheckForIllegalCrossThreadCalls = false;
}
-
- //具体要调用的方面
- private void callbakc()
+ ///
+ /// 委托更新界面
+ ///
+ private void setText(string text)
{
- richTextBox1.Text += tcpConn.acceptConnection() + " Connected \n";
- richTextBox1.Text += tcpConn.acpMsg() + " Disconnected \n";
+ if (richTextBox1.InvokeRequired)
+ {
+ SetTextCallback method = new SetTextCallback(setText);
+ Invoke(method, new object[] { text });
+ }
+ else
+ {
+ richTextBox1.Text += text;
+ }
}
private void btnSerSwitch_Click(object sender, EventArgs e)
@@ -37,12 +44,10 @@ namespace StuMgmServer
tcpConn.CloseServer();
else if (sFlag != true)
{
- tcpConn.OpenServer(Convert.ToInt16(txtPort.Text));
-
- //test();
- tAccept = new Thread(newTest);
+ int port = Convert.ToInt16(txtPort.Text);
+ tcpConn.OpenServer(port);
+ tAccept = new Thread(updateHistory);
tAccept.Start();
-
}
}
catch (Exception ep)
@@ -50,16 +55,23 @@ namespace StuMgmServer
MessageBox.Show(ep.Message);
}
}
- private void newTest()
+
+ private void updateHistory()
{
- SetTextCallbakc stcb = new SetTextCallbakc(callbakc);
- while (true)
+ while (tcpConn.SocketExist)
{
- Invoke(stcb);
- //callbakc();
+ setText(tcpConn.acceptConnection());
+ setText(tcpConn.acpMsg());
}
}
+ private void tmr_Tick(object sender, EventArgs e)
+ {
+ if (tcpConn.SocketExist)
+ btnSerSwitch.Text = "关闭服务器";
+ else
+ btnSerSwitch.Text = "打开服务器";
+ }
diff --git a/StuMgmServer/StuMgmServer/TcpConn.cs b/StuMgmServer/StuMgmServer/TcpConn.cs
index c08814c..8be32ec 100644
--- a/StuMgmServer/StuMgmServer/TcpConn.cs
+++ b/StuMgmServer/StuMgmServer/TcpConn.cs
@@ -56,12 +56,12 @@ namespace StuMgmServer
#endregion
#region 接收客户端连接
- public EndPoint acceptConnection()
+ public string acceptConnection()
{
try
{
socketClient = socket.Accept(); // 阻塞等待客户端连接
- return socketClient.RemoteEndPoint;
+ return socketClient.RemoteEndPoint.ToString() + " 已连接 \n";
}
catch (Exception)
{
@@ -70,20 +70,26 @@ namespace StuMgmServer
}
#endregion
+ const int recvTimeOut = 3000; // 设置接收超时时间
#region 接收数据
public string acpMsg()
{
byte[] arrDataRecv = new byte[1024]; // 定义接收数组
- string reEdPoint = socketClient.RemoteEndPoint.ToString();
+ string reEdPoint = "";
try
{
+ reEdPoint = socketClient.RemoteEndPoint.ToString();
+ socketClient.ReceiveTimeout = recvTimeOut;
int len = socketClient.Receive(arrDataRecv);
List listDataRecv = new List { }; // 定义截取列表
- return reEdPoint + " " + len.ToString();
+ return reEdPoint + " " + len.ToString() + " 断开连接 \n";
}
catch // 客户端断开连接
{
- return reEdPoint;
+ if (socketClient != null)
+ return reEdPoint + " 断开连接 \n";
+ else
+ return null;
}
finally
{