控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

C#简单组播示例

作者:佚名 出处:互联网 2011年01月17日 阅读:

C#简单组播示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MultiCastServer server = new MultiCastServer();
            MultiCastClient client = new MultiCastClient();

            Thread ClientThread = new Thread(new ThreadStart(client.ReceiveData));
            ClientThread.Start();

            Thread ServerThread = new Thread(new ThreadStart(server.SendData));
            ServerThread.Start();

        }
    }


    class MultiCastServer {
        private IPAddress multicastIP = IPAddress.Parse("224.110.10.1");
        private int port = 5001;


        public void SendData()
        {
            Console.WriteLine("Sender Start");
            IPEndPoint multicastIep = new IPEndPoint(multicastIP, port);
            UdpClient sendUdpClient = new UdpClient();

            sendUdpClient.EnableBroadcast = true;

            string sendString = "How   are   you";
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sendString);

            try
            {
                sendUdpClient.Send(bytes, bytes.Length, multicastIep);
            }
            catch
            {
                Console.WriteLine("send error");
            }
            finally
            {
                sendUdpClient.Close();
                Console.WriteLine("Sender Close");
            }
        }
    }

    class MultiCastClient {

        private IPAddress multicastIP = IPAddress.Parse("224.110.10.1");
        private int port = 5001;
       
        public void ReceiveData()
        {
            Console.WriteLine("Reciever Start");
            UdpClient receiveUdp = new UdpClient(this.port);
            try
            {
                receiveUdp.JoinMulticastGroup(this.multicastIP,10);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message.ToString());
            }

            IPEndPoint remoteHost = null;

            while (true)
            {
                try
                {
                    byte[] bytes = receiveUdp.Receive(ref remoteHost);
                    string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    Console.WriteLine(str);
                }
                catch
                {
                    Console.WriteLine("Reciever Close");
                    break;
                }
            }
        }
   
    }
}

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统