1. 程式人生 > >UDP協議下內網與公網IP進行傳送訊息

UDP協議下內網與公網IP進行傳送訊息

namespace p2pserver
{
    public partial class Form1 : Form
    {
        private Thread th;
        private UdpClient tcpl;
        public bool listenerRun = true;
        private ArrayList pclient;//
        IPEndPoint groupEP;
        public Form1()
        {
            InitializeComponent();
            this.button2.Enabled = false;
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        private void Listen()
        {
            try
            {
                tcpl = new UdpClient(5270);//在5656埠新建一個TcpListener物件
                 groupEP = new IPEndPoint(IPAddress.Any, 5270);
                 pclient = new ArrayList();
                while (listenerRun)//開始監聽
                {
                    pclient.Add(groupEP);   
                    Byte[] stream = new Byte[80];
                    stream = tcpl.Receive(ref groupEP);
                    this.textBox1.AppendText(groupEP.ToString() + System.Text.Encoding.UTF8.GetString(stream));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {