当前位置: 首页 > 工具软件 > MyQQ > 使用案例 >

MyQQ project 2

阎星华
2023-12-01

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
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的Socket
        private System.Threading.Thread thdUdp; //创建一个线程
        public delegate void DataArrivalEventHandler(byte[] Data, IPAddress Ip, int Port);  //定义了一个托管方法
        public event DataArrivalEventHandler DataArrival;   //通过托管理在控件中定义一个事件
        private string localHost = "127.0.0.1";
        [Browsable(true), Category("Local"), Description("本地IP地址")]   //在“属性”窗口中显示localHost属性
        public string LocalHost
        {
            get { return localHost; }
            set { localHost = value; }
        }

        private int localPort = 11000;
        [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);//if it is component ,herein we should assume container;

            InitializeComponent();
        }

        protected void Listener()   //监听
       {
            ServerEndPoint = new IPEndPoint(IPAddress.Any,localPort);   //将IP地址和端口号以网络端点存储
            if (UDP_Server != null)
                UDP_Server.Close();
            //if (UDP_Server == null)
            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);//利用当前控件的DataArrival事件将消息发给远程计算机
                    }
                    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);    //将IP地址和端口号实例化一个IPEndPoint对象
                UDP_Server.Send(Data, Data.Length, server);         //将消息发给远程计算机
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void OpenSocket()    //打开socket
        {
            Listener();         //通过该方法对UDP协议进行监听
        }

        private void CloseSocket()    //关闭socket
        {
            if (UDP_Server != null)     //如果socket不为空
                UDP_Server.Close();     //关闭socket
            if (thdUdp != null)         //如果自定义线程被打开
            {
                Thread.Sleep(30);       //睡眠主线程
                thdUdp.Abort();         //关闭子线程
            }
        }
    }
}
be wise to  why assume  the way of component here

rule:whatever you need to set the parameters ,you need to assume the componet way;

put the relative method into property .that we make the invoke ways expediently;

in the common sense ,we distinguished server and client apart

at server we do it like this .bind(ipendport) then listen;

if it detects a requst ,it can generate a new socket to send and receive;

at client .connect (server endpoint) then send and receive;

 

but herein it did :

ipendpoint end = new ipendpoint(ipaddress.any,port);

svrsocket.receive(ref end);

 

it didn't tell from server and client;

 类似资料:

相关阅读

相关文章

相关问答