本人将UDP监听程序封类,因为需要循环监听,所以在类里面做了个后台线程,但是问题出现了,-----------我如何从封的类中返回接收到的信息?为了防止混乱,沙发处贴码;
头文件:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

解决方案 »

  1.   

    public class MyThread
        {
            Server mys = new Server();
            public void Run()//此线程用来监听发的数据包
            {
                Thread myth = new Thread(new ThreadStart(mys.receive));
                myth.IsBackground = true;
                myth.Start();
            }
        }
        class Server
        {
            private UdpClient udp = new UdpClient(2006);
            private IPEndPoint remoteEndPoint;        public void receive()
            {
                PrintHandler myPrint = new PrintHandler(getBack);
                while (true)//用死循环来监听发的数据包
                {
                    remoteEndPoint = new IPEndPoint(new IPAddress(0), 0);
                    byte[] data = udp.Receive(ref remoteEndPoint);
                    string response = Encoding.Default.GetString(data);
                    myPrint(response);
                }
            }
            public string getBack(string response)
            {
                return response;//!!!问题所在,如何返回此值到调用的应用程序?!!!
            }
        }
      

  2.   

    想过使用Invoke,但本人不才,写不成;
    PS:顶者有分,答者多分,解者加多分.
      

  3.   

    不好意思,后面少了个委托的声明.......
    public delegate string myPrint(string str);
      

  4.   

    昏,打错委托名称 public delegate string PrintHandler(string str);
      

  5.   

    应您要求:  这个只是一个DLL的设计,如何将收到的数据包传给调用它的应用程序?
    namespace UDPLine
    {
       
        public class MyThread
        {
            Server mys = new Server();
            public void Run()//此线程用来监听发的数据包
            {
                Thread myth = new Thread(new ThreadStart(mys.receive));
                myth.IsBackground = true;
                myth.Start();
            }
        }
        class Server
        {
            private UdpClient udp = new UdpClient(2006);
            private IPEndPoint remoteEndPoint;        public void receive()
            {
                PrintHandler myPrint = new PrintHandler(getBack);
                while (true)//用死循环来监听发的数据包
                {
                    remoteEndPoint = new IPEndPoint(new IPAddress(0), 0);
                    byte[] data = udp.Receive(ref remoteEndPoint);
                    string response = Encoding.Default.GetString(data);
                    myPrint(response);
                }
            }
            public string getBack(string response)
            {
                return response;//!!!问题所在,如何返回此值到调用的应用程序?!!!
            }
        }   public class Client
        {
            private UdpClient client = new UdpClient(); //客户端
            private IPEndPoint serverIP;
            public void send(string IP, string content)
            {
                serverIP = new IPEndPoint(IPAddress.Parse(IP), 2006); //服务器IP
                byte[] mybyte = Encoding.Default.GetBytes(content);
                client.Send(mybyte, mybyte.Length, serverIP);
            }
        }
        public delegate string PrintHandler(string str);    // 声明委托类型}
      

  6.   

    收到数据写入到一个List中。增加一个方法从List中取数据。
      

  7.   

    答:
       wdy9927() 这样的做法并不实际,因为我不知道什么时候有数据发来,什么时候去读List.
       
       pkkfaii(火柴头),这个在单个应用程序里面的实现很简单,但是不是我要问的主旨,我是要该类拥有高重用性,我把"发"和"收"放到同一个命名空间,不代表用同一个程序调用.
      

  8.   

    wdy9927() 这样的做法并不实际,因为我不知道什么时候有数据发来,什么时候去读List.
    ---------------------------------------------------
    不清楚你的需求了,我觉得你这个dll就应该是接收数据,然后放到某个位置,然后调用这个dll的程序需要的时候就来读,也可以设置个循环,不断地去读。
    如果实在不行或许用事件可以。但dll中怎么做才好呢?
      

  9.   

    哈哈,这种问题我刚刚遇到过,
    不过……没有彻底解决,最后改了设计这种事情,要么拉,要么推推的话,只能是在你的 public string getBack(string response)中就调用要取用数据的函数,把response放在其参数里。
    拉的话,那就得再有一个循环侦听,一捕捉到即马上开始使用数据。委托触发的方式我也考虑过,没弄懂,感觉似乎也简单不起来。
    可以参考一下下边这个:
    http://zhuweisky.cnblogs.com/archive/2005/09/26/244318.aspx
    如果点不开请看快照:
    http://cache.baidu.com/c?word=%C6%F3%D2%B5%3B%BF%AA%B7%A2%3B%BB%F9%B4%A1%3B%C9%E8%CA%A9%3B%2D%3B%2D%3B%CA%C2%BC%FE%3B%CD%A8%D6%AA%3B%B7%FE%CE%F1&url=http%3A//www%2Ecnblogs%2Ecom/zhuweisky/archive/2005/09/26/244318%2Ehtml&p=8f70cd15d9c150f517be9b7f4a&user=baidu
      

  10.   

    用自定义事件可以的
    public delegate void ReviceEndHander(object sender, EventArgs e);
    public event ReviceEndHander ReviceEnd;
    protected virtual void OnReviceEnd(object sender,EventArgs e)
    {
      if (ReviceEnd!=null)
         ReviceEnd(this,e)
    }
    public void revicedata()
    {
       ..........
       this.OnReviceEnd(this,new EventArgs ());
    }
    然后在窗体中引用这个时间就可以了
      

  11.   

    http://www.codeproject.com/useritems/winsock2005.asp这个别人已经封装好了的,多线程,异步