与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
原文:与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
作者:webabcd
介绍
与众不同 windows phone 7.5 (sdk 7.1) 之通信
- 实例 - 基于 Socket UDP 开发一个多人聊天室
示例
1、服务端
Main.cs
/*
* Socket UDP 聊天室的服务端
*
* 注:udp 报文(Datagram)的最大长度为 65535(包括报文头)
*/ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO; namespace SocketServerUdp
{
public partial class Main : Form
{
SynchronizationContext _syncContext; System.Timers.Timer _timer; // 客户端终结点集合
private List<IPEndPoint> _clientList = new List<IPEndPoint>(); public Main()
{
InitializeComponent(); // UI 线程
_syncContext = SynchronizationContext.Current; // 启动后台线程接收数据
Thread thread = new Thread(new ThreadStart(ReceiveData));
thread.IsBackground = true;
thread.Start(); // 每 10 秒运行一次计时器所指定的方法,群发信息
_timer = new System.Timers.Timer();
_timer.Interval = 10000d;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
} // 接收数据
private void ReceiveData()
{
// 实例化一个 UdpClient,监听指定端口,用于接收信息
UdpClient listener = new UdpClient();
// 客户端终结点
IPEndPoint clientEndPoint = null; try
{
while (true)
{
// 一直等待,直至接收到数据为止(可以获得接收到的数据和客户端终结点)
byte[] bytes = listener.Receive(ref clientEndPoint);
string strResult = Encoding.UTF8.GetString(bytes); OutputMessage(strResult); // 将发送此信息的客户端加入客户端终结点集合
if (!_clientList.Any(p => p.Equals(clientEndPoint)))
_clientList.Add(clientEndPoint);
}
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// 每 10 秒群发一次信息
SendData(string.Format("webabcd 对所有人说:大家好! 【信息来自服务端 {0}】", DateTime.Now.ToString("hh:mm:ss")));
} // 发送数据
private void SendData(string data)
{
// 向每一个曾经向服务端发送过信息的客户端发送信息
foreach (IPEndPoint ep in _clientList)
{
// 实例化一个 UdpClient,用于发送信息
UdpClient udpClient = new UdpClient(); try
{
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
// 发送信息到指定的客户端终结点,并返回发送的字节数
int count = udpClient.Send(byteData, byteData.Length, ep);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
} // 关闭 UdpClient
// udpClient.Close();
}
} // 在 UI 上输出指定信息
private void OutputMessage(string data)
{
_syncContext.Post((p) => { txtMsg.Text += p.ToString() + "\r\n"; }, data);
}
}
}
2、客户端
UdpPacketEventArgs.cs
/*
* 演示 ASM 和 SSM 用
*/ using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; namespace Demo.Communication.SocketClient
{
public class UdpPacketEventArgs : EventArgs
{
// UDP 包的内容
public string Message { get; set; }
// UDP 包的来源的 IPEndPoint
public IPEndPoint Source { get; set; } public UdpPacketEventArgs(byte[] data, IPEndPoint source)
{
this.Message = System.Text.Encoding.UTF8.GetString(data, , data.Length);
this.Source = source;
}
}
}
UdpDemo.xaml
<phone:PhoneApplicationPage
x:Class="Demo.Communication.SocketClient.UdpDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel HorizontalAlignment="Left"> <ScrollViewer x:Name="svChat" Height="400">
<TextBlock x:Name="txtChat" TextWrapping="Wrap" />
</ScrollViewer> <TextBox x:Name="txtName" />
<TextBox x:Name="txtInput" KeyDown="txtInput_KeyDown" />
<Button x:Name="btnSend" Content="发送" Click="btnSend_Click" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>
UdpDemo.xaml.cs
/*
* Socket UDP 聊天室的客户端
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using System.Net.Sockets;
using System.Text;
using System.Threading; namespace Demo.Communication.SocketClient
{
public partial class UdpDemo : PhoneApplicationPage
{
// 客户端 Socket
private Socket _socket; // 用于发送数据的 Socket 异步操作对象
private SocketAsyncEventArgs _socketAsyncSend; // 用于接收数据的 Socket 异步操作对象
private SocketAsyncEventArgs _socketAsyncReceive; // 是否已发送过数据
private bool _sent = false; private ManualResetEvent _signalSend = new ManualResetEvent(false); public UdpDemo()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(UdpDemo_Loaded);
} void UdpDemo_Loaded(object sender, RoutedEventArgs e)
{
// 初始化姓名和需要发送的默认文字
txtName.Text = "匿名用户" + new Random().Next(, ).ToString().PadLeft(, '');
txtInput.Text = "hi"; // 实例化 Socket
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // 实例化 SocketAsyncEventArgs ,用于发送数据
_socketAsyncSend = new SocketAsyncEventArgs();
// 服务端的 EndPoint
_socketAsyncSend.RemoteEndPoint = new DnsEndPoint("192.168.8.217", );
// 异步操作完成后执行的事件
_socketAsyncSend.Completed += new EventHandler<SocketAsyncEventArgs>(_socketAsyncSend_Completed);
} public void SendData()
{
byte[] payload = Encoding.UTF8.GetBytes(txtName.Text + ":" + txtInput.Text);
// 设置需要发送的数据的缓冲区
_socketAsyncSend.SetBuffer(payload, , payload.Length); _signalSend.Reset(); // 无信号 // 异步地向服务端发送信息(SendToAsync - UDP;SendAsync - TCP)
_socket.SendToAsync(_socketAsyncSend); _signalSend.WaitOne(); // 阻塞
} void _socketAsyncSend_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError != SocketError.Success)
{
OutputMessage(e.SocketError.ToString());
} _signalSend.Set(); // 有信号 if (!_sent)
{
_sent = true;
// 注:只有发送过数据,才能接收数据,否则 ReceiveFromAsync() 时会出现异常
ReceiveData();
}
} // 接收信息
public void ReceiveData()
{
// 实例化 SocketAsyncEventArgs,并指定端口,以接收数据
_socketAsyncReceive = new SocketAsyncEventArgs();
_socketAsyncReceive.RemoteEndPoint = new IPEndPoint(IPAddress.Any, ); // 设置接收数据的缓冲区,udp 报文(Datagram)的最大长度为 65535(包括报文头)
_socketAsyncReceive.SetBuffer(new Byte[], , );
_socketAsyncReceive.Completed += new EventHandler<SocketAsyncEventArgs>(_socketAsyncReceive_Completed); // 异步地接收数据(ReceiveFromAsync - UDP;ReceiveAsync - TCP)
_socket.ReceiveFromAsync(_socketAsyncReceive);
} void _socketAsyncReceive_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
// 接收数据成功,将接收到的数据转换成字符串,并去掉两头的空字节
var response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
response = response.Trim('\0'); OutputMessage(response);
}
else
{
OutputMessage(e.SocketError.ToString());
} // 继续异步地接收数据
_socket.ReceiveFromAsync(e);
} private void OutputMessage(string data)
{
// 在聊天文本框中输出指定的信息,并将滚动条滚到底部
this.Dispatcher.BeginInvoke(
delegate
{
txtChat.Text += data + "\r\n";
svChat.ScrollToVerticalOffset(txtChat.ActualHeight - svChat.Height);
}
);
} private void btnSend_Click(object sender, RoutedEventArgs e)
{
SendData();
} private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
SendData();
this.Focus();
}
}
}
}
OK
[源码下载]
与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室的更多相关文章
- 与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室
原文:与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...
- Python编写基于socket的非阻塞多人聊天室程序(单线程&多线程)
前置知识:socket非阻塞函数(socket.setblocking(False)),简单多线程编程 代码示例: 1. 单线程非阻塞版本: 服务端: #!/usr/bin/env python # ...
- 基于tcp和多线程的多人聊天室-C语言
之前在学习关于网络tcp和多线程的编程,学了知识以后不用一下总绝对心虚,于是就编写了一个基于tcp和多线程的多人聊天室. 具体的实现过程: 服务器端:绑定socket对象->设置监听数-> ...
- 基于websocket实现的一个简单的聊天室
本文是基于websocket写的一个简单的聊天室的例子,可以实现简单的群聊和私聊.是基于websocket的注解方式编写的.(有一个小的缺陷,如果用户名是中文,会乱码,不知如何处理,如有人知道,请告知 ...
- C 基于UDP实现一个简易的聊天室
引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...
- 与众不同 windows phone (33) - Communication(通信)之源特定组播 SSM(Source Specific Multicast)
原文:与众不同 windows phone (33) - Communication(通信)之源特定组播 SSM(Source Specific Multicast) [索引页][源码下载] 与众不同 ...
- 与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)
原文:与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast) [索引页][源码下载] 与众不同 wind ...
- 与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信
原文:与众不同 windows phone (29) - Communication(通信)之与 OData 服务通信 [索引页][源码下载] 与众不同 windows phone (29) - Co ...
- 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议
[源码下载] 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议 作者:webabcd 介绍与众不同 windows ...
随机推荐
- 【转】如何在CentOS/RHEL中安装基于Web的监控系统 linux-das
Linux-dash是一款为Linux设计的基于Web的轻量级监控面板.这个程序会实时显示各种不同的系统属性,比如CPU负载.RAM使用率.磁盘使用率.网速.网络连接.RX/TX带宽.登录用户.运行的 ...
- 07-UIKit(tableview的编辑模式、accessoryView)
目录: 一.tableview的编辑模式-增删改查 二.不使用继承创建tableview 三.accessoryView辅助视图 回到顶部 一.tableview的编辑模式-增删改查 [1-conta ...
- Python 绝对简明手册
Python 绝对简明手册 help(函数名)来获取相关信息 另外,自带的文档和google也是不可少的 2. 基本语法2.1. if / elif / else x=int(raw_input(&q ...
- Qt核心剖析:信息隐藏(三篇)
http://devbean.blog.51cto.com/448512/335550 http://devbean.blog.51cto.com/448512/325581 http://devbe ...
- OGR 官方文档
OGR 官方文档 http://www.gdal.org/ogr/index.html The OGR Simple Features Library is a C++ open source lib ...
- linux下shutdown无法关闭tomcat进程的解决方式
1.问题 笔者在linux下发现使用tomcat6.0.41自带的./shutdown.sh常常无法停止进程,导致各种问题的发生,令笔者相当反感! 2.解决方式一: 查找到全部的tomcat进程 $ ...
- getComputedStyle与currentStyle
currentStyle:获取计算后的样式.也叫当前样式.终于样式. 长处:能够获取元素的终于样式.包含浏览器的默认值,而不像style仅仅能获取行间样式.所以更经常使用到. 注意:不能获取复合样式如 ...
- Swift - 使用UI Dynamics给UIKit组件添加移动吸附行为
UI Dynamics是UIKit的一个新组成部分,它向iOS中的视图提供了与物理学有关的功能和动画.可以让你向视图中引入力和物理属性,可以让你的视图弹跳,舞动,受重力影响等等. 下面通过样例,演示使 ...
- pomelo研究笔记-RPCclient
1. mailbox数据收发模块 一个RPC客户端可能同一时候须要调用多个远端(server)提供的服务.在pomelo里每一个server抽象为一个mailbox.先来看看mailbox的实现: v ...
- SQL Server 存储过程、触发器、游标
存储过程 1.存储过程是事先编好的.存储在数据库中的程序,这些程序用来完成对数据库的指定操作. 2.系统存储过程: SQL Server本身提供了一些存储过程,用于管理有关数据库和用户的信息. 用户存 ...