📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets ;
//使用到TcpListen类
using System.Net ;
using System.Threading ;
//使用到线程
namespace sc
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Button button1;
int port = 8000 ;
//定义侦听端口号
private Thread thThreadRead ;
//创建线程,用以侦听端口号,接收信息
private bool blistener = true ;
//设定标示位,判断侦听状态
private Socket stRead ;
private TcpListener tlTcpListen;
private int count = 0;
private double num;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 144);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 0;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(258, 124);
this.listBox1.TabIndex = 1;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 338);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(290, 22);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "无连接";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Location = new System.Drawing.Point(16, 192);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(256, 144);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "同步TCP测试(传输准确性)";
//
// button3
//
this.button3.Location = new System.Drawing.Point(144, 24);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(80, 24);
this.button3.TabIndex = 6;
this.button3.Text = "计算准确率";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label4
//
this.label4.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label4.Location = new System.Drawing.Point(104, 112);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(144, 16);
this.label4.TabIndex = 5;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label2.Location = new System.Drawing.Point(104, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(144, 16);
this.label2.TabIndex = 2;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 1;
this.label1.Text = "实际接收数据m";
//
// button2
//
this.button2.Location = new System.Drawing.Point(32, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 24);
this.button2.TabIndex = 0;
this.button2.Text = "计数器清零";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 16);
this.label3.TabIndex = 4;
this.label3.Text = "准确率(m/n)";
//
// button4
//
this.button4.Location = new System.Drawing.Point(168, 144);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(72, 32);
this.button4.TabIndex = 5;
this.button4.Text = "结束监听";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label5
//
this.label5.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label5.Location = new System.Drawing.Point(104, 64);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(144, 16);
this.label5.TabIndex = 7;
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 64);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(96, 16);
this.label6.TabIndex = 8;
this.label6.Text = "实际发送数据n";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(290, 360);
this.Controls.Add(this.button4);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "TCP接收端";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void Listen ( )
{
try
{
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
tlTcpListen = new TcpListener(ipAddress, port);
//以8000端口号来初始化TcpListener实例
tlTcpListen.Start ( ) ;
//开始监听网络的连接请求
statusBar1.Text = "正在监听..." ;
stRead = tlTcpListen.AcceptSocket ( ) ;
//通过连接请求,并获得接收数据时使用的Socket实例
EndPoint tempRemoteEP = stRead.RemoteEndPoint ;
IPEndPoint tempRemoteIP = ( IPEndPoint ) tempRemoteEP ;
statusBar1.Text = "已经连接!" ;
//循环侦听
while ( blistener )
{
string sTime = DateTime.Now.ToShortTimeString ( ) ;
//获取接收数据时的时间
Byte [ ] byRead =new Byte [ 800 ] ;
int iRead = stRead.ReceiveFrom
( byRead , ref tempRemoteEP ) ;
//获得接收的字节数目
Byte [ ] byText = new Byte [ iRead ] ;
//并根据接收到的字节数目来定义字节数组
Array.Copy ( byRead , 0 , byText , 0 , iRead ) ;
//sourceArray,sourceIndex,destinationArray,destinationIndex,length
string sTemp = System.Text.Encoding.Default.
GetString ( byText ) ;
//判断是否为测试数据
if ( sTemp.Trim().StartsWith("#") )
{
string abc=sTemp;
abc = abc.Replace("#", "");
num = Convert.ToDouble(abc);
this.label5.Text = num.ToString()+"个";
count--;
}
//判断是否为断开连接控制码
if ( sTemp.Trim ( ) == "STOP" )
{
stRead.Close ( ) ;
tlTcpListen.Stop ( ) ;
//关闭侦听
statusBar1.Text = "连接已经关闭!" ;
thThreadRead.Abort ( ) ;
//中止线程
return ;
}
else
count++;
listBox1.Items.Add ( sTime + " " + sTemp +" 收到的第"+count+"个数据" ) ;
this.label2.Text=(count+"个");
}
}
catch( System.Security.SecurityException )
{
MessageBox.Show ( "侦听失败!" , "错误" ) ;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
thThreadRead = new Thread ( new ThreadStart ( Listen ) ) ;
//以Listen过程来初始化Thread实例
thThreadRead.Start ( ) ;
//启动线程
button1.Enabled = false ;
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button2_Click(object sender, System.EventArgs e)
{
count=0;
num=0;
this.label5.Text=(num+"个");
this.label2.Text=(count+"个");
this.label4.Text=("");
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
double temp=count/num*100;
this.label4.Text=(temp.ToString()+"%");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button4_Click(object sender, System.EventArgs e)
{
stRead.Close ( ) ;
tlTcpListen.Stop ( ) ;
//关闭侦听
statusBar1.Text = "连接已经关闭!" ;
thThreadRead.Abort ( ) ;
//中止线程
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -