惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
博客园 - 司徒正美
Martin Fowler
Martin Fowler
T
Tailwind CSS Blog
B
Blog RSS Feed
Vercel News
Vercel News
博客园 - 聂微东
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
GbyAI
GbyAI
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
B
Blog

博客园 - 水寒

C++ string 详解 (转)Cstring转char、string、int等数据类型的方法 (转)进程通信(用户自定义消息,用户注册消息,windows剪贴板,WM_COPY, 内存映射,对目标进程的内存) ACE WSA Startup not initialized 问题 21个值得深思的故事! sqlite 索引优化方法 C++内存管理详解 下载Eclipse解压后运行出现问题,出现了jvm terminated.Exit code=-1的错误 Java虚拟机(JVM)中的内存设置详解 每天读一遍,慢慢的你就变了 可以有效改进项目管理技能的十个过程 再次写给我们这些浮躁的程序员 vbcrlf常数 vc字符串函数大全 C++sprintf()函数 UDP组播 一个字符转换引发的问题!诡异..... 再看字节对齐! C++虚函数表解析(转)
C#简单组播示例
水寒 · 2011-01-16 · via 博客园 - 水寒

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;
                }
            }
        }
   
    }
}