TCP通信之Socket、C#之Socket断线重连

C#之Socket断线重连
https://blog.csdn.net/thebestleo/article/details/52354126

 

 

一、Socket介绍

程序员可以通过socket来发送和接收网络上的数据。

在C#中,MS为我们提供了 System.Net.Sockets 命名空间,里面包含了Socket类。

 

二、访问网络

要想访问网络,还得有些基本的条件:

a. 要确定本机的IP和端口,socket只有与某一IP和端口绑定,才能发挥强大的威力。

b. 得有协议吧(否则谁认得你这发送到网络的是什么呀)。TCP & UDP。

 

 

  如果具备了基本的条件,就可以开始用它们访问网络了。来看看步骤吧:

 

       a. 建立一个套接字,绑定本机的IP和端口

        套件字: 就是网络通信的接口,一个IP地址 然后 接上冒号和端口。相当于家门前的邮箱。

 

       c. 如果是TCP,因为是面向连接的,所以要利用ListenO()方法来监听网络上是否有人给自己发东西;如果是UDP,因为是无连接的,所以来者不拒。

 

       d. TCP情况下,如果监听到一个连接,就可以使用accept来接收这个连接,然后就可以利用Send/Receive来执行操作了。而UDP,则不需要accept, 直接使用SendTo/ReceiveFrom来执行操作。(看清楚哦,和TCP的执行方法有区别,因为UDP不需要建立连接,所以在发送前并不知道对方的IP和端口,因此需要指定一个发送的节点才能进行正常的发送和接收)

       e. 如果你不想继续发送和接收了,就不要浪费资源了。能close的就close吧。

 

三、流程图

 

 

代码:

 

       /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.label1 = new System.Windows.Forms.Label();
            this.ServerIP = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.ServerPort = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.ClientIp = new System.Windows.Forms.ComboBox();
            this.BtnStartListener = new System.Windows.Forms.Button();
            this.BtnStopListener = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.TextRead = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.TextWrite = new System.Windows.Forms.TextBox();
            this.CheckBoxRead = new System.Windows.Forms.CheckBox();
            this.BtnReadClear = new System.Windows.Forms.Button();
            this.CheckBoxWrite = new System.Windows.Forms.CheckBox();
            this.BtnWriteClear = new System.Windows.Forms.Button();
            this.BtnWrite = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioButton2);
            this.groupBox1.Controls.Add(this.radioButton1);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(175, 53);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "TCP协议类型";
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(95, 20);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(59, 16);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.Text = "客户端";
            this.radioButton2.UseVisualStyleBackColor = true;
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Checked = true;
            this.radioButton1.Location = new System.Drawing.Point(6, 20);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(59, 16);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "服务端";
            this.radioButton1.UseVisualStyleBackColor = true;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(210, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "服务器地址:";
            // 
            // ServerIP
            // 
            this.ServerIP.Location = new System.Drawing.Point(283, 9);
            this.ServerIP.Name = "ServerIP";
            this.ServerIP.Size = new System.Drawing.Size(142, 21);
            this.ServerIP.TabI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiz198183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值