当前位置:首页 > 技术 > .NET > 正文内容

UDP通信学习

watrt5年前 (2019-08-11).NET11580

学习了很久写一个UDP通信学习的例子

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
       public delegate void back(string str);
       UdpClient udp_server, udp_client;
       bool server_zt;
        public Form1()
        {
            InitializeComponent();
        }
        public void add()
        {
            try
            {
                IPEndPoint ipinfo = new IPEndPoint(IPAddress.Any, 1000);
                IPEndPoint ipfo = new IPEndPoint(IPAddress.Any, 0);
                udp_client = new UdpClient(ipinfo);
                Byte[] sendstr;
                while (server_zt)
                {
                    sendstr = udp_client.Receive(ref ipfo);
                    textadd(Encoding.Default.GetString(sendstr));
                }
            }
                catch(Exception err)
            {
                MessageBox.Show(err.Message,"错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        public void textadd(string str)
        {
            if (listBox1.InvokeRequired)
            {
                
                back s = new back(textadd);
                listBox1.Invoke(s, str);
            }
            else 
            {
                listBox1.Items.Add(str + "[" + DateTime.Now + "]");
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            server_zt = true;
            Thread t = new Thread(new ThreadStart(add));
            t.Start();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text.Length > 0)
                {
                    udp_server = new UdpClient();
                    Byte[] s_send = Encoding.Default.GetBytes(comboBox1.Text);
                    udp_server.Send(s_send, s_send.Length, "localhost", 1000);
                    comboBox1.Text = "";
                }
                else
                {
                    MessageBox.Show("发表内容不能为空!", "提示!",MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message,"错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}


运行界面

2294302535169630018.jpg

分享给朋友:

相关文章

C# 使用串口编程例子

C# 使用串口编程例子

使用C#调用com控件来与ESP32通信 打发用的IDE是:SharpDevelop 5.1/*  * 由SharpDevelop创建。  * 用户: xk100  * 日期: 2023/4/7  * 时间: 11:04  *   * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件  */ using...

网页定时任务windows服务解决方法

网页定时任务windows服务解决方法

在项目中需要后台运行一个定时任务,周期性的触发事件。我使用vs2015写了一个简单的服务,事定时访问网址以下是Service1.cs代码using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。