首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

用C#写了一个DLL,怎么传送信息给宿主程序?

2012-07-24 
用C#写了一个DLL,如何传送信息给宿主程序???问题描述:DLL 做了一个 Socket监听,当收到信息(字符串之类的)

用C#写了一个DLL,如何传送信息给宿主程序???
问题描述:

  DLL 做了一个 Socket监听,当收到信息(字符串之类的)后,如何将该信息传送给宿主程序?

 

[解决办法]
事件么~
[解决办法]
用射映和动态解析
[解决办法]

C# code
using System;using System.ComponentModel;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;using System.Windows.Forms;using System.Threading;namespace QQClass{    public partial class UDPSocket : Component    {        private IPEndPoint ServerEndPoint = null;   //定义网络端点        private UdpClient UDP_Server = new UdpClient(); //创建网络服务,也就是UDP的Sockets        private System.Threading.Thread thdUdp; //创建一个线程        public delegate void DataArrivalEventHandler(byte[] Data, IPAddress Ip, int Port);  //定义了一个托管方法        public event DataArrivalEventHandler DataArrival;   //通过托管在控件中定义一个事件              private string localHost = "192.168.1.126";        [Browsable(true), Category("Local"), Description("本地IP地址")]   //在“属性”窗口中显示localHost属性        public string LocalHost        {            get { return localHost; }            set { localHost = value; }        }       // private int localPort = 11000;        private int localPort =80;        [Browsable(true), Category("Local"), Description("本地端口号")] //在“属性”窗口中显示localPort属性        public int LocalPort        {            get { return localPort; }            set { localPort = value; }        }        private bool active = false;        [Browsable(true), Category("Local"), Description("激活监听")]   //在“属性”窗口中显示active属性        public bool Active        {            get { return active; }              set //该属性读取值            {                 active = value;                if (active) //当值为True时                {                    OpenSocket();   //打开监听                }                else                {                    CloseSocket();  //关闭监听                }            }        }        public UDPSocket()        {            InitializeComponent();        }        public UDPSocket(IContainer container)        {            container.Add(this);            InitializeComponent();        }        protected void Listener()   //监听        {            ServerEndPoint = new IPEndPoint(IPAddress.Any,localPort);   //将IP地址和端口号以网络端点存储            if (UDP_Server != null)                UDP_Server.Close();            UDP_Server = new UdpClient(localPort);  //创建一个新的端口号            UDP_Server.Client.ReceiveBufferSize = 1000000000;   //接收缓冲区大小            UDP_Server.Client.SendBufferSize = 1000000000;  //发送缓冲区大小                        try            {                thdUdp = new Thread(new ThreadStart(GetUDPData));   //创建一个线程                thdUdp.Start(); //执行当前线程            }            catch (Exception e)            {                MessageBox.Show(e.ToString());  //显示线程的错误信息            }        }        private void GetUDPData()   //获取当前接收的消息        {            while (active)            {                try                {                    byte[] Data = UDP_Server.Receive(ref ServerEndPoint);   //将获取的远程消息转换成二进制流                    if (DataArrival != null)                    {                        DataArrival(Data, ServerEndPoint.Address, ServerEndPoint.Port);                    }                    Thread.Sleep(0);                }                catch { }            }        }        private void CallBackMethod(IAsyncResult ar)        {            //从异步状态ar.AsyncState中,获取委托对象            DataArrivalEventHandler dn = (DataArrivalEventHandler)ar.AsyncState;            //一定要EndInvoke,否则你的下场很惨            dn.EndInvoke(ar);        }        public void Send(System.Net.IPAddress Host, int Port, byte[] Data)        {            try            {                IPEndPoint server = new IPEndPoint(Host, Port);                UDP_Server.Send(Data, Data.Length, server);            }            catch (Exception e)            {                MessageBox.Show(e.ToString());            }        }         private void OpenSocket()        {            Listener();        }        private void CloseSocket()        {            if (UDP_Server != null)                UDP_Server.Close();            if (thdUdp != null)            {                Thread.Sleep(30);                thdUdp.Abort();            }        }    }} 


[解决办法]
接口,委托,事件。
看你的需要了

热点排行