• 大小: 76KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: C#
  • 标签: 广播  异步编程  C#  

资源简介

C#网络编程UDP广播 广播 异步编程 C#

资源截图

代码片段和文件信息

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 广播
{
    public partial class Form1 : Form
    {
        UdpClient client;
        int port;
        volatile bool isStopped;
        public Form1()
        {
            InitializeComponent();
            port = 8888;
            client = new UdpClient(port);
            isStopped = false;
        }

        private async void buttonSend_ClickAsync(object sender EventArgs e)
        {
            try
            {
                byte[] b = Encoding.Unicode.GetBytes(TextBoxMessage.Text);
                IPEndPoint remote = new IPEndPoint(IPAddress.Broadcast port);
                await client.SendAsync(b b.Length remote);
                TextBoxMessage.Clear();
            }
            catch
            {
                MessageBox.Show(“发送失败!“);
            }
        }


        private async void ReceiveData()
        {

            while (!isStopped)
            {
                try
                {
                    Task h = client.ReceiveAsync();
                    UdpReceiveResult result = await h;
                    string message = Encoding.Unicode.GetString(result.Buffer);
                    IPEndPoint remote = result.RemoteEndPoint;
                    showMessageDel d = new showMessageDel(ShowMessage);
                    ListBox.Invoke(d remote.ToString() + “说:“ + message.ToString());
                }
                catch
                {
                    break;
                }
            }
        }
        private delegate void showMessageDel(string str);
        private void ShowMessage(string str)
        {

            ListBox.Items.Add(str);

        }

        private void Form1_Load(object sender EventArgs e)
        {
            Thread h = new Thread(ReceiveData);
            h.IsBackground = true;
            h.Start();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-05-16 13:52  广播\
     目录           0  2017-05-17 11:21  广播\.vs\
     文件          37  2017-05-17 10:56  广播\.vs\ProjectSettings.json
     文件      258048  2017-05-17 11:21  广播\.vs\slnx.sqlite
     目录           0  2017-05-16 13:52  广播\.vs\广播\
     目录           0  2017-05-16 13:52  广播\.vs\广播\v15\
     文件       17920  2017-05-17 10:56  广播\.vs\广播\v15\.suo
     目录           0  2017-05-17 11:21  广播\广播\
     文件         189  2017-05-16 13:52  广播\广播\App.config
     文件        3745  2017-05-17 11:20  广播\广播\Form1.Designer.cs
     文件        2227  2017-05-17 11:21  广播\广播\Form1.cs
     文件        5817  2017-05-17 11:18  广播\广播\Form1.resx
     文件         518  2017-05-16 13:52  广播\广播\Program.cs
     目录           0  2017-05-16 13:52  广播\广播\Properties\
     文件        1310  2017-05-16 13:52  广播\广播\Properties\AssemblyInfo.cs
     文件        2825  2017-05-16 13:52  广播\广播\Properties\Resources.Designer.cs
     文件        5612  2017-05-16 13:52  广播\广播\Properties\Resources.resx
     文件        1093  2017-05-16 13:52  广播\广播\Properties\Settings.Designer.cs
     文件         249  2017-05-16 13:52  广播\广播\Properties\Settings.settings
     目录           0  2017-05-17 10:56  广播\广播\bin\
     目录           0  2017-05-17 11:18  广播\广播\bin\Debug\
     文件       12288  2017-05-17 11:20  广播\广播\bin\Debug\广播.exe
     文件         189  2017-05-16 13:52  广播\广播\bin\Debug\广播.exe.config
     文件       24064  2017-05-17 11:20  广播\广播\bin\Debug\广播.pdb
     目录           0  2017-05-17 11:22  广播\广播\bin\Release\
     目录           0  2017-05-17 10:56  广播\广播\obj\
     目录           0  2017-05-17 11:18  广播\广播\obj\Debug\
     文件          42  2017-05-17 10:56  广播\广播\obj\Debug\CoreCompileInputs.cache
     文件         868  2017-05-16 13:52  广播\广播\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7280  2017-05-17 10:56  广播\广播\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2017-05-17 11:22  广播\广播\obj\Debug\TempPE\
............此处省略13个文件信息

评论

共有 条评论