资源简介
实现组播模式通信,服务器端发出消息,所有客户端接收到消息。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace NBC.Test.objectModel
{
///
/// 组播通信类
///
public class SocketUDP
{
///
/// 组播模式发送信息
///
///
public void SendMessage(object obj)
{
//定义消息格式
//日期_/时间_/活动时长_/指标_/出课教师_/班级_/学科_/课题_/ClassID
Activityobject mess = ((Operateobject)obj).Acobject;//此代码是我自定义的消息对象,使用时可按照自己需求从新定义
IPAddress GroupAddress = IPAddress.Parse(“234.168.100.2“); //此IP值为固定区间 详情请查询msdn
int GroupPort = 11000;
UdpClient sender = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress GroupPort);
sender.EnableBroadcast = true;
try
{
byte[] bytes = UTF8Encoding.UTF8.GetBytes(mess.Date + “_/“ + mess.Time + “_/“ + mess.ActivityLength + “_/“ + mess.Index + “_/“ + mess.Techer + “_/“ + mess.Grade + “_/“ + mess.Subject + “_/“ + mess.Project + “_/“ + mess.ClassID);
sender.Send(bytes bytes.Length groupEP);
//sender.Close();
}
catch
{
}
}
//创建一个UdpClient实例
private Socket udpReceive;
///
/// 接收组播信息
///
private void ReceiveMessage()
{
评论
共有 条评论