Socket向指定的一批IP发送信息,只要这个功能就行了

解决方案 »

  1.   

    简单的多人聊天(C#.Socket).
    http://www.sohozu.com/2005/5-24/1305423866.Html
      

  2.   

    //**************发送端********************
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace Socket广播发送端
    {
        public partial class Form1 : Form
        {
            private IPEndPoint enLoacp;
            private Socket mySocket;
            private Thread Mythread;
            private byte[] sendbuff;
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
                enLoacp = new IPEndPoint(IPAddress.Broadcast, 9050);
                mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                
                Mythread = new Thread(new ThreadStart(Recive));
                Mythread.Name = "a";
                Mythread.Start();
            }
            private void Recive()
            {
                try
                {
                    while (true)
                    {
                        try
                        {
                            sendbuff = Encoding.Default.GetBytes("现在时间:" + DateTime.Now.ToLongTimeString());
                            mySocket.SendTo(sendbuff, enLoacp);
                            Thread.Sleep(2000);
                        }
                        catch (Exception ex)
                        {                    }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  3.   

    //********************接收端*********************
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    namespace Socket广播
    {
        public partial class Form1 : Form
        {
            private IPEndPoint enLoacp;
            private Socket mySocket;
            private Thread Mythread;
            private EndPoint ep;
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                CheckForIllegalCrossThreadCalls = false;
                enLoacp = new IPEndPoint(IPAddress.Any, 9050);
                mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                mySocket.Bind(enLoacp);
                ep = (EndPoint)enLoacp;
                Mythread = new Thread(new ThreadStart(Recive));
                Mythread.Name = "a";
                Mythread.Start();
            }
            private void Recive()
            {
                try
                {
                    while (true)
                    {
                        try
                        {
                            if (mySocket == null)
                            {
                                Thread.Sleep(1000);
                                continue;
                            }
                            else
                            {
                                if (listBox1.Items.Count > 10)
                                {
                                    listBox1.Items.Clear();
                                }
                                byte[] b=new byte[1024];
                                int size = mySocket.ReceiveFrom(b,ref ep);
                                string strbuff = Encoding.Default.GetString(b,0,size);
                                listBox1.Items.Add("收到广播消息:" + strbuff);
                                
                            }
                        }
                        catch(Exception ex)
                        { 
                        
                        }
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (Mythread != null & Mythread.ThreadState != ThreadState.Unstarted && Mythread.ThreadState != ThreadState.Stopped)
                {
                    Mythread.Abort();
                }
                if (mySocket != null)
                {
                    mySocket.Close();
                }
            }
        }
    }
      

  4.   

    MSDN自带的就有 自己去查一下UDPClient
      

  5.   

    用UDPClient进行收发才叫广播么??
    那Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)这种形式也应该叫广播吧?