資源簡介
C#網(wǎng)絡(luò)編程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(“發(fā)送失??!“);
????????????}
????????}
????????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();
????????}
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????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個文件信息
評論
共有 條評論