使用UDP发送数据简单实现
2016-02-23 09:54
459 查看
UDP 是无连接传输协议,所以不需要在发送和接收数据前建立远程主机连接.但是需要远程主机绑定一个端口进行监听是否是有发送端的IP和端口发送来的。发送端同样要绑定一个端口向远程IP地址的一个端口发送数据。
这里是一个基本实现,没有使用多线程以及异步等:
namespace UDPSendClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private UdpClient udpClient;
private IPEndPoint ipEndPoint;
public MainWindow()
{
InitializeComponent();
udpClient = new UdpClient(12345);
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2112);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
byte[] bytes = new byte[1024];
bytes=Encoding.Default.GetBytes(this.txbMsg.Text);
udpClient.Send(bytes, bytes.Length, ipEndPoint);
}
}
}
namespace WpfUDPReceiveClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private UdpClient udpServer;
private IPEndPoint ipEndPoint;
private int _abortThread = 0;
public MainWindow()
{
InitializeComponent();
udpServer = new UdpClient(2112);
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void HandleReceived()
{
byte[] data = null;
while(_abortThread==0)
{
data = udpServer.Receive(ref ipEndPoint);
MessageBox.Show(Encoding.Default.GetString(data));
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(HandleReceived);
thread.IsBackground = true;
thread.Start();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Interlocked.Exchange(ref _abortThread, 1);
}
}
}
这里是一个基本实现,没有使用多线程以及异步等:
namespace UDPSendClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private UdpClient udpClient;
private IPEndPoint ipEndPoint;
public MainWindow()
{
InitializeComponent();
udpClient = new UdpClient(12345);
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2112);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
byte[] bytes = new byte[1024];
bytes=Encoding.Default.GetBytes(this.txbMsg.Text);
udpClient.Send(bytes, bytes.Length, ipEndPoint);
}
}
}
namespace WpfUDPReceiveClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private UdpClient udpServer;
private IPEndPoint ipEndPoint;
private int _abortThread = 0;
public MainWindow()
{
InitializeComponent();
udpServer = new UdpClient(2112);
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void HandleReceived()
{
byte[] data = null;
while(_abortThread==0)
{
data = udpServer.Receive(ref ipEndPoint);
MessageBox.Show(Encoding.Default.GetString(data));
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(HandleReceived);
thread.IsBackground = true;
thread.Start();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Interlocked.Exchange(ref _abortThread, 1);
}
}
}
相关文章推荐
- java spring mvc时间String转Date(二)
- python爬虫(抓取百度新闻列表)
- htm5移动端开发 和 pc端开发
- Fiddler 会拉弦的程序员
- Android Weak Handler:可以避免内存泄漏的Handler库
- 视图的优缺点
- 257. Binary Tree Paths
- Java transient关键字使用
- 版本号V1*.V2*.V3*.V4* 第四位从svn versioon同步
- 深度学习edusoho之前端插件
- 2016.02.23,英语,《Vocabulary Builder》Unit 01
- CocoaPods安装使用及配置私有库
- MIME协议
- CodeForces 629C Famil Door and Brackets
- easyUI-tree
- bat脚本批量输出文件夹名
- idea Context: local file . file is included in 3 contexts
- Android--数据库数据显示至屏幕
- (五)带属性值的ng-app指令,实现自己定义模块的自己主动载入
- charles使用方法