C# udp通信测试助手-点对点-网络断开检测

1、界面和功能如下,实现功能

(1)100毫秒接收一次,超过1秒钟没有接收到目标信息,则视为连接断开

(2)接收的Soecket目标地址、端口号显示

(3)1秒钟自动发送

(4)本机地址和端口号、目标地址和端口号,从配置文件中读取。

2、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UdpTest
{
    public partial class MaiForm : Form
    {
        public MaiForm()
        {
            InitializeComponent();
        }
        //
        IniFile ini = new IniFile("./Config.ini");
        //
        bool isRecvIpShow = false;
        bool isContinueSend = false;        //是否连续发送
        bool isAutoNewLine = false;         //是否自动换行
        // 创建UdpClient实例
        Socket udpClient;
        Thread t;
        Thread t2;
        string udpRecv;

        string target_IpPort;
        long heartFlag = 0;
        long nRecvLengthSum = 0;
       

        private void MaiForm_Load(object sender, EventArgs e)
        {
            //char ch = '1';
            //string str = "";
            //str = ch.ToString();

            //string str = "1,2,3";
            //char ch1 = str[0];
            //char ch2 = str[2];
            //char ch3 = str[4];

           //long a = 6553611111111111111;

            try
            {
                //
                txtLocalIp.Text = ini.IniReadValue("系统参数", "本机IP地址");
                txtLocalPort.Text = ini.IniReadValue("系统参数", "本机端口号");
                txtSendIp.Text = ini.IniReadValue("系统参数", "目标IP地址");
                txtSendPort.Text = ini.IniReadValue("系统参数", "目标端口号");
                //
                udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpClient.Bind(new IPEndPoint(IPAddress.Parse(txtLocalIp.Text), int.Parse(txtLocalPort.Text)));

                t = new Thread(sendMsg);
                t.Start();
                t2 = new Thread(ReciveMsg);
                t2.Start();

                timer1.Start();
                target_IpPort = txtSendIp.Text + ":" + txtSendPort.Text;
                txtLocalUdpState.Text = "Udp创建成功";
            }
            catch 
            {
                txtLocalUdpState.Text = "Udp创建失败";
            }
        }

        /// <summary>
        /// 向特定ip的主机的端口发送数据报
        /// </summary>
        void sendMsg()
        {
            EndPoint point = new IPEndPoint(IPAddress.Parse(txtSendIp.Text), int.Parse(txtSendPort.Text));
            while (true)
            {
                if(isContinueSend)
                {
                    string msg = txtSend.Text;
                    udpClient.SendTo(Encoding.Default.GetBytes(msg), point);
                    Thread.Sleep(1000);
                }                
            }
        }
        /// <summary>
        /// 接收发送给本机ip对应端口号的数据报
        /// </summary>
        void ReciveMsg()
        {
            while (true)
            {
                try
                {
                    EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
                    byte[] buffer = new byte[1024];
                    int length = udpClient.ReceiveFrom(buffer, ref point);//接收数据报
                    string strRecvTemp = Encoding.UTF8.GetString(buffer, 0, length);
                    if (length > 0)
                    {
                        nRecvLengthSum += length;
                        //是否显示接收的ip地址和端口号
                        if (isRecvIpShow)
                        {
                            //是否自动换行
                            if (!isAutoNewLine)
                            {
                                udpRecv += point.ToString() + ": " + strRecvTemp;
                            }
                            else
                            {
                                udpRecv += point.ToString() + ": " + strRecvTemp + "\r\n";
                            }
                        }
                        else
                        {
                            if (!isAutoNewLine)
                            {
                                udpRecv +=  strRecvTemp;
                            }
                            else
                            {
                                udpRecv +=  strRecvTemp + "\r\n";
                            }
                        }
                        
                        

                        string str = point.ToString();
                        if (target_IpPort == str)
                        {
                            heartFlag = 0;
                        }
                    }
                    Thread.Sleep(100);
                }
                catch { }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            EndPoint point = new IPEndPoint(IPAddress.Parse(txtSendIp.Text), int.Parse(txtSendPort.Text));
            string msg = txtSend.Text;
            udpClient.SendTo(Encoding.UTF8.GetBytes(msg), point);

        }

        
        private void timer1_Tick(object sender, EventArgs e)
        {
            isAutoNewLine = checkAutoNewLine.Checked;
            isRecvIpShow = check_接收Ip地址和端口号.Checked;
            txtUdpRecv.Text = udpRecv;
            txtRecvLength.Text = nRecvLengthSum.ToString();
            if (heartFlag > 10)
            {
                txtTargetConnectState.Text = "连接断开";
            }
            else
            {
                txtTargetConnectState.Text = "连接成功";
            }
            heartFlag++;
            //if (heartFlag > 65534)
            //{
            //    heartFlag = 100;
            //}
            txtDisConnectTime.Text = (heartFlag / 10).ToString();
            
        }

        private void MaiForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(0);         //强制关闭所有线程   
            Application.Exit();
        }
        
        private void checkContinueSend_CheckedChanged(object sender, EventArgs e)
        {
            isContinueSend = checkContinueSend.Checked;
            if(isContinueSend)
            {
                btnSend.Enabled = false;
            }
            else
            {
                btnSend.Enabled = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtUdpRecv.Text = "";
            udpRecv = "";
            nRecvLengthSum = 0;
        }


    }
}

3、下载连接

https://download.csdn.net/download/panjinliang066333/89879538

相关推荐
小伍_Five1 小时前
透视网络世界:计算机网络习题的深度解析与总结【前3章】
服务器·网络·计算机网络
网络安全(king)3 小时前
网络安全攻防学习平台 - 基础关
网络·学习·web安全
李白你好3 小时前
家用无线路由器的 2.4GHz 和 5GHz
运维·网络
嵌入(师)4 小时前
嵌入式驱动开发详解21(网络驱动开发)
网络·驱动开发
柒烨带你飞4 小时前
路由器的原理
网络·智能路由器·php
xserver24 小时前
ensp 基于EASY IP的公司出口链路配置
网络·tcp/ip·智能路由器
枫零NET4 小时前
学习思考:一日三问(学习篇)之匹配VLAN
网络·学习·交换机
手心里的白日梦5 小时前
UDP传输层通信协议详解
网络·网络协议·udp
红米饭配南瓜汤5 小时前
WebRTC服务质量(11)- Pacer机制(03) IntervalBudget
网络·网络协议·音视频·webrtc·媒体