Demo entry 6746017

赵赵赵赵赵小公主

   

Submitted by anonymous on May 28, 2018 at 08:11
Language: C#. Code size: 1.9 kB.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace 广播
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        UdpClient udp;
        private async void btn_send_Click(object sender, RoutedEventArgs e)
        {
                btn_send.IsEnabled = false;
                IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast, 8888);
                byte[] bs = Encoding.Unicode.GetBytes(textBox1.Text);
                await udp.SendAsync(bs, bs.Length, ipe);
                btn_send.IsEnabled = true;
        }
       
        private async void ReceviceData(object obj)
        {
            while (true)
            {
                UdpReceiveResult result = await udp.ReceiveAsync();
                listbox1.Dispatcher.Invoke(() => listbox1.Items.Add(result.RemoteEndPoint + "说: " + Encoding.Unicode.GetString(result.Buffer) + "\t" + i++));
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            udp = new UdpClient(8888);
            ThreadPool.QueueUserWorkItem(ReceviceData);
            ThreadPool.QueueUserWorkItem(method);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            listbox1.Items.Clear();
        }
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).