一、 窗体布局
效果图如下
代码如下:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Closed="Window_Closed">
<Grid>
<Border BorderThickness="2" BorderBrush="Black" Margin="26,30,181,359">
<TextBox x:Name="MsgBox" Width="600" Height="30"></TextBox>
</Border>
<Border BorderThickness="2" BorderBrush="Black" Margin="640,30,26,359">
<Button Click="Button_Click">发送</Button>
</Border>
<StackPanel x:Name="MsgWindow" Width="740" Margin="21,95,21,27">
</StackPanel>
</Grid>
</Window>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
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;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//循环用到socket
public static Socket socket = null;
//发送消息需要提供
public IPEndPoint sendPoint = new IPEndPoint(IPAddress.Broadcast, 12345);
public MainWindow()
{
InitializeComponent();
//1.创建Socket对象
socket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
List<IPAddress>iPAddresses=GetIPAddresses();
foreach (IPAddress ip in iPAddresses)
{
Console.WriteLine(ip.ToString());
}
//3.绑定ip和端口
socket.Bind(new IPEndPoint(iPAddresses[0], 12345));
//4.设置socket
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
//5.启动一个任务,Task中传一个action 构建方法DisplayMsg
Action action = new Action(DisplayMsg);
Task task = new Task(action);
task.Start();
}
private void DisplayMsg()
{
//10.创建action对象 13.通过action来对界面进行更新
Action<string,string> action= new Action<string, string>(UIupdate);
//6.接收信息 首先要有接收地址
EndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 12345);
//7.循环Task一直可以接收消息
while (true)
{
//8.需要一个缓冲区
byte[] vs = new byte[1024];
//参数 缓冲区 获取到的内容保存到了iPEndPoint
socket.ReceiveFrom(vs,ref iPEndPoint);
//11.对ip地址进行转换
IPAddress address=((IPEndPoint)iPEndPoint).Address;
//12.缓冲区进行转换
String p = Encoding.UTF8.GetString(vs);
//9.UI界面的内容和获取到的内容在不同的线程中,所以要跨线程调用
this.Dispatcher.Invoke(action,address.ToString(),p);//在这里执行action,获取ip和内容
}
}
//14.对ui线程进行操作
private void UIupdate(string arg1, string arg2)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(arg1);
stringBuilder.Append(":");
stringBuilder.Append(arg2);
Label label = new Label();
label.Content = stringBuilder.ToString();
MsgWindow.Children.Add(label);
}
//2.获取ip地址
public static List<IPAddress> GetIPAddresses()
{
List<IPAddress> iPAddresses=new List<IPAddress>();
foreach(IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if(ip.AddressFamily.ToString()== "InterNetwork")
{
iPAddresses.Add(ip);
}
}
return iPAddresses;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//发送消息
byte[] vs = Encoding.UTF8.GetBytes(MsgBox.Text);
socket.SendTo(vs, sendPoint);
}
private void Window_Closed(object sender, EventArgs e)
{
//释放socket
socket.Close();
Application.Current.Shutdown(0);
}
}
}