我想做一个服务器端,负责发送文本信息,而客户端只负责接收服务器端发送的信息,做成广播式的发送。请问我应该如何实现?小弟没有思路,各位大哥帮忙。最好给个例子

解决方案 »

  1.   

    UDP通讯组件using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Diagnostics;namespace UDPComm
    {
        public class UDP
        {
            public delegate void dlg_UDPMsgHandler(string receivebytes);
            public static event dlg_UDPMsgHandler UDPMsgArrived;        dlg_UDPMsgHandler udpmsghandle = new dlg_UDPMsgHandler(UDPRaiseEvent);
            
            public static void Send(string range, string datagram)
    {
    UdpClient sender = new UdpClient();
    IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(range),10001);            try
    {
    datagram += (char)3;
    byte[] bytes = Encoding.ASCII.GetBytes(datagram);
    sender.Send(bytes,bytes.Length,endPoint);
    }
    catch (Exception ex)
    {
    Debug.WriteLine(ex.ToString());
    return;
    }
    finally
    {
    sender.Close();
    }
            }        public static void Receiver()
            {            UdpClient receiveUdpClient = new UdpClient(10001);
                IPEndPoint RemoteIPEndPoint = null;            try
                {
                    while (true)
                    {
                        byte[] receiveBytes = receiveUdpClient.Receive(ref RemoteIPEndPoint);
                        string returnData = Encoding.ASCII.GetString(receiveBytes);
                        if (returnData.IndexOf((char)3) > -1)
                        {
                            UDPRaiseEvent(returnData.Substring(0, returnData.Length - 1));
                        }
                        else
                            return;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    return;
                }
            }        public static void UDPRaiseEvent(string recebytes)
            {
                UDPMsgArrived(recebytes);
            }
        }
    }
      

  2.   

    服务器:
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;
    using UDPComm;namespace UdpServer
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
        class Char
        {
            [STAThread]
            static void Main(string[] args)
            {
                try
                {
                    Thread tRec = new Thread(new ThreadStart(UDPComm.UDP.Receiver));
                    tRec.Start();
                    UDP.UDPMsgArrived += new UDP.dlg_UDPMsgHandler(UDP_UDPMsgArrived);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }        static void UDP_UDPMsgArrived(string receivebytes)
            {
                Console.WriteLine(System.DateTime.Now.ToShortTimeString()+" "+receivebytes);
            }
        }
    }
      

  3.   

    客户端;
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;
    using UDPComm;namespace UdpServer
    {
    class Char
    {
    [STAThread]
    static void Main(string[] args)
    {
    try
    {
    Thread tSend = new Thread(new ThreadStart(s));
    tSend.Start();
    Console.ReadLine();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.ToString());
    }
    }
    private static void s()
    {
    string D = ((char)22).ToString();
    string F = ((char)3).ToString();
    string S = ((char)16).ToString(); for (int i=0;i<1000;i++)
    {
    Console.WriteLine(i.ToString());
    UDPComm.UDP.Send("127.255.255.255", "IVIP1"+D+"250"+S+"301"+S+System.DateTime.Now.ToString("yyyyMMdd")+S+System.DateTime.Now.ToString("HHmmss")+S+"1"+S+i.ToString().PadLeft(15));
    Thread.Sleep(1000);
    }
                //How to end the UDP receive thread
                UDPComm.UDP.Send("127.255.255.255", F);
    }
    }
    }
      

  4.   

    to: wangsaokui(无间道III(终极无间)) 不用设置服务器的发送网段么?比如192.168.1.*这样的?
    发送127.255.255.255是发送到哪里?
      

  5.   

    为什么我把Server这端停了,Client这端还在不断记数啊?什么样的情况下表示正常连通?是不是在Server这端也有显示的情况下才是正常?要不要关掉防火墙?
      

  6.   

    to 为什么我把Server这端停了,Client这端还在不断记数啊?什么样的情况下表示正常连通?是不是在Server这端也有显示的情况下才是正常?要不要关掉防火墙?server关闭,需要发送消息进行通知所有client端。